C Programming Questions With Their Solutions

C Programming Questions With Their Solutions



Basic

1. Write a C program to perform input/output of all basic data types.
// 1. Write a C program to perform input/output of all basic data types. #include<stdio.h> int main() { int a; float b; char c; double d; printf("Enter character : "); scanf("%c",&c); printf("Enter integer : "); scanf("%d",&a); printf("Enter float : "); scanf("%f",&b); printf("Enter double : "); scanf("%lf",&d); printf("Character : %c\nInteger : %d\nFloat : %f\nDouble : %lf",c,a,b,d); }

2. Write a C program to enter two numbers and find their sum.
// 2. Write a C program to enter two numbers and find their sum. #include<stdio.h> int main() { float a,b,sum; printf("Enter value of A : "); scanf("%f",&a); printf("Enter value of B : "); scanf("%f",&b); sum = a + b; printf("Sum of A and B : %f",sum); }

3. Write a program to enter two numbers and perform all arithmetic operations.
// 3. Write a program to enter two numbers and perform all arithmetic operations. #include<stdio.h> int main() { int a,b; printf("Enter value of A : "); scanf("%d",&a); printf("Enter value of B : "); scanf("%d",&b); printf("Sum : %d\nSubtraction : %d\nMultiply : %d\nDivision : %d\nRemainder : %d",a+b,a-b,a*b,a/b,a%b); }

4. Write a C program to enter length and breadth of a rectangle and find its perimeter.
// 4. Write a C program to enter length and breadth of a rectangle and find its perimeter. #include<stdio.h> int main() { float length, breadth, perimeter; printf("Enter Breadth : "); scanf("%f",&breadth); printf("Enter length : "); scanf("%f",&length); perimeter = 2*(length+breadth); printf("Rectangle perimeter : %f",perimeter); }

5. Write a C program to enter length and breadth of a rectangle and find its area.
// 5. Write a C program to enter length and breadth of a rectangle and find its area. #include<stdio.h> int main() { float length, breadth, area; printf("Enter length : "); scanf("%f",&length); printf("Enter breadth : "); scanf("%f",&breadth); area = length*breadth; printf("Rectangle area : %f",area); }

6. Write a C program to enter radius of a circle and find its diameter, circumference and area.
// 6. Write a C program to enter radius of a circle and find its diameter, circumference and area. #include<stdio.h> int main() { float radius, diameter, circumference, area; printf("Enter circle radius : "); scanf("%f",&radius); diameter = 2*radius; circumference = 2*3.14*radius; area = 3.14*radius*radius; printf("Diameter : %f\nCircumference : %f\nArea : %f",diameter,circumference,area); }

7. Write a C program to enter length in centimeter and convert it into meter and kilometer.
// 7. Write a C program to enter length in centimeter and convert it into meter and kilometer. #include<stdio.h> int main() { float length, meter, kilometer; printf("Enter the length : "); scanf("%f",&length); meter = length/100; kilometer = meter/1000; printf("Length in meter : %f\nLength in kilometer : %f",meter,kilometer); }

8. Write a C program to enter temperature in celsius and convert it into fahrenheit.
// 8. Write a C program to enter temperature in celsius and convert it into fahrenheit. #include<stdio.h> int main() { float celsius, fahrenheit; printf("Enter temperature in celsius : "); scanf("%f",&celsius); fahrenheit = celsius*9/5 + 32; printf("Temperature in fahrenheit : %f",fahrenheit); }

9. Write a C program to enter temperature in fahrenheit and convert it into celsius.
// 9. Write a C program to enter temperature in fahrenheit and convert it into celsius. #include<stdio.h> int main() { float celsius, fahrenheit; printf("Enter temperature in fahrenheit : "); scanf("%f",&fahrenheit); celsius = (fahrenheit - 32)*5/9; printf("Temperature in celsius : %f",celsius); }

