Programming in C : 20 Important Questions of Programming in C.

20 Important Questions of  Programming in C.

Question : Write a C Program to find area and circumference of circle.
/*
Write a C Program to find area and circumference of circle.
Formula:
Area = 3.14 * radius * radius
Circumference = 2 * 3.14 * radius
Program
*/

#include <stdio.h>
int main()
{
               int circle_radius;
                float PI_VALUE=3.14, circle_area, circle_circumf;
               //Ask user to enter the radius of circle
               printf("\nEnter radius of circle: ");
               //Storing the user input into variable circle_radius
               scanf("%d",&circle_radius);
               //Calculate and display Area
               circle_area = PI_VALUE * circle_radius * circle_radius;
               printf("\nArea of circle is: %f ",circle_area);
               //Caluclate and display Circumference
               circle_circumf = 2 * PI_VALUE * circle_radius;
               printf("\nCircumference of circle is: %f ",circle_circumf);

               return (0);
}

OUTPUT :
Enter radius of circle: 2
Area of circle is: 12.560000
Circumference of circle is: 12.560000


Question : Write a C Program to find the simple interest.
/*
Write a C Program to find the simple interest.
SI = (principle * time * rate) / 100
*/

#include <stdio.h>
int main()
{
                float principle, time, rate, SI;
               /* Input principle, rate and time */
               printf("Enter principle (amount): ");
               scanf("%f", &principle);
               printf("Enter time: ");
               scanf("%f", &time);
               printf("Enter rate: ");
               scanf("%f", &rate);
               /* Calculate simple interest */
               SI = (principle * time * rate) / 100;
               /* Print the resultant value of SI */
               printf("Simple Interest = %f ", SI);
               return (0);
}

OUTPUT :
Enter principle (amount): 1200
Enter time: 2
Enter rate: 5.4
Simple Interest = 129.600006

Question : Write a C Program to perform basic arithmetic operations which are addition, subtraction, multiplication and division of two numbers. Numbers are assumed to be integers and will be entered by the user.
/*
Write a C Program to perform basic arithmetic operations which are addition, subtraction, multiplication and division of two numbers. Numbers are assumed to be integers and will be entered by the user.

Addition = First Number + Second Number
Subtraction = First Number - Second Number
Multiplication = First Number * Second Number
Division = First Number  / (float) Second Number   
*/

#include <stdio.h>
int main()
{
           
int first, second, add, subtract, multiply;
           
float divide;
           
printf("Enter First integers\n");
           
scanf("%d ", &first);
            printf("Enter Second integers\n");
           
scanf("%d",&second);
            add = first + second;
            subtract = first - second;
            multiply = first * second;
            divide = first / (float)second;   
/*typecasting*/
printf("Sum = %d\n", add);
           
printf("Difference = %d\n", subtract);
           
printf("Multiplication = %d\n", multiply);
           
printf("Division = %.2f\n", divide);
  
return (0);
}

OUTPUT :
Enter First integers
7
Enter Second integers
3
Sum = 10
Difference = 4
Multiplication = 21
Division = 2.33

Question :  Write a C Program to find greatest in 3 numbers.
/*
Write a C Program to find greatest in 3 numbers.
*/
 
#include <stdio.h>
int main()
{
                int num1,num2,num3;
               //Ask user to input any three integer numbers
               printf("\nEnter value of num1, num2 and num3:");
               //Store input values in variables for comparsion
               scanf("%d %d %d",&num1,&num2,&num3);
                if((num1>num2)&&(num1>num3))
               {
                               printf("\n Number1 is greatest");
               }
               else if((num2>num3)&&(num2>num1))
               {     
                               printf("\n Number2 is greatest");
               }
               else
                {
                               printf("\n Number3 is greatest");
               }
               return (0);
}
 
OUTPUT :
Enter value of num1, num2 and num3: 15  200  101
Number2 is greatest

Question :  Write a C Program to find Area of Right Angled Triangle.
/*
Write a C Program to find Area of Right Angled Triangle.
Area = 0.5 * height * width
*/
 
#include <stdio.h>
int main()
{
               float height, width;
               float area;
               printf("Enter height of the given triangle:\n ");
               scanf("%f ", &height);
               printf("Enter width of the given triangle:\n ");
               scanf("%f", &width);
               area = 0.5 * height * width;
               printf("Area of right angled triangle is: %.3f\n", area);
               return (0);
}
 
OUTPUT :
Enter height of the given triangle:
10 
Enter width of the given triangle:
15
Area of right angled triangle is: 75.000

