Important Basic Programming Question And Solution



                                               
1.     Write a  program  that  shows the summation of positive integer in between lower limit to upper limit.
#include <stdio.h>
int main()
{
    int l,u,i,sum=0;
    printf("Enter Lower And Upper Limit=");
    scanf("%d %d",&l,&u);
    for(i=l;i<=u;i++)
    {
        sum=sum+i;
    }
    printf("summation is= %d",sum);
    return 0;
}

2.     Write a program that shows the even summation of positive integer in between lower limit to upper limit.
#include <stdio.h>
int main()
{
    int l,u,i,sum=0;
    printf("Enter Lower And upper Limit=");
    scanf("%d%d",&l,&u);
    for(i=l;i<=u;i++)
    {
        if(i%2==0)
        {
            sum=sum+i;
        }
    }
    printf("Even Summation Is=%d",sum);
    return 0;
}
3. Write a program that show the odd summation of positive integer in between lower limit to upper limit if summation is greater than 9000.
  #include <stdio.h>
int main()
{
    int i,l,u,sum=0;
    printf("Enter Lower And Upper Limit=");
    scanf("%d%d",&l,&u);
    for(i=l;i<=u;i++)
    {
       if(i%2!=0)
       {
           sum=sum+i;
       }
    }
    if(sum>9000)
    {
        printf("Odd Summation Greater Than 9000 is=%d",sum);
    }
    return 0;
}

4. Write a program that show the summation of 1².2!+2².3!+3².4!+………..+n².(n+1)! .
#include <stdio.h>
int main()
{
    int n,m,i,f=1,j,sum1,sum2=0,sum=0;
    printf("Enter Number Of Series=");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        m=i+1;
        f=1;
        for(j=1;j<=m;j++)
        {
            f=f*j;
        }
        sum1=i*i*f;
        sum2=sum2+sum1;
  }
    printf("Summation is=%d",(sum2));
}

 5. Write a program that shows prime/non-prime number.
     Sample input: 6
     Output : 6 is not a prime number


#include <stdio.h>
int main()
{
    int i,n,prime=1;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=2;i<n;i++)
    {
        if(n%i==0)
        {
            prime=0;
        }
    }
    if(prime==1)
    {
        printf("%d is a prime number",n);
    }
    else
    {
        printf("%d is not a prime number",n);
    }
}

 6. Write a program to generate the all prime numbers of a given range.



#include <stdio.h>

int main()

{

    int l,u,i,j,prime;

    printf("Enter Lower And Upper Limit=");

    scanf("%d%d",&l,&u);

    for(i=l;i<=u;i++)

    {

        prime=1;

        for(j=2;j<i;j++)

        {

            if(i%j==0)

            {

                prime=0;

                break;

            }

        }

            if(prime==1)

            {

                printf("%d\n",i);

            }

    }

    return 0;

}



 7. Write a program for nPr .

#include <stdio.h>
int main()
{
    int n,r,i,m,f=1,g=1,j;
    printf("Enter n and r=");
    scanf("%d%d",&n,&r);
    m=n-r;
    for(i=1;i<=n;i++)
    {
        f=f*i;
    }
    for(j=1;j<=m;j++)
    {
        g=g*j;
    }
    printf("npr is=%d",f/g);
    return 0;
}

 8. Write a program for nCr .

#include <stdio.h>
int main()
{
    int n,r,i,j,k,f=1,g=1,h=1,m;
    printf("Enter n and r=");
    scanf("%d%d",&n,&r);
    m=n-r;
    for(i=1;i<=n;i++)
    {
        f=f*i;
    }
    for(j=1;j<=m;j++)
    {
        g=g*j;
    }
    for(k=1;k<=r;k++)
    {
        h=h*k;
    }
    printf("ncr is=%d",(f/(h*g)));
    return 0;
}

9. Write a program that shows the all factorial of positive integer in between lower limit to upper limit.

