FUNCTIONS


Distance between 2 points

Write a program to find the distance between 2 points using functions.

Function specification:

float findDistance(int x1, int y1, int x2, int y2)

The function accepts 4 integer and returns a float.

Input Format:
Input consists of 4 integers. The first and second integer corresponds to the x-coordinate and y-coordinate of the first point. The third and fourth integer corresponds to the x-coordinate and y-coordinate of the second point.

Output Format:
Output consists of a single floating point number (correct to 2 decimal places.) Refer sample output for formatting details.

Sample Input:
3
6
4
3

Sample Output:
Distance between the 2 points is 3.16


Function Definitions: 

float findDistance (int x1, int y1, int x2, int y2) 

Program

01  #include<stdio.h>
02#include<math.h>
03float findDistance(int x1,int y1,int x2,int y2)
04{
05    float dist=sqrt(pow((x2-x1),2.0)+pow((y2-y1),2.0));
06    return dist;
07}
08int main(void)
09{
10    int x1,x2,y1,y2;
11    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
12    printf("Distance between the 2 points is %.2f",findDistance(x1,y1,x2,y2));
13    return 0;
14}



Factorial of a number

Write a program to find the factorial of a number using functions.

Function Specification:

int factorial(int n)
The function accepts a int and returns an int.

Input Format:
Input consists of 1 integer.

Output Format:
Output consists of a single integer. Refer sample output for formatting details.

Sample Input:
3

Sample Output:
6


Function Definitions: 

int factorial (int n) 

Program

01  #include<stdio.h>
02int factorial(int n)
03{
04    int fact=1;;
05    while(n!=0)
06    {fact*=n;
07    n--;}
08    return (fact);
09}
10int main(void)
11{
12    int n;
13    scanf("%d",&n);
14    printf("%d",factorial(n));
15    return 0;
16}





Functions – Lucky String

Write a program to find whether the given string is Lucky or not.

A string is said to be lucky if the sum of the ascii values of the characters in the string is even.

Function specifications:

int checkLucky(char * a)

The function accepts a pointer to a string and returns an int.
The return value is 1 if the string is lucky and 0 otherwise.

Input and Output Format:
Input consists of a string. Assume that all characters in the string are lowercase letters and the maximum length of the string is 100.
Refer sample input and output for formatting specifications.
All text in bold corresponds to input and the rest corresponds to output.

Sample Input and Output 1:
Enter the input string
anitha
anitha is not lucky

Sample Input and Output 2:
Enter the input string
technology
technology is lucky



Function Definitions: 

int checkLucky (char * a) 

Program

01  #include<stdio.h>
02#include<string.h>
03int checkLucky(char *a)
04{
05     
06    int i,sum=0;
07    int size=strlen(a);
08    for(i=0;i<size;i++)
09    sum+=a[i];
10    if(sum%2==0)
11    return 1;
12    else return 0;
13     
14}
15int main(void)
16{
17    char str[100];
18    int flag=0;
19    printf("Enter the input string");
20    scanf("%s",str);
21    char *ptr=str;
22     
23    flag=checkLucky(ptr);
24     
25    if(flag==1)
26    printf("\n%s is lucky",str);
27    else
28    if(flag==0)
29    printf("\n%s is not lucky",str);
30    return 0;
31}