10. Write a C program to convert days into years, weeks and days.
// 10. Write a C program to convert days into years, weeks and days. #include<stdio.h> int main() { int days; printf("Enter days : "); scanf("%d",&days); printf("\n%d Years and %d Days\n\tOR\n%d Weeks and %d Days",days/365,days%365,days/7,days%7); }

11. Write a C program to find power of any number x^y.
// 11. Write a C program to find power of any number x^y. #include<stdio.h> #include<math.h> int main() { int x,y,result; printf("Enter value of X : "); scanf("%d",&x); printf("Enter value of Y : "); scanf("%d",&y); result = pow(x,y); printf("Result X^Y : %d",result); }

12. Write a C program to enter any number and calculate its square root.
// 12. Write a C program to enter any number and calculate its square root. #include<stdio.h> #include<math.h> int main() { float a,result; printf("Enter any number : "); scanf("%f",&a); result = sqrt(a); printf("Square root of number : %f",result); }

13. Write a C program to enter two angles of a triangle and find th third angle.
// 13. Write a C program to enter two angles of a triangle and find th third angle. #include<stdio.h> int main() { float angle1, angle2, angle3; printf("Enter value of angle 1 : "); scanf("%f",&angle1); printf("Enter value of angle 2 : "); scanf("%f",&angle2); angle3 = 180 - angle1 - angle2; printf("The angle 3 : %f",angle3); }

14. Write a C program to enter base and height of a triangle and find its area.
// 14. Write a C program to enter base and height of a triangle and find its area. #include<stdio.h> int main() { float base, height, area; printf("Enter rectangle base : "); scanf("%f",&base); printf("Enter rectangle height : "); scanf("%f",&height); area = 0.5*base*height; printf("Area : %f",area); }

15. Write a C program to calculate area of an equilateral triangle.
// 15. Write a program to calculate area of an equilateral triangle. #include<stdio.h> int main() { float a, area; printf("Enter length of equilateral side : "); scanf("%f",&a); area = (a*a*1.732)/4; printf("Area : %f",area); }

16. Write a C program to enter marks of five subjects and calculate total, average and percentage.
// 16. Write a C program to enter marks of five subjects and calculate total, average and percentage. #include<stdio.h> int main() { float hindi, english, math, science, social, total, average, percentage; printf("Enter Hindi marks (out of 100) : "); scanf("%f",&hindi); printf("Enter English marks (out of 100) : "); scanf("%f",&english); printf("Enter Math marks (out of 100) : "); scanf("%f",&math); printf("Enter Science marks (out of 100) : "); scanf("%f",&science); printf("Enter Social marks (out of 100) : "); scanf("%f",&social); total = hindi + english + math + science + social; average = total/5; percentage = total*100/500; printf("\nTotal : %f (out of 500)\nAverage : %f\nPercentage : %f%",total,average,percentage); }

17. Write a C program to enter P, T, R and calculate simple interest.
// 17. Write a C program to enter P, T, R and calculate simple interest. #include<stdio.h> int main() { float p, t, r, interest; printf("Enter value of P : "); scanf("%f",&p); printf("Enter value of T : "); scanf("%f",&t); printf("Enter value of R : "); scanf("%f",&r); interest = p*(1+r*t); printf("Interest : %f",interest); }

18. Write a C program to Find maximum between two numbers.
// 18. Find maximum between two numbers. #include<stdio.h> int main() { int a,b; printf("Enter any two numbers : "); scanf("%d%d",&a,&b); if(a>b){ printf("%d is the maximum number.",a); } else{ printf("%d is the maximum number.",b); } }

Conditional Operator

1. Write a C program to find maximum between two numbers using conditional operator.
#include<stdio.h>
int main()
{
    int a,b;
    printf("Enter any two numbers : ");
    scanf("%d%d",&a,&b);
    (a>b)?printf("%d is greater",a):printf("%d is greater",b);
}