#include <stdio.h>
int main()
{
    int l,u,i,j;
    printf("Enter Lower And Upper Limit=");
    scanf("%d%d",&l,&u);
    for(i=l;i<=u;i++)
    {
        int f=1;
        for(j=1;j<=i;j++)
        {
            f=f*j;
        }
        printf("%d\n",f);
    }
  return 0;
}
10. Write a program to generate the Fibonacci Series of a given range. (Fibonacci Series= 0 1
1 2 3 5 8 13 …..)
Sample input: 7
Output : 0 1 1 2 3 5 8 13 21
#include <stdio.h>
int main()
{
    int n,a[100000],i;
    a[0]=0;
    a[1]=1;
    printf("Enter Range=");
    scanf("%d",&n);
    for(i=2;i<=n+2;i++)
    {
        a[i]=a[i-1]+a[i-2];
  }
    for(i=0;i<n+2;i++)                                                                                                                                                                
    {
        printf(" %d",a[i]);
    }
}
11. Write a program that shows the multiplication of two matrixes.
#include <stdio.h>
int main()
{
    int a[100][100],b[100][100],c[100][100],i,j,sum,c1,c2,r1,r2,k;
    printf("Enter Row Of Matrix A=");
    scanf("%d",&r1);
    printf("Enter Column Of Matrix A=");
    scanf("%d",&c1);
    printf("Enter Elements=\n");
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c1;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Enter Row Of Matrix B=");
    scanf("%d",&r2);
    printf("Enter Column Of Matrix B=");
    scanf("%d",&c2);
    printf("Enter Elements=\n");
    for(i=0;i<r2;i++)
    {
        for(j=0;j<c2;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    printf("Matrix Multiplication Is=\n");
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c2;j++)
        {
            sum=0;
            for(k=0;k<c1;k++)
            {
                sum=sum+a[i][k]*b[k][j];
          }
             c[i][j]=sum;
             printf("%d\t",c[i][j]);
        }
        printf("\n");
}
    return 0;
}

12. Write a program that shows the summation of two matrixes.
#include <stdio.h>
int main()
{
    int a[100][100],b[100][100],c[100][100],i,j,r1,c1,r2,c2;
    printf("Enter Row Of Matrix A=");
    scanf("%d",&r1);
    printf("Enter Column Of Matrix A=");
    scanf("%d",&c1);
    printf("Enter Elements=\n");
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c1;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Enter Row Of Matrix B=");
    scanf("%d",&r2);
    printf("Enter Column Of Matrix B=");
    scanf("%d",&c2);
    printf("Enter Elements=\n");
    for(i=0;i<r2;i++)
    {
        for(j=0;j<c2;j++)
        {
            scanf("%d",&b[i][j]);
        }
    }
    printf("Sum Of Matrix is=\n");
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c1;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }

    }
    for(i=0;i<r1;i++)
    {
        for(j=0;j<c1;j++)
        {
            printf("%d\t",c[i][j]);
        }
        printf("\n");
    }
    return 0;
}

13. Write a program that shows a transpose matrix.
#include <stdio.h>
int main()
{
    int a[100][100],i,j,t[100][100],r,c;
    printf("Enter Row And Column Of Matrix=");
    scanf("%d%d",&r,&c);
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            t[j][i]=a[i][j];
        }
    }
    printf("Transpose Matrix Is=\n");
    for(i=0;i<c;i++)
    {
        for(j=0;j<r;j++)
        {
            printf("%d\t",t[i][j]);
        }
        printf("\n");
    }
}

 14. Write a program that the show the average marks of best 3 out of 4 CT.
#include <stdio.h>
int main()
{
    int a[100],i,min,n,l,avg,sum=0;
    printf("Enter How Many CT Number=");
    scanf("%d",&n);
    printf("Enter Numbers=\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    min=a[0];
    for(i=0;i<n;i++)
    {
        if(a[i]<=min)
        {
            min=a[i];
        }
    }
    for(i=0;i<n;i++)
    {
        sum=sum+a[i];
    }
    n=n-1;
    sum=sum-min;
    avg=sum/n;
    printf("Average is=%d",avg);
}


15. Write a program that show the output
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

#include <stdio.h>
int main()
{
    int i,j,n;
    printf("Enter Number=\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
       for(j=1;j<=n;j++)
       {
           printf("%d",j);
       }
       printf("\n");
    }
}
16. Write a program that show the output
1
1 2
1 2 3
1 2 3 4
#include <stdio.h>
int main()
{
    int i,j,n;
    printf("Enter Number=\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++)
        {
            printf("%d",j);
        }
        printf("\n");
    }
    return 0;
}

17. Write a program that show the output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include <stdio.h>
int main()
{
    int i,j,n,sp;
    printf("Enter Number=\n");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(sp=1;sp<=n-i;sp++)
        {
            printf(" ");
        }
        for(j=1;j<=i;j++)
        {
            printf("%d",j);
        }
        printf("\n");
    }
    return 0;
}
18. Write a program that show the output
1 2 3 4 5
6 7 8 9
11 12 13
16 17
21

