Computer Scientists are Pretty Pessimistic

Showing posts with label Function. Show all posts
Showing posts with label Function. Show all posts

Monday, 5 October 2015

WAP whether a number is prime or not using function.

CODE ::

#include <stdio.h>
#include <math.h>
int check (int p);
int main ()
{
    int n;
    printf("Enter a number: ");
    scanf("%d",&n);
    if(check(n))
        printf("Prime\n");
    else
        printf("Not prime\n");
    return 0;
}
int check (int p)
{
    int i,f=0,limit;
    limit=sqrt(p);
    for(i=2;i<=limit;i++)
    {
        if(p%i==0)
        {
            f=1;
            break;
        }
    }
    if(f==0)
        return 1;

    return 0;
}

Sunday, 4 October 2015

WAP to find the sum of following series using function.

1 + 2^2/2! + 3^3/3! + - - - - - -  - - - + n^2/n!

CODE ::

#include <stdio.h>
long int fact(int n);
long int p(int n);
int main ()
{
    double sum=0;
    int i,n;
    printf("Enter term N = ");
    scanf("%d",&n);

    for(i=1;i<=n;i++)
        sum = sum + (double)p(i)/(double)fact(i);

    printf("%lf\n",sum);
    return 0;
}
long int fact (int n)
{
    int i,res=1;
    for(i=n;i>=1;i--)
        res = res*i;

    return res;
}
long int p (int n)
{
    return n*n;
}

Friday, 4 September 2015

Write a function that returns the sum of the following series where x and n are passed to the function as argument. (1 + x2/2! + x4/4! + x6/6! + …….xn/n!)

#include <stdio.h>
double sum (int x, int n);
int main ()
{
    int x,n;
    double res;
    scanf("%d %d",&x,&n);
    res=sum(x,n);
    printf("%.3lf",res);
    return 0;
}
double sum (int x, int n)
{
    int i,j;
    double s=1,f,p;
    for(i=2;i<=n;i+=2)
    {
        f=1;
        p=1;
        for(j=0;j<i;j++)
        {
            p=p*2;
        }
        for(j=1;j<=i;j++)
        {
            f=f*j;
        }
        s=s+(p/f);
    }
    return s;
}

WAP to input 4 integers a, b, c, d and check that the equation a3 + b3 +c3 = d3 is satisfied or not.

#include <stdio.h>
int cube (long long n);
int main ()
{
    long long a,b,c,d;
    printf("Enter the value of a,b,c,d:\n");
    scanf("%lld %lld %lld %lld",&a,&b,&c,&d);
    if(cube(a)+cube(b)+cube(c)==cube(d))
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}
int cube (long long n)
{
    return n*n*n;
}

Write a function to check the equality of two matrices. Matrices are passed to the function as argument and function returns 1 if they are equal else returns 0.

#include <stdio.h>
int check (int a[3][3],int b[3][3]);
int main ()
{
    int i,j,a[3][3],b[3][3],res;
    printf("Enter 1st matrix:\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Enter 2nd matrix:\n");
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    res=check(a,b);
    printf("\n%d\n",res);
    return 0;
}

int check (int a[3][3],int b[3][3])
{
    int i,j,c=0;
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {
            if(a[i][j]==b[i][j])
                c++;
        }
    }
    if(c==9)
        return 1;
    else
        return 0;
}

Write a function to search a string in the array of strings. String and array of strings should be passed to the function as parameters.

#include <stdio.h>
#include <string.h>
void check (char str[],char a_str[]);
void main()
{
    char str[100],a_str[100];
    printf("Enter string:\n");
    gets(str);
    printf("Enter array of string:\n");
    getchar();
    gets(a_str);
    check(str,a_str);
}
void check (char str[],char a_str[])
{
    int i,j,c,k;
    for(i=0;i<a_str[i];i++)
    {
        c=0;
        if(a_str[i]==str[0])
        {
            for(k=i,j=0;str[j];j++,k++)
            {
                if(str[j]==a_str[k])
                    c++;
            }
        }
        if(c==(strlen(str)))
            break;
    }
    if(c==(strlen(str)))
        printf("Found\n");
    else
        printf("Not Found\n");
}

Write a function that returns 1 if the number is prime and 0 if not prime. Number is passed to the function as argument.

#include <stdio.h>
#include <math.h>
int prime(int n);
int main ()
{
    int n,res;
    scanf("%d",&n);
    res=prime(n);
    printf("%d",res);
    return 0;
}
int prime(int n)
{
    int flag=1,m,i;
    m=sqrt(n);
    for(i=2;i<=m;i++)
    {
        if(n%i==0)
        {
            flag=0;
            break;
        }
    }
    if(flag==1)
        return 1;
    else
        return 0;
}

Write a function that accepts a character as argument and returns its ASCII value.

#include <stdio.h>
int check (char m); //function prototype
void main ()
{
    char m;
    scanf("%c",&m);
    printf("%d",check(m)); //call function
}
int check (char m) //function
{
    return m;
}