Question :  Write a C Program to find Area of Square.
/*
Write a C Program to find Area of Square.
Area of Square = Square_Side * Square_Side
*/
 
#include <stdio.h>
int main()
{
               int square_side, area;
               printf("Enter the side of square: ");
               scanf("%d", &square_side);
               //calculation of the area
               area = square_side * square_side;
               printf("Area of the square: %d", area);
               return (0);
}
 
OUTPUT :
Enter the side of square: 8
Area of the square:64

Question :  Write a C Program to swap two numbers with and without using third variable.
/*
Write a C Program to swap two numbers with and without using third variable.
*/

#include <stdio.h>
#include<conio.h>
void main()
{
            int a,b;
            clrscr();
            printf("Enter the Values for a & b to Swap: ");
            scanf("%d %d",&a,&b);
            a=a+b;
            b=a-b;
            a=a-b;
            printf("After Swapping the Values of a & b are :%d %d",a,b);
            getch();
}

OUTPUT :
Enter the Values for a & b to Swap: 10 20
After Swapping the Values of a & b are :20 10

Question :  Write a C Program to find Area of Rectangle.
/*
Write a C Program to find Area of Rectangle.
*/

#include <stdio.h>
#include<conio.h>
int main()
{
            int length, breadth, area;
            printf("\nEnter the Length of Rectangle : ");
            scanf("%d", &length);
            printf("\nEnter the Breadth of Rectangle : ");
            scanf("%d", &breadth);
            area = length * breadth;
            printf("\nArea of Rectangle : %d", area);
return (0);
}

OUTPUT :
Enter the Length of Rectangle  : 5
Enter the Breadth of Rectangle : 4
Area of Rectangle : 20

Question :  Write a C Program to calculate sum of  5 subjects and find percentage.
/*
Write a C Program to calculate sum of  5 subjects and find percentage.
*/

#include <stdio.h>
int main()
{
            int s1, s2, s3, s4, s5, sum, total = 500;
float per;
printf("\n Enter marks of 5 subjects : ");
            scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);
            sum = s1 + s2 + s3 + s4 + s5;
            printf("\nSum : %d", sum);
            per = (sum * 100) / total;
            printf("\nPercentage : %f", per);
               return (0);
}

OUTPUT :
Enter marks of 5 subjects : 80 70  90  80  80
Sum : 400
Percentage : 80.00

Question :  Write a Write a C Program to reverse a number.
/*
 Write a Write a C Program to reverse a number.
*/
#include <stdio.h>
int main()
{
            int n, rev = 0, remainder;
            printf("Enter an integer: ");
            scanf("%d", &n);
            while (n != 0)
            {
                        remainder = n % 10;
                        rev = rev * 10 + remainder;
                        n /= 10;
            }
            printf("Reversed number = %d", rev);
return (0);
}

OUTPUT :
Enter an integer: 2345
Reversed number = 5432

Question :  Write a C Program to check whether a number is palindrome or not.
/*
Write a C Program to check whether a number is palindrome or not.
*/

#include <stdio.h>
int main()
{
            int n, reversedN = 0, remainder, originalN;
            printf("Enter an integer: ");
            scanf("%d", &n);
            originalN = n;
            // reversed integer is stored in reversedN
            while (n != 0)
{
                        remainder = n % 10;
                        reversedN = reversedN * 10 + remainder;
                        n /= 10;
            }
            // palindrome if orignalN and reversedN are equal
            if (originalN == reversedN)
            {
                        printf("%d is a palindrome.", originalN);
            }
            else
            {  
                        printf("%d is not a palindrome.", originalN);
            }
return (0);
}
OUTPUT :
Enter an integer: 1001
1001 is a palindrome.

Question :  Write a C Program to check whether a number is prime or not.
/*
Write a C Program to check whether a number is prime or not.
*/

#include <stdio.h>
main()
{
            int n, i, c = 0;
            printf("Enter any number n:");
            scanf("%d", &n);
             /*logic*/ 
            for (i = 1; i <= n; i++)
            {
                        if (n % i == 0)
                        {
                        c++;
                        }
            }
             if (c == 2)
            {
                        printf("n is a Prime number");
            }
            else
            {
                        printf("n is not a Prime number");
            }
return (0);
}

OUTPUT :
Enter any number n: 7
n is Prime number

Question :  Write a C Program to check whether a number is Armstrong or not.

#include <stdio.h>
 int main()   
{   
int n,r,sum=0,temp;   
printf("enter the number=");   
scanf("%d",&n);   
temp=n;   
while(n>0)   
{   
r=n%10;   
sum=sum+(r*r*r);   
n=n/10;   
}    
if(temp==sum)
{  
printf("armstrong  number ");   
}
else   
{
printf("not armstrong number");   
}
return 0; 
} 