#include <stdio.h>
int main()
{
    int i,j,n,k=1,t=0;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=n;i>=1;i--)
    {
        for(j=1;j<=i;j++,k=k+1)
        {
            printf(" %d",k);
        }
        printf("\n");
        k=k+t;
        t++;

    }
}

19. Write a program that show the output
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

#include <stdio.h>
int main()
{
    int i,j,k=1,n;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=i;j++,k++)
        {
            printf(" %d",k);
        }
        printf("\n");
    }

}
20. Write a program that show the output
         5
      4 5
    3 4 5
  2 3 4 5
1 2 3 4 5

#include <stdio.h>
int main()
{
    int i,j,n,sp;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(sp=n-i;sp>=1;sp--)
        {
            printf(" ");
        }
        for(j=n-i+1;j<=n;j++)
        {
            printf("%d",j);
        }
        printf("\n");
    }
}
21. Write a program that show the output
5 4 3 2 1
4 3 2 1
3 2 1
2 1
#include <stdio.h>
int main()
{
    int i,j,n;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=n;i>=1;i--)
    {
        for(j=i;j>=1;j--)
        {
            printf("%d",j);
        }
        printf("\n");
    }
    return 0;
}
22. Write a program that show the output
          1
      1 2 3
    1 2 3 4 5
  1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9

#include <stdio.h>
int main()
{
    int i,j,sp,n;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        for(sp=1;sp<=n-i;sp++)
        {
            printf(" ");
        }
        for(j=1;j<=2*i-1;j++)
        {
            printf("%d",j);
        }
        printf("\n");
    }
}
23. Write a program to read a string and compare it with predefined string “Rajshahi”.
#include <stdio.h>
#include <string.h>
int main()
{
    char p[]="Rajshahi";
    char a[100];
    printf("Enter Your String=");
    gets(a);
    if(strcmp(p,a)==0)
    {
        printf("Entered String is Matched With Predefined String ");
    }
    else
    {
        printf("Entered String is Not Matched With Predefined String");
    }
    return 0;
}
24. Write a program to read a string and print it in reverse order.
Sample input: Hello
Output : olleH

#include <stdio.h>
#include <string.h>
int main()
{
    int a[100],b[100];
    printf("Enter Your String=");
    gets(a);
    strrev(a);
    printf("Reversed String Is=");
    puts(a);
    return 0;

}
25. Write a program to compute n raised to the power x.
Sample input: n=2 x=3
Output : 2^3=8
#include <stdio.h>
int main()
{
    int n,x,i,r=1;
    printf("Enter n and x=");
    scanf("%d%d",&n,&x);
    for(i=1;i<=x;i++)
    {
        r=n*r;
    }
    printf("%d",r);
    return 0;
}
26. Write a program that show perfect number or not.
Sample input: 6
Output : 6 is a perfect number

#include <stdio.h>
int main()
{
    int n,i,sum=0;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=1;i<n;i++)
    {
        if(n%i==0)
        {
          sum=sum+i;
        }

    }
    if(sum==n)
    {
        printf("%d is Perfect Number",n);
    }
    else
    {
        printf("%d is Not Perfect Number",n);
    }
    return 0;
}
27. Write a program that shows armstrong number or not.
Sample input: 371
Output : 371 is a armstrong number

