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.

2. Write a C program to enter two numbers and find their sum.

3. Write a program to enter two numbers and perform all arithmetic operations.

4. Write a C program to enter length and breadth of a rectangle and find its perimeter.

5. Write a C program to enter length and breadth of a rectangle and find its area.

6. Write a C program to enter radius of a circle and find its diameter, circumference and area.

7. Write a C program to enter length in centimeter and convert it into meter and kilometer.

8. Write a C program to enter temperature in celsius and convert it into fahrenheit.

9. Write a C program to enter temperature in fahrenheit and convert it into celsius.

10. Write a C program to convert days into years, weeks and days.

11. Write a C program to find power of any number x^y.

12. Write a C program to enter any number and calculate its square root.

13. Write a C program to enter two angles of a triangle and find th third angle.

14. Write a C program to enter base and height of a triangle and find its area.

15. Write a C program to calculate area of an equilateral triangle.

16. Write a C program to enter marks of five subjects and calculate total, average and percentage.

17. Write a C program to enter P, T, R and calculate simple interest.

18. Write a C program to Find maximum between two numbers.

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.

Post a Comment

If you have any doubt, let me know.

Previous Post Next Post