2. Write a C program to find maximum between three numbers using conditional operator.
#include<stdio.h>
int main()
{
    int a,b,c;
    printf("Enter any three numbers : ");
    scanf("%d%d%d",&a,&b,&c);
    (a>b)?(a>c?printf("%d is greatest",a):printf("%d is greatest",c)):(b>c?printf("%d is greatest",b):printf("%d is greatest",c));
}

3. Write a C program to check whether a number is even or odd using conditional operator.
#include<stdio.h>
int main()
{
    int n;
    printf("Enter any number : ");
    scanf("%d",&n);
    (n%2==0)?printf("%d is even",n):printf("%d is odd",n);
}

4. Write a C program to check whether a year is leap year or not using conditional operator.
#include<stdio.h>
int main()
{
    int year;
    printf("Enter any year : ");
    scanf("%d",&year);
    (year%100==0)?(year%400==0?printf("%d is leap year",year):printf("%d is not leap year",year)):(year%4==0?printf("%d is leap year",year):printf("%d is not leap year",year));
}

5. Write a C program to check whether character is an alphabet or not using conditional operator.
#include<stdio.h>
int main()
{
    char c;
    printf("Enter any character : ");
    scanf("%c",&c);
    (c>='a' && c<='z')?printf("%c is alphabet",c):printf("%c is not alphabet",c);
}

If else

1. Write a C program to find maximum between two numbers.


If else if...else

1.


For loop

1.


While loop

1. Write a C program to print all natural numbers from 1 to N.
#include <stdio.h>
int main()
{
    int n,i=1;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<=n)
    {
        printf("%d ",i);
        i++;
    }
}

2. Write a C program to print all natural numbers in reverse from 1 to N.
#include <stdio.h>
int main()
{
    int n;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(n>0)
    {
        printf("%d ",n);
        n--;
    }
}

3. Write a C program to print all alphabets from a to z.
#include <stdio.h>
int main()
{
    char c='a';
    while(c<='z')
    {
        printf("%c ",c);
        c++;
    }
}

4. Write a C program to print all even numbers between 1 to 100.
#include <stdio.h>
int main()
{
    int i = 1, n = 100;
    while (i <= n)
    {
        if (i % 2 == 0)
        {
            printf("%d ", i);
        }
        i++;
    }
}

5. Write a C program to print all odd numbers between 1 to 100.
#include <stdio.h>
int main()
{
    int i=1,n=100;
    while(i<=n)
    {
        if(i%2==1)
        {
            printf("%d ",i);
        }
        i++;
    }
}

6. Write a C program to find sum of all odd numbers between 1 to N.
#include <stdio.h>
int main()
{
    int i=1,n,sum=0;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<=n)
    {
        if(i%2==1)
        {
            sum=sum+i;
        }
        i++;
    }
    printf("Sum of odd numbers between 1 to %d : %d",n,sum);
}

7. Write a C program to find sum of all even numbers between 1 to n.
#include <stdio.h>
int main()
{
    int i=1,n,sum=0;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<=n)
    {
        if(i%2==0)
        {
            sum=sum+i;
        }
        i++;
    }
    printf("Sum of all even number between 1 to %d : %d",n,sum);
}

8. Write a C program to find sum of all natural numbers between 1 to N.
#include <stdio.h>
int main()
{
    int i=0,n,sum=0;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<=n)
    {
        sum=sum+i;
        i++;
    }
    printf("Sum between 1 to %d : %d",n,sum);
}

9. Write a C program to print multiplication table of any number.
#include <stdio.h>
int main()
{
    int i=1,n;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<=10)
    {
        printf("%d X %d = %d\n",n,i,n*i);
        i++;
    }
}

10. Write a C program to count number of digits in a number.
#include <stdio.h>
int main()
{
    int n,count=0;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(n>0)
    {
        count++;
        n=n/10;
    }
    printf("Digits in given number : %d",count);
}

11. Write a C program to find first and last digit of a number.
#include <stdio.h>
int main()
{
    int n,f,l;
    printf("Enter any number : ");
    scanf("%d",&n);
    l=n%10;
    while(n>9)
    {
        n=n/10;
    }
    f=n;
    printf("First digit : %d\nLast digit  : %d",f,l);
}