#include <stdio.h>
int main()
{
    int n,t,sum=0,r;
    printf("Enter Number=");
    scanf("%d",&n);
    t=n;
    while(t!=0)
    {
        r=t%10;
        sum=sum+r*r*r;
        t=t/10;
    }
    if(n==sum)
        printf("%d is an Armstrong Number\n",n);
    else
        printf("5d is Not an Armstrong\n",n);
}
28. Write a program that convert float number into integer number.
Sample input : 7.8
Output : 8

#include <stdio.h>
int main()
{
    float n;
    int m;
    printf("Enter Float Number=");
    scanf("%f",&n);
    m=n+0.5;
    printf("Integer Number is=%d",m);

}
29. Write a program that convert uppercase letter to lowercase letter or vice versa.
Sample input : WeaReCse’13
Output : wEArEcSE’13

#include <stdio.h>
#include <string.h>
int main()
{
    char a[100];
    int i;
    printf("Enter Your String=");
    gets(a);
    for(i=0;i<strlen(a);i++)
    {
        if(islower(a[i]))
        {
            printf("%c",toupper(a[i]));
        }
        else
        {
            printf("%c",tolower(a[i]));
        }
    }
}
30. Write a program that show how many vowels in a word.
Sample input : RUET
Output : 2

#include <stdio.h>
#include <string.h>
int main()
{
    char a[100];
    int i,v=0,l;
    printf("Enter String=");
    gets(a);
    l=strlen(a);
    for(i=1;i<=l;i++)
    {
        if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||a[i]=='i'||a[i]=='I'||a[i]=='o'||a[i]=='O'||a[i]=='u'||a[i]=='U')
        {
            v=v+1;
        }
    }
    printf("%d",v);
}
31. Write a program that show how many consonants in a word.
Sample input : RUET
Output : 2
#include <stdio.h>
#include <string.h>
int main()
{
    char a[100];
    int i,v=0,c,l;
    printf("Enter String=");
    gets(a);
    l=strlen(a);
    for(i=1;i<=l;i++)
    {
        if(a[i]=='a'||a[i]=='A'||a[i]=='e'||a[i]=='E'||a[i]=='i'||a[i]=='I'||a[i]=='o'||a[i]=='O'||a[i]=='u'||a[i]=='U')
        {
            v=v+1;
        }
    }
    c=l-v;
    printf("%d",c);
}
32. Write a program that for below:
Sample input : we live in Bangladesh
Output : we
live
in
Bangladesh

#include <stdio.h>
#include <string.h>
int main()
{
    char a[100];
    int i,l;
    printf("Enter String=");
    gets(a);
    l=strlen(a);
    for(i=0;i<l;i++)
    {
      if(a[i]==' ')
      {
          printf("\n");
      }
      else
        printf("%c",a[i]);
    }
}
33. Write a program that shows strong number or not.
Sample input: 145
Output : 145 is a strong number

#include <stdio.h>
int main()
{
    int i,j,f,sum=0,r,n,t;
    printf("Enter Number=");
    scanf("%d",&n);
    t=n;
    while(t!=0)
    {
        f=1;
        r=t%10;
        for(i=1;i<=r;i++)
        {
            f=f*i;
        }
        sum=sum+f;
        t=t/10;
    }
    if(sum==n)
    {
        printf("%d is Strong Number\n",n);
    }
    else
    {
        printf("%d is Not Strong Number\n",n);
    }
}
34. Write a program that shows palindrome number or not.
Sample input: 145
Output : 145 is not a palindrome number

#include <stdio.h>
int main()
{
    int n,t,r,sum=0;
    printf("Enter Number=");
    scanf("%d",&n);
    t=n;
    while(t>0)
    {
        r=t%10;
        sum=sum*10+r;
        t=t/10;
    }
        if(sum==n)
            printf("%d is Palindrome Number",n);
        else
            printf("%d is not Palindrome Number",n);
}
35. Write a program that shows your given string is palindrome or not.
Sample input: ruet
Output : ruet is not a palindrome string

#include <stdio.h>
#include <string.h>
int main()
{
    char a[100],b[100];
    printf("Enter String=");
    gets(a);
    strcpy(b,a);
    strrev(b);
    if(strcmp(a,b)==0)
    {
        printf("%s is Palindrome",a);
    }
    else
    {
         printf("%s is not Palindrome",a);
    }

}
36. Write a program that convert decimal number to binary number.

