Programs using Functions

Aim: 8.1 Write a program that defines a function to add first n numbers.

Code:

#include <stdio.h>

void main()

{

    int i, num, sum = 0;

    printf("\nEnter an integer number : ");

    scanf ("%d", &num);

    for (i = 1; i <= num; i++)

    {

        sum = sum + i;

    }

    printf ("\nSum of first %d natural numbers = %d\n", num, sum);

}

Output screenshot:





Aim: 8.2 Write a function in the program to return 1 if the number is prime otherwise return 0.

Code:

#include<stdio.h>

int prime_num(int);

void main()

{

    int n,result=0;

    printf("\n Enter One Number : ");

    scanf("%d",&n);

    result=prime_num(n);

    if( result==0)

    {

        printf("\n %d is prime number. ",n);

    }

    else

    {

        printf("\n %d is not prime number. ",n);

    }

}

int prime_num(int a)

{

    int i;

    for(i=2;i<=a/2;i++)

    {

        if(a%i!=0)

        {

            continue;

        }

        else

        {

            return 1;

        }

    }

    return 0;

}

Output screenshot:

Aim: 8.3 Write a function Exchange to interchange the values of two variables, say x and y. illustrate the use of this function in a calling function.

Code:

#include<stdio.h>

#include<conio.h>

void exchange(int x,int y);

int main()

{

    int x,y;

    printf("\nEnter two numbers : \n");

    scanf("%d%d",&x,&y);

    printf("\nBefore Exchange x = %d and y = %d",x,y);

    exchange(x,y);

    getch();

}

void exchange(int x,int y)

{

    int z;

    z=x;

    x=y;

    y=z;

    printf("\nAfter Exchange x = %d and y = %d",x,y);

}

Output screenshot:

Aim: 8.4 Write a C program to use recursive calls to evaluate 

F(x) = x – x^3 / 3! + x^5 / 5 ! – x^7 / 7! +... x^n / n!.

Code:

#include<stdio.h>

#include<math.h>

float rec_call(int,int);

int fact(int);

int main()

{

    int n,x;

    float sum=0;

    printf("\nEnter Value of X : ");

    scanf("%d",&x);

    printf("\nEnter no of iteration : ");

    scanf("%d",&n);

    sum = rec_call(x,n);

    printf("\nSum = %f",sum);

    return 0;

}

float rec_call(int x,int n)

{

    static float sum;

    if(n==1)

    return sum+x;

    if(n%2==0)

    {

        sum =sum -((pow(x,(2*n)-1)*1.0)/fact((2*n)-1));

    }

    else

    {

    sum =sum +((pow(x,(2*n)-1)*1.0)/fact((2*n)-1));

    }

    rec_call(x,--n);

}

int fact(int n)

{

    if(n==1)

    return 1;

    return n*fact(n-1);

}

Output screenshot:

Aim: 8.5 Write a function that will scan a character string passed as an argument and convert all lowercase characters into their uppercase equivalents.

Code:

#include <stdio.h>

void UpperCase(char *);


int main(void) 

{

     char str[50];

     printf("Enter a String : ");

     scanf("%s",str);

     UpperCase(str);

     printf("String in Upper Case : %s",str);

     return 0;

}

void UpperCase(char *ch)

{

     int i=0;

     while(ch[i]!='\0')

     {

          if(ch[i]>='a' && ch[i]<='z')

          {

               ch[i]=ch[i]-32;

          }

          i++;

     }

}

Output screenshot:






Aim: 8.6 Write a program to find factorial of a number using recursion.

Code:

#include<stdio.h>

long int multiplyNumbers(int n);

int main()

{

    int n;

    printf("Enter a positive integer : ");

    scanf("%d",&n);

    printf("Factorial of %d = %ld", n, multiplyNumbers(n));

    return 0;

}

long int multiplyNumbers(int n)

{

    if (n>=1)

    return n*multiplyNumbers(n-1);

    else

    return 1;

}

Output screenshot:

No comments:

Post a Comment

If you have any doubts or suggestions, Please let me know