12. Write a C program to find sum of first and last digit of a number.
#include <stdio.h>
int main()
{
    int n,f,l;
    printf("Enter any number : ");
    scanf("%d",&n);
    l=n%10;
    while(n>9)
    {
        n=n/10;
    }
    f=n;
    printf("First digit : %d\nLast digit  : %d\n",f,l);
    printf("First and last digit sum : %d",f+l);
}

13. Write a C program to swap first and last digits of a number.
#include <stdio.h>
int main()
{
    int n,f,l,tmp,multiple=1;
    printf("Enter any number : ");
    scanf("%d",&n);
    tmp=n;
    l=n%10;
    while(tmp>9)
    {
        tmp=tmp/10;
        multiple=multiple*10;
    }
    f=tmp%10;
    n=n-l;
    n=n+f;
    n=n-(f*multiple);
    n=n+(l*multiple);
    printf("After swapping : %d",n);
}

14. Write a C program to calculate sum of digits of a number.
#include <stdio.h>
int main()
{
    int n,sum=0;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(n>0)
    {
        sum=sum+(n%10);
        n=n/10;
    }
    printf("Sum of digits of a number : %d",sum);
}

15. Write a C program to calculat product of digit of a number.
#include <stdio.h>
int main()
{
    int n,product=1;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(n>0)
    {
        product=product*(n%10);
        n=n/10;
    }
    printf("Product of digits of a number : %d",product);
}

16. Write a C program to enter a number and print its reverse.
#include <stdio.h>
int main()
{
    int n,res=0;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(n>0)
    {
        res=(res*10)+n%10;
        n=n/10;
    }
    printf("Reverse of number : %d",res);
}

17. Write a C program to check whether a number is palindrome or not.
#include <stdio.h>
int main()
{
    int n,tmp,res=0;
    printf("Enter any number : ");
    scanf("%d",&n);
    tmp=n;
    while(tmp>0)
    {
        res=(res*10)+tmp%10;
        tmp=tmp/10;
    }
    if(res==n)
    {
        printf("PALINDROME");
    }
    else
    {
        printf("NOT PALINDROME");
    }
}

18. Write a C program to find frequency of each digit in a given integer.
#include <stdio.h>
int main()
{
    int n,a[10]={0};
    printf("Enter any number : ");
    scanf("%d",&n);
    while(n>0)
    {
        a[n%10]++;
        n=n/10;
    }
    for (n=0;n<10;n++)
    {
        if(a[n]!=0)
        {
            printf("Frequency of %d : %d\n",n,a[n]);
        }
    }
}

19. Write a C program to enter a number and print it in words.
#include <stdio.h>
int main()
{
    int i=0,n,tmp[20]={0};
    char num[10][10]={"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
    printf("Enter any number : ");
    scanf("%d",&n);
    while(n>0){
        tmp[i++]=n%10;
        n=n/10;
    }
    while(i>0){
        printf("%s ",num[tmp[--i]]);
    }
}

20. Write a C program to print all ASCII character with their values.
#include <stdio.h>
int main()
{
    int i = 0;
    while (i < 255){
        printf("Character %c : %d\n", i, i);
        i++;
    }
}

21. Write a C program to find power of a number.
#include <stdio.h>
int main()
{
    int n,p,res=1;
    printf("Enter any number : ");
    scanf("%d",&n);
    printf("Enter power : ");
    scanf("%d",&p);
    printf("%d the power of %d : ",n,p);
    while(p>0){
        res=res*n;
        p--;
    }
    printf("%d",res);
}

22. Write a C to find all factors of a number.
#include <stdio.h>
int main()
{
    int n,i=1;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<=n){
        if(n%i==0){
            printf("%d, ",i);
        }
        i++;
    }
}

23. Write a C program to calculate factorial of a number.
#include<stdio.h>
int main()
{
    int n,fact=1;
    printf("Enter any number : ");
    scanf("%d",&n);
    while (n>0){
        fact=fact*n;
        n--;
    }
    printf("Factorial is : %d",fact);
}