#include <stdio.h>
int main()
{
    int i=0,a[100],n,s=0;
    printf("Enter binary number=");
    scanf("%d",&n);
    while(n>0)
    {
        a[i]=n%10*pow(2,i);
        n=n/10;
        s=s+a[i];
        i++;
    }
    printf("%d",s);

}
37. Write a program that convert binary number to decimal number
#include <stdio.h>
int main()
{
    int a[20];
    int dec,j,i=0;
    printf("Enter the decimal number=");
    scanf("%d",&dec);
    while(dec>0)
    {
        a[i]=dec%2;
        i++;
        dec=dec/2;


    }
    for(j=i-1;j>=0;j--)
    printf("%d",a[j]);

}
38. Write a program that convert decimal number to hexadecimal number.
#include <stdio.h>
int main()
{
    int n,i=0,j,a[100];
    printf("Enter Number=");
    scanf("%d",&n);
    while(n!=0)
    {
        a[i]=n%16;
        if(a[i]==10)
        {
            a[i]='A';
        }
        else if(a[i]==11)
        {
            a[i]='B';
        }
        else if(a[i]==12)
        {
            a[i]='C';
        }
        else if(a[i]==13)
        {
            a[i]='D';
        }
        else if(a[i]==14)
        {
            a[i]='E';
        }
        else if(a[i]==15)
        {
            a[i]='F';
        }
        n=n/16;
        i++;

    }
    for(j=i-1;j>=0;j--)
    {
        if(a[j]>=65 && a[j]<=70)
            printf("%c",a[j]);
        else
        {
        printf("%d",a[j]);
        }
    }
}
39. Write a program that convert decimal number to octal number.
#include <stdio.h>
int main()
{
    int a[100],i=0,j,n;
    printf("Enter Decimal Number=");
    scanf("%d",&n);
    while(n!=0)
    {
        a[i]=n%8;
        n=n/8;
        i++;
    }
    printf("Octal Number is=");
    for(j=i-1;j>=0;j--)
    {
        printf("%d",a[j]);
    }
}

40. Write a program that find out the area of any triangle.
#include <stdio.h>
int main()
{
    int h,b,a;
    printf("Enter Height And Base=");
    scanf("%d%d",&h,&b);
    a=0.5*b*h;
    printf("Area OF Triangle Is=%d",a);
}
41. Write a program that find out the volume and surface area of any cylinder.
#include <stdio.h>
#define PI 3.141592654
int main()
{
    double r,h,v,a;
    printf("Enter Radius And Height=");
    scanf("%lf%lf",&r,&h);
    v=PI*r*r*h;
    a=2*PI*r*r+2*PI*r*h;
    printf("Volume Is=%0.3lf\n",v);
    printf("Surface Area Is=%0.3lf",a);

}
42. Write a program that can take any character as a input and show ASCII value
of that character.
#include <stdio.h>
int main()
{
    char a,v;
    printf("Enter Character=");
    scanf("%c",&a);
    v=toascii(a);

    printf("ASCII Value Is=%d",v);
}
43. Write a program that shows all perfect number of a given range.
#include <stdio.h>
int main()
{
    int l,u,i,j;
    printf("Enter Lower Limit And Upper Limit=");
    scanf("%d%d",&l,&u);
    for(i=l;i<=u;i++)
    {
        int sum=0,check=0;
        for(j=1;j<i;j++)
        {
            if(i%j==0)
            {
                sum=sum+j;
            }
        }
        if(sum==i)
        {
            check=1;
        }

        if(check==1)
        {
            printf("%d\n",i);
        }
    }
}
44. Write a program that shows all armstrong number of a given range.
#include <stdio.h>
int main()
{
    int l,u,i,t,r,f=0;
    printf("Enter Range=");
    scanf("%d%d",&l,&u);
    for(i=l;i<=u;i++)
    {
        t=i;
        int sum=0;
        while(t!=0)
        {
            r=t%10;
            sum=sum+r*r*r;
            t=t/10;
        }
        if(sum==i)
        {
            printf("%d\n",i);
            f=1;
        }
    }
    if(f!=1)
    {
        printf("Not Found\n");
    }
}
45. Write a program that take 10 data from user and find minimum value.
Sample input: 10 18 1 29 5 30 4 12 10 98
Output : 1
#include <stdio.h>
int main()
{
    int a[100],i,min,n;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    min=a[0];
    for(i=0;i<n;i++)
    {
        if(a[i]<=min)
        {
            min=a[i];
        }
    }
    printf("Minimum is=%d",min);
}
46. Write a program that take 10 data from user and find maximum value.
Sample input: 10 18 1 29 5 30 4 12 10 98
Output : 98
#include <stdio.h>
int main()
{
    int a[100],max=0,n,i;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=0;i<n;i++)
    {
        if(a[i]>max)
        {
            max=a[i];
        }
    }
    printf("Maximum is=%d",max);
}
47. Write a program to sort the data in ascending order
Sample input: 10 18 1 29 5 30 4 12 11 98
Output : 1 4 5 10 11 12 18 29 30 98
#include <stdio.h>
int main()
{
    int a[100],i,j,n,t;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=1;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            if(a[i]>a[j])
            {
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        }
    }
    printf(" Ascending Sorted Array Is=");
    for(i=1;i<=n;i++)
    {
        printf(" %d",a[i]);
    }
    return 0;
}
48. Write a program to sort the data in descending order
Sample input: 10 18 1 29 5 30 4 12 11 98
Output : 98 30 29 18 12 11 10 5 4 1
#include <stdio.h>
int main()
{
    int a[100],i,j,n,t;
    printf("Enter Number=");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=1;i<=n;i++)
    {
        for(j=i+1;j<=n;j++)
        {
            if(a[i]<a[j])
            {
                t=a[i];
                a[i]=a[j];
                a[j]=t;
            }
        }
    }
    printf(" Descending Sorted Array Is=");
    for(i=1;i<=n;i++)
    {
        printf(" %d",a[i]);
    }
    return 0;
}

