Nakum Anil

PRACTICAL-SET-6

6_1.c
 Write a function program to add first N numbers.


#include<stdio.h>
#include<conio.h>
int add(int a);
void main()
{
    int n,d;
    clrscr();
    printf("enter the value of n: ");
    scanf("%d",&n);
    d=add(n);
    printf("sum of n numbers is %d",d);
    getch();
}
    add(int a)
{
    int i,c=0;
    for(i=1;i<=a;i++)
    {
        c=c+i;
    }
        return(c);

}



6_2.c
 Write a function find out maximum out of three numbers.


#include<stdio.h>
#include<stdio.h>
int max(int a,int b,int c);
void main()
{
    int d,e,f,g;
    clrscr();
    printf("enter the three value");
    scanf("%d%d%d",&d,&e,&f);
    g=max(d,e,f);
    printf("maximum is %d",g);
    getch();
}
    int max(int a,int b,int c)
{
    if(b>a)
    {
        if(b>c)
        {
        return(b);
        }
        else
        {
        return(c);
        }
    }
    else
    {
    return(a);
    }
}



6_3.c
Write a function power that computes x raised to the power y for integer x and y and returns double type value.


#include<stdio.h>
#include<conio.h>
double power(int x,int y);
void main()
{
    int a,b;
    float c;
    clrscr();
    printf("enter the value of x and y");
    scanf("%d%d",&a,&b);
    c=power(a,b);
    printf("power of x to y is %f",c);
    getch();
}
    double power(int x,int y)
    {
        float c=1.0;
        int count=1;
        while(count<=y)
        {
            c=c*x;
            count++;
        }
        return(c);
    }



6_4.c
 Write a program to find factorial of a number using recursion.


#include<stdio.h>
#include<conio.h>
long int factorial(int n);
void main()
{
    int a;
    long int ans;
    clrscr();
    printf("enter the value and take factorial: ");
    scanf("%d",&a);
    ans=factorial(a);
    printf("your number`s factorial is %d",ans);
    getch();
}
    long int factorial(int n)
    {
        long int ans;
        if(n==1)
        {
            return(1);
        }
        else
        {
            ans=n*factorial(n-1);
            return(ans);
        }
    }



6_5.c
 Write a program that used user defined function Swap ( ) and interchange the value   of two variable.


#include<stdio.h>
#include<conio.h>
int swep(int a,int b,int *x,int *y);
void main()
{
    int c,d,x,y;
    clrscr();
    printf("enter the value of a: ");
    scanf("%d",&c);
    printf("enter the value of b: ");
    scanf("%d",&d);
    swep(c,d,&x,&y);
    printf("sweping a=%d and b=%d",x,y);
    getch();
}
    int swep(int a,int b,int *m,int *n)
    {
        *m=b;
        *n=a;
    }




6_6.c
 Write a function prime that return 1 if it‘s argument is prime and return 0 otherwise.


#include<stdio.h>
#include<conio.h>
int prime(int x);
void main()
{
    int a,c;
    clrscr();
    printf("enter the number");
    scanf("%d",&a);
    c=prime(a);
    if(c==1)
    {
        printf("%d number is prime",a);
    }
    else
    {
        printf("%d number is not prime",a);
    }
    getch();
}
    int prime(int x)
    {
        int i;
        for(i=2;i<=x/2;i++)
        {
            if(x%i!=0)
            {
                continue;
            }
            else
            {
                return(0);
            }
        }
        return(1);
    }




6_7.c
Write a calculator program(add,subtract,multiply,divide). Prepare user defined function for each functionality.


#include<stdio.h>
#include<conio.h>
float add(float a,float b);
float sub(float i,float j);
float mul(float k,float l);
float divi(float m,float n);
void main()
{
    float c,d,e,f,g,h;
    clrscr();
    printf("enter the two value for addition,subtraction,multiplication and division: ");
    scanf("%f%f",&c,&d);
    e=add(c,d);
    f=sub(c,d);
    g=mul(c,d);
    h=divi(c,d);
    printf("addition is %f\nsubtraction is %f\nmultiplication is %f\ndivision is %f",e,f,g,h);
    getch();
}
    float add(float a,float b)
    {
        float e;
        e=a+b;
        return(e);
    }
    float sub(float i,float j)
    {
        float f;
        f=i-j;
        return(f);
    }
    float mul(float k,float l)
    {
        float g;
        g=k*l;
        return(g);
    }
    float divi(float m,float n)
    {
        float h;
        h=m/n;
        return(h);
    }

No comments:

Post a Comment