24. Write a C program to find HCF (GCD) of two numbers.
#include <stdio.h>
int main()
{
    int n1,n2,i=1,hcf;
    printf("Enter first number : ");
    scanf("%d",&n1);
    printf("Enter second number : ");
    scanf("%d",&n2);
    while(i<=n1 && i<=n2){
        if(n1%i==0 && n2%i==0){
            hcf=i;
        }
        i++;
    }
    printf("HCF : %d",hcf);
}

25. Write a C program to find LCM of two numbers.
#include <stdio.h>
int main()
{
    int n1,n2,i=1,hcf;
    printf("Enter first number : ");
    scanf("%d",&n1);
    printf("Enter second number : ");
    scanf("%d",&n2);
    while(i<=n1 && i<=n2){
        if(n1%i==0 && n2%i==0){
            hcf=i;
        }
        i++;
    }
    printf("HCF : %d",(n1*n2)/hcf);
}

26. Write a c program to check whether a number is prime number or not.
#include<stdio.h>
int main()
{
    int n,i=2;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<n/2+1){
        if(n%i==0){
            break;
        }
        i++;
    }
    if(n/2+1==i){
        printf("Given number is Prime number.");
    }
    else{
        printf("Given number is not Prime number.");
    }
}

27. Write a C program to print all Perfect numbers between 1 to N.
#include<stdio.h>
int main()
{
    int n,i=1,j,per;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<=n)
    {
        per=0;
        j=1;
        while(j<i){
            if(i%j==0){
                per=per+j;
            }
            j++;
        }
        if(per==i){
            printf("%d ",i);
        }
        i++;
    }
}

28. Write a C program to find sum of all Prime numbers between 1 to N.
#include<stdio.h>
int main()
{
    int n,i=1,j,per,sum=0;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<=n){
        per=0;
        j=1;
        while(j<i){
            if(i%j==0){
                per=per+j;
            }
            j++;
        }
        if(per==i){
            sum=sum+i;
            printf("%d ",i);
        }
        i++;
    }
    printf("\nSum is : %d",sum);
}

29. Write a c program to find all prime factors of a number.
#include <stdio.h>
int main()
{
    int n,i,j;
    printf("Enter any number : ");
    scanf("%d",&n);

    for(i=1;i<n;i++){
        if(n%i==0){
            j=2;
            while(j<i/2+1){
                if(i%j==0)
                {
                    break;
                }
                j++;
            }
            if(i/2+1==j){
                printf("%d ",i);
            }
        }
    }
}

30. Write a C program to check whether a number is Armstrong number or not.
#include<stdio.h>
#include<math.h>
int main()
{
    int n,arm=0,tmp,len=0,rem,j;
    printf("Enter any number : ");
    scanf("%d",&n);
    tmp=n;
    while (tmp>0){
        len++;
        tmp=tmp/10;
    }
    tmp=n;
    while (tmp>0){
        rem=1;
        j=0;
        while(j<len){
            rem=rem*(tmp%10);
            j++;
        }
        arm=arm+rem;
        tmp=tmp/10;
    }
    if(arm==n){
        printf("Given number is Armstrong number.");
    }
    else{
        printf("Given number is not Armstrong number.");
    }
}

31. Write a C program to print all Armstrong number between 1 to N.
#include<stdio.h>
#include<math.h>
int main()
{
    int n,arm,tmp,len,rem,i=1,j;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<n){
        tmp=i;
        arm=0;
        len=0;
        while (tmp>0){
            len++;
            tmp=tmp/10;
        }
        tmp=i;
        while (tmp>0){
            rem=1;
            j=0;
            while(j<len){
                rem=rem*(tmp%10);
                j++;
            }
            arm=arm+rem;
            tmp=tmp/10;
        }
        if(arm==i){
            printf("%d ",i);
        }

        i++;
    }
}