51. Write a program to add two integer numbers by using function.
#include <stdio.h>
int sum(int a,int b);
int main()
{
    int c,d;
    printf("Enter Two Number=");
    scanf("%d%d",&c,&d);
    sum(c,d);
}
int sum(int a,int b)
{
    int s=a+b;
    printf("Summation Is=%d",s);
}
52. Write a program to find a prime number by using function.
#include <stdio.h>
int prime(int p);
int main()
{
    int a;
    printf("Enter Prime=");
    scanf("%d",&a);
    prime(a);
}
int prime(int p)
{
    int check=1,i;
    for(i=2;i<p;i++)
    {
        if(i%2==0)
        {
            check=0;
            break;
        }
    }
    if(check==1)
    {
        printf("%d is Prime",p);
    }
    else
    {
        printf("%d is not Prime",p);
    }
}
54. Write a program to find a perfect number by using function.
#include <stdio.h>
int perfect(int n);
int main()
{
    int p;
    printf("Enter Number=");
    scanf("%d",&p);
    perfect(p);
}
int perfect(int n)
{
    int i,sum=0;
    for(i=1;i<n;i++)
    {
        if(n%i==0)
        {
            sum=sum+i;
        }
    }
    if(sum==n)
    {
        printf("%d is Perfect Number\n",n);
    }
    else
    {
        printf("%d is Not Perfect Number\n",n);
    }
    return 0;
}
55. Write a program to find factorial of a number by using function.
#include <stdio.h>
int fact(int a);
int main()
{
    int n;
    printf("Enter Number=");
    scanf("%d",&n);
    fact(n);
}
int fact(int a)
{
    int f=1,i;
    for(i=a;i>=1;i--)
    {
        f=f*i;
    }
    printf("Factorial is=%d",f);
}

Download Coding Interview Book and Get More Tutorials for Coding and Interview Solution: Click Here

Download System Design Interview Book and Get More Tutorials and Interview Solution: Click Here

Do you need more Guidance or Help? Then Book 1:1 Quick Call with Me: Click Here

Share on Google Plus

About Ashadullah Shawon

I am Ashadullah Shawon. I am a Software Engineer. I studied Computer Science and Engineering (CSE) at RUET. I Like To Share Knowledge. Learn More: Click Here
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment