C Program Excersise
C Program Excersise
int main()
scanf("%d",&maths);
scanf("%d",&phy);
scanf("%d",&chem);
scanf("%d",&total_pcm);
scanf("%d",&total_pm);
if((maths>=65&&phy>=55&&chem>=50)&&(total_pcm>=190||total_pm>=140))
{
else
return 0;
12. Write a C program to read roll no, name and marks of three subjects and
calculate the total, percentage and division. Go to the editor
Test Data :
Input the Roll Number of the student :784
Input the Name of the Student :James
Input the marks of Physics, Chemistry and Computer Application : 70 80 90
Expected Output :
Roll No : 784
Name of Student : James
Marks in Physics : 70
Marks in Chemistry : 80
Marks in Computer Application : 90
Total Marks = 240
Percentage = 80.00
Division = First
Answer:
#include <stdio.h>
int main()
{
int roll_no, phy, chem, computer, total, division;
char name[10];
float percentage;
printf(" \n Enter the roll no. : ");
scanf("%d" ,&roll_no);
printf("\nTotal = %d \n",total);
percentage = (total)/3;
printf("Percentage (%) = %f \n",percentage);
if(percentage>70.00)
{
printf("Division = Distriction");
}
else if(percentage>60.00&&percentage>70.00)
{
printf("Division = 1st class");
}
else if(percentage>50.00&&percentage>60.00)
{
printf("Division = 2nd class");
}
else if(percentage>35.00&&percentage>50.00)
{
printf("Division = 3rd class");
}
else
{
printf("Division = Failed");
}
return 0;
}
if(Temp < 0)
{
printf("Freezing weather");
}
else if(Temp>0&&Temp<=10.00)
{
printf("Very Cold weather");
}
else if(Temp>10&&Temp<=20.00)
{
printf("Cold weather");
}
else if(Temp>20&&Temp<=30.00)
{
printf("Normal in Temp");
}
else if(Temp>30&&Temp<=40.00)
{
printf("Its Hot");
}
else if(Temp>=40)
{
printf("Its Very Hot");
}
return 0;
}
and
Answer:-
#include <stdio.h>
int main()
{
int anglea,angleb,anglec;
printf("Enter the angles of triangle = ");
scanf("%d,%d,%d",&anglea,&angleb,&anglec);
if(anglea+angleb+anglec==180)
{
if(anglea==angleb&&angleb==anglec)
{
printf("This is an equilateral triangle.");
}
else if (anglea==angleb||anglea==anglec||angleb==anglec)
{
printf("This is an isosceles triangle.");
}
else
{
printf("This is a scalene triangle.");
}
}
else
{
printf("The triangle is not valid.");
}
return 0;
}
16. Write a C program to check whether a character is an alphabet, digit or
special character. Go to the editor
Test Data :
@
Expected Output :
This is a special character.
Click me to see the solution
And
Answer:
#include <stdio.h>
int main()
{
char ch;
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
{
if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||
ch=='U')
{
printf("The alphabet is a vowel.");
}
else
{
printf("The alphabet is a consonant.");
}
}
else if (ch>='0'&&ch<='9')
{
printf("This is a digit.");
}
else
{
printf("This is a special character.");
}
return 0;
Answer:
#include <stdio.h>
int main()
{
int amount1,amount2,PnL;
printf("Enter the amount of purchases : ");
scanf("%d",&amount1);
printf("Enter the amount of Sell : ");
scanf("%d",&amount2);
if(amount2-amount1>=0)
{
PnL=amount2-amount1;
printf("You made profit ₹ : %d ", PnL);
}
else
{
(amount2-amount1<0);
PnL=amount2-amount1;
printf("You made loss ₹ : %d",PnL);
}
return 0;
}
Unit Charge/unit
upto 199 @1.20
If bill exceeds Rs. 400 then a surcharge of 15% will be charged and the minimum
bill should be of Rs. 100/-
Test Data :
1001
James
800
Expected Output :
Customer IDNO :1001
Customer Name :James
unit Consumed :800
Amount Charges @Rs. 2.00 per unit : 1600.00
Surchage Amount : 240.00
Net Amount Paid By the Customer : 1840.00
Answer.
#include <stdio.h>
int main()
int cust_id;
float unit,amount,rate,surcharge;
char name[10];
scanf("%s",&name);
scanf("%f",&unit);
amount=unit*rate;
if (unit<=199)
amount=unit*1.20;
else if (unit>=200&&unit<400)
amount=unit*1.50;
else if (unit>=400&&unit<600)
amount=unit*1.80;
else if (unit>=600)
amount=unit*2.00;
if(unit<=83.33333)
amount=100;
printf("\nMinimum bill amount = 100 ");
if(amount>400)
surcharge=amount*0.15;
amount=amount+surcharge;
return 0;}
Grade Description
E Excellent
V Very Good
G Good
A Average
F Fail
Test Data :
Input the grade :A
Expected Output :
You have chosen : Average
Answer:
#include <stdio.h>
int main()
char grade;
char E,e,V,v,G,g,A,a,F,f;
scanf("%c",&grade);
if(grade=='E' || grade=='e')
return 0;
#include <stdio.h>
int main()
char ch;
scanf("%c",&ch);
switch (ch)
case 'E':
printf("Excellent");
break;
case 'V':
printf("Very Good");
break;
case 'G':
printf("Good");
break;
case 'A':
printf("Average");
break;
case 'F':
printf("Fail");
break;
default:
printf("Charecter is invalid.");
break;
return 0;
21. Write a program in C to read any day number in integer and display day
name in the word. Go to the editor
Test Data :
4
Expected Output :
Thursday
Answer:
#include <stdio.h>
int main()
int digit;
printf("Enter the digit : ");
scanf("%d",&digit);
if(digit==1)
printf("Monday");
else if (digit==2)
printf("Tuesday");
else if (digit==3)
printf("Wednesday");
else if (digit==4)
printf("Thursday");
else if (digit==5)
printf("Friday");
else if (digit==6)
printf("Saturday");
else if (digit==7)
{
printf("Sunday");
else if (digit==0)
else
return 0;
#include <stdio.h>
int main()
int digit;
scanf("%d",&digit);
switch (digit)
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Number is invalid.");
break;
}
return 0;
22. Write a program in C to read any digit, display in the word. Go to the editor
Test Data :
4
Expected Output :
Four
Answer:
#include <stdio.h>
int main()
int digit;
scanf("%d",&digit);
if (digit==0)
printf("Zero");
else if(digit==1)
printf("One");
else if (digit==2)
printf("Two");
}
else if (digit==3)
printf("Three");
else if (digit==4)
printf("Four");
else if (digit==5)
printf("Five");
else if (digit==6)
printf("Six");
else if (digit==7)
printf("Seven");
else if (digit==8)
printf("Eight");
else if (digit==9)
printf("Nine");
}
else
return 0;
#include <stdio.h>
int main()
int digit;
scanf("%d",&digit);
switch (digit)
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
case 3:
printf("Three");
break;
case 4:
printf("Four");
break;
case 5:
printf("Five");
break;
case 6:
printf("Six");
break;
case 7:
printf("Seven");
break;
case 8:
printf("Eight");
break;
case 9:
printf("Nine");
break;
default:
printf("Number is In valid");
break;
return 0;
23. Write a program in C to read any Month Number in integer and display Month
name in the word. Go to the editor
Test Data :
4
Expected Output :
April
Answer:
#include <stdio.h>
int main()
int digit;
scanf("%d",&digit);
if(digit==1)
printf("January");
else if (digit==2)
{
printf("February");
else if (digit==3)
printf("March");
else if (digit==4)
printf("April");
else if (digit==5)
printf("May");
else if (digit==6)
printf("June");
else if (digit==7)
printf("July");
else if (digit==8)
printf("August");
else if (digit==9)
{
printf("September");
else if (digit==10)
printf("October");
else if (digit==11)
printf("November");
else if (digit==12)
printf("December");
else
(digit==0||digit>=13);
return 0;
23. Write a program in C to read any Month Number in integer and display Month
name in the word. Go to the editor
Test Data :
4
Expected Output :
April
Click me to see the solution
And
24. Write a program in C to read any Month Number in integer and display the
number of days for this month. Go to the editor
Test Data :
7
Expected Output :
Month have 31 days
Answer:
#include <stdio.h>
int main()
int digit;
scanf("%d",&digit);
if(digit==1)
else if (digit==2)
printf("February , 28 days");
else if (digit==3)
printf("March , 31 days");
}
else if (digit==4)
printf("April , 30 days");
else if (digit==5)
printf("May, 31 days");
else if (digit==6)
printf("June, 30 days");
else if (digit==7)
printf("July, 31 days");
else if (digit==8)
printf("August, 31 days");
else if (digit==9)
printf("September, 30 days");
else if (digit==10)
printf("October, 31 days");
}
else if (digit==11)
printf("November, 30 days");
else if (digit==12)
printf("December, 31 days");
else
(digit==0||digit>=13);
return 0;
#include <stdio.h>
int main()
int digit;
scanf("%d",&digit);
switch (digit)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
printf("August month have 31 days");
break;
case 9:
break;
case 10:
break;
case 11:
break;
case 12:
break;
default:
printf("Number is invalid.");
break;
return 0;
}
25. Write a program in C which is a Menu-Driven Program to compute the area of
the various geometrical shape. Go to the editor
Test Data :
1
5
Expected Output :
The area is : 78.500000
Answer:
#include <stdio.h>
int main()
int shape;
float r, l, b, h, area;
scanf("%d",&shape);
switch(shape)
case 1:
scanf("%f",&r);
area=3.14*r*r;
break;
case 2:
area=0.5*(b*h);
break;
case 3:
scanf("%f %f",&l,&b);
area=l*b;
break;
default :
scanf("%f",&l);
area=4*l;
return 0;
}
26. Write a program in C which is a Menu-Driven Program to perform a simple
calculation. Go to the editor
Test Data :
10
2
3
Expected Output :
The Multiplication of 10 and 2 is: 20
Answer
#include <stdio.h>
int main()
int num1,num2,option,add,subs,multi,division ;
printf("Select the option from the below \n addition (1) \n substraction (2) \n multiplication
(3) \n division (4) \n select option = ");
scanf("%d", &option);
switch(option)
//Addition
case 1:
scanf("%d",&num1);
scanf("%d",&num2);
add=num1+num2;
break;
//Substraction
case 2:
printf("Enter the 1st digit = ");
scanf("%d",&num1);
scanf("%d",&num2);
subs=num1-num2;
break;
//Multiplication
case 3:
scanf("%d",&num1);
scanf("%d",&num2);
multi=num1*num2;
break;
//Division
case 4 :
scanf("%d",&num1);
scanf("%d",&num2);
division=num1/num2;
break;
default :
printf("This program is only for Addition, Substraction, Multiplication, Division.");
break;
return 0;