32. Write a C program to check whether a number is Perfect number or not.
#include<stdio.h>
int main()
{
    int n,i=1,per=0;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<n){
        if(n%i==0){
            per=per+i;
        }
        i++;
    }
    if(per==n){
        printf("Given number is Perfect number.");
    }
    else{
        printf("Given number is not Perfect number.");
    }
}

33. Write a C program to print all Perfect numbers between 1 to N.
#include<stdio.h>
int main()
{
    int n,i=1,j,per;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<=n){
        per=0;
        j=1;
        while(j<i){
            if(i%j==0){
                per=per+j;
            }
            j++;
        }
        if(per==i){
            printf("%d ",i);
        }
        i++;
    }
}

34. Write a C program to check whether a number is Strong number or not.
#include<stdio.h>
int main()
{
    int n,tmp,i,fact,strong=0;
    printf("Enter any number : ");
    scanf("%d",&n);
    tmp=n;
    while (tmp>0){
        i=tmp%10;
        fact=1;
        while(i>0){
            fact=fact*i;
            i--;
        }
        strong=strong+fact;
        tmp=tmp/10;
    }
    if(n==strong){
        printf("Given number is Strong number.");
    }
    else{
        printf("Given number is not Strong number.");
    }
}

35. Write a C program to print all Strong numbers between 1 to N.
#include<stdio.h>
int main()
{
    int n,tmp,i=1,j,fact,strong;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(i<=n){
        strong=0;
        tmp=i;
        while (tmp>0){
            j=tmp%10;
            fact=1;
            while(j>0){
                fact=fact*j;
                j--;
            }
            strong=strong+fact;
            tmp=tmp/10;
        }
        if(i==strong){
            printf("%d ",i);
        }
        i++;
    }
}

36. Write a C program to print Fibonacci series up to n terms.
#include<stdio.h>
int main()
{
    int n,first=0,second=1;
    printf("Enter any number : ");
    scanf("%d",&n);
    while(n>0){
        printf("%d ",second);
        second=first+second;
        first=second-first;
        n--;
    }
}

Do while loop

1.


Switch case

1.


Array

1.


Matrix

1.


String

1.


Function

1.


Recursion

1.


Struct

1.


Computer Network

1. Write a C program to get the MAC or physical address of the system using Address Resolution Protocol in C.
#include <sys/types.h> #include <sys/socket.h> #include <net/if_arp.h> #include <sys/ioctl.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <math.h> #include <complex.h> #include <arpa/inet.h> #include <netinet/in.h> #inlude < netinet / if_ether.h> #include <net/ethernet.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct sockaddr_in sin = {0}; struct arpreq myarp = {{0}}; unsigned char *ptr; int sd; sin.sin_family = AF_INET; if (inet_aton(argv[1], &sin.sin_addr) == 0) { printf("IP address Entered '%s' is not valid\n", argv[1]); exit(0); } memcpy(&myarp.arp_pa, &sin, sizeof(myarp.arp_pa)); strcpy(myarp.arp_dev, "echo"); sd = socket(AF_INET, SOCK_DGRAM, 0); if (ioctl(sd, SIOCGARP, &myarp) == 1) { printf("No Entry in ATP cache for '%s'\n", arg[1]); exit(0); } ptr = &myarp.arp_pa.sa_data[0]; printf("\nMAC Address for '%s' : ", argv[1]); printf("%X:%X:%X:%X:%X:%X\n", *ptr, *(ptr + 1), *(ptr + 2), *(ptr + 3), *(ptr + 4), *(ptr + 5)); printf("\n\t\t\t\t%X:%X:%X:%X:%X:%X\n", myarp.arp_na.sa_data[0], myarp.arp_na.sa_data[1], myarp.arp_na.sa_data[2], myarp.arp_na.sa_data[3], myarp.arp_na.sa_data[4], myarp.arp_na.sa_data[5]); return 0; }

Post a Comment

If you have any doubt, let me know.

Previous Post Next Post