OUTPUT :
enter the number=153
armstrong number

Question :   Write a C Program for Fibonacci series.

#include <stdio.h>
int main()   
{   
int n1=0,n2=1,n3,i,number;   
            printf("Enter the number of elements:");   
scanf("%d",&number);   
            printf("\n%d %d",n1,n2);//printing 0 and 1   
            for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed    
            {   
                        n3=n1+n2;   
                        printf(" %d",n3);   
                         n1=n2;   
                         n2=n3;   
 } 
  return 0; 
 }  
OUTPUT :
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

Question :  Write a C Program to find ASCII value of a character entered by user.

#include <stdio.h>
int main()
{ 
            char c;
            printf("Enter a character: ");
            scanf("%c", &c); 
            // %d displays the integer value of a character
            // %c displays the actual character
            printf("ASCII value of %c = %d", c, c);
            return 0;
}

OUTPUT :
Enter a character: G
ASCII value of G = 71

Question :  Write a C Program to find minimum element in array.
/*
Write a C Program to find minimum element in array.
*/

#include <stdio.h>
int main()
{
            int array[100], minimum, size, c, location = 1;
            printf("Enter number of elements in array\n");
            scanf("%d", &size);
            printf("Enter %d integers\n", size);
for (c = 0; c < size; c++)
            scanf("%d", &array[c]);
            minimum = array[0];
            for (c = 1; c < size; c++)
                        {
                                    if (array[c] < minimum)
                                                {
                                                 minimum = array[c];
                                                location = c+1;
                                                }
                         }
  
printf("Minimum element is present at location %d and its value is %d.\n", location, minimum);
    return 0;
}
OUTPUT :
Enter number of elements in array
5
Enter 5 integers
2
5
4
1
6
Minimum element is present at location 4 and its value is 1.

Question :  Write a C Program to check whether a year is leap year or not.
/*
Write a C Program to check whether a year is leap year or not.
*/

#include <stdio.h>
int main()
{
            int year;
            printf("Enter a year: ");
            scanf("%d", &year);
            if (year % 4 == 0)
{
                        if (year % 100 == 0)
{
                                    // the year is a leap year if it is divisible by 400.
                                                if (year % 400 == 0)
                                    {
                                     printf("%d is a leap year.", year);
                                    }
                                     else
{          
                                    printf("%d is not a leap year.", year);
                                    }
                         }
else
{
                                    printf("%d is a leap year.", year);
}
            }
else
{
            printf("%d is not a leap year.", year);
            }
    return 0;
}

OUTPUT :
Enter a year: 1900
1900 is not a leap year.

Question :  Write a C Program to check whether a number is positive or negative.
/*
Write a C Program to check whether a number is positive or negative.
*/

#include <stdio.h>
int main()
{
            double num;
            printf("Enter a number: ");
            scanf("%lf", &num);
            if (num <= 0.0)
{
                        if (num == 0.0)
                        {
                                    printf("You entered 0.");
                        }
else
{
printf("You entered a negative number.");
}
            }
else
{
            printf("You entered a positive number.");
            }
    return 0;
}

OUTPUT :
Enter a number: 12.3
You entered a positive number.

Question :  Write a C Program to find the factorial of a number.
/*
Write a C Program to find the factorial of a number.
*/

#include <stdio.h>
int main()
{
            int n, i;
            unsigned long long fact = 1;
            printf("Enter an integer: ");
            scanf("%d", &n);
            // shows error if the user enters a negative integer
            if (n < 0)
            {
                        printf("Error! Factorial of a negative number doesn't exist.");
            }
            else
{
                        for (i = 1; i <= n; ++i)
{
                         fact *= i;
                        }
             printf("Factorial of %d = %llu", n, fact);
            }
    return 0;
}

OUTPUT :
Enter an integer: 10
Factorial of 10 = 3628800

Question :  Write a C Program to Count Number of Digits in an Integer.
/*
Write a C Program to Count Number of Digits in an Integer.
*/

#include <stdio.h>
int main()
{
            long long num;
             int count = 0;
            /* Input number from user */
            printf("Enter any number: ");
            scanf("%lld", &num);
            /* Run loop till num is greater than 0 */
            while(num != 0)
            {
                         /* Increment digit count */
                        count++;
                        /* Remove last digit of 'num' */
                         num /= 10;
            }
            printf("Total digits: %d", count);
            return 0;
}

OUTPUT :
Enter any number: 45698
Total digits: 5

Comments