26 Programs With Solution
26 Programs With Solution
1. A program that will either calculate area of a rectangle, area of a square or circle,
depending on the user’s choice.
Solution
The problem is determination of area of rectangular, square and circle
For a rectangular length and width will be required
For a square length or width may be required
For a circle radius will be required
i. By using if
#include<stdio.h>
int main (void)
{
int choice, l, w, r, area; /* Here variables are declared, you can choose any name*/
printf(“ \t A PROGRAM FOR CALCULATING AREA \n\n\n”);
printf(“Please Enter your selection eg 2 \n\n”);
printf(“ 1:Rectangular\n 2:Square\n 3:Circle\n”);
scanf(“%d”,&choice);
system(“cls”); /* This is not necessary it just clear screen, you can omit it*/
if(choice==1)
{
printf(“Rectangular\n\n Enter the Length\n”);
scanf(“%d”,&l);
printf(“Enter the width \n”);
scanf(“%d”,&w);
area=l*w;
printf(“The area is %d\n”,area);
}
if(choice==2)
{
printf(“Square\n\n Enter the Length or width\n”);
scanf(“%d”,&w);
area=w*w;
printf(“The area is %d\n”,area);
}
if(choice==3)
{
Page 1 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
#include<stdio.h>
int main (void)
{
int choice, l, w, r, area; /* Here variables are declared, you can choose any name*/
printf(“ \t A PROGRAM FOR CALCULATING AREA \n\n\n”);
printf(“Please Enter your selection eg 2 \n\n”);
printf(“ 1:Rectangular\n 2:Square\n 3:Circle\n”);
scanf(“%d”,&choice);
system(“cls”); /* This is not necessary it just clear screen, you can omit it*/
if(choice==1)
{
printf(“Rectangular\n\n Enter the Length\n”);
scanf(“%d”,&l);
printf(“Enter the width %d\n”);
scanf(“%d”,&w);
area=l*w;
printf(“The area is %d\n”,area);
}
else if(choice==2)
{
printf(“Square\n\n Enter the Length or width\n”);
scanf(“%d”,&w);
area=w*w;
printf(“The area is %d\n”,area);
}
else if(choice==3)
{
Page 2 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
else
{
printf(“\n INVALID SELECTION\n TRY AGAIN LATER\n”);
}
return 0;
}
}
#include<stdio.h>
int main (void)
{
int choice, l, w, r, area; /* Here variables are declared, you can choose any name*/
printf(“ \t A PROGRAM FOR CALCULATING AREA \n\n\n”);
printf(“Please Enter your selection eg 2 \n\n”);
printf(“ 1:Rectangular\n 2:Square\n 3:Circle\n”);
scanf(“%d”,&choice);
system(“cls”); /* This is not necessary it just clear screen, you can omit it*/
switch(choice)
{
case 1:
Page 3 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
case 2:
printf(“Square\n\n Enter the Length or width\n”);
scanf(“%d”,&w);
area=w*w;
printf(“The area is %d\n”,area);
break;
case 3:
printf(“Circle\n Enter the radius\n”);
scanf(“%d”,&r);
area=3.14*r*r;
printf(“The area is%d\n”,area);
break;
default:
printf(“\n INVALID SELECTION\n TRY AGAIN LATER\n”);
break;
}
return 0;
}
Page 4 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
2. A simple calulator
#include<stdio.h>
int main ()
{
int choice,a,b;
printf(" A SIMPLE CALCULATOR \n By Sylvester Kiluswa\n\n");
printf("Enter your selection\n\n");
printf(" 1: Addition\n 2: Minus\n 3: Multiplication\n 4: Division\n 5: Reminder\n\n");
scanf("%d",&choice);
system("cls");
{
if(choice==1)
{
printf("Adition\nPlease enter first number \n");
scanf("%d",&a);
printf("Please enter the second number\n");
scanf("%d",&b);
printf("The result is %d\n",a+b);
}
else if (choice==2)
{
printf("Minus\nPlease enter first number\n");
scanf("%d",&a);
printf("Please enter the second number\n");
scanf("%d",&b);
printf("The result is %d\n",a-b);
}
else if (choice==3)
{
printf("Multiplication\nPlease enter first number\n");
scanf("%d",a);
printf("Please enter the second number\n");
scanf("%d",&b);
printf("The result is %d\n",a*b);
}
else if (choice==4)
{
printf("Division\nPlease enter first number\n");
Page 5 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
scanf("%d",&a);
printf("Please enter the second number\n");
scanf("%d",&b);
printf("The result is %d\n",a/b);
}
else if (choice==5)
{
printf("Reminder\nPlease enter first number\n");
scanf("%d",&a);
printf("Please enter the second number\n");
scanf("%d",&b);
printf("The result for remainder is %d\n",a%b);
}
else
printf("\n\n\n\n\t\tINVALID CHOISE, PLEASE ENTER CORRECT
SELECTION\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j,k,n;
j=k;
k=i+j;
}
while(k<n);
return 0;
}
Output
Page 7 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
6. A program to count backwards from 20-1 (using while loop) and displaying MUST
each time
Page 8 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
#include<stdio.h>
int main ()
{
int count;
count=21;
while(count>1)
{
printf("%d\tMUST\n",count=count-1);
}
system("pause");
return 0;
}
}
return 0;
}
#include <stdio.h>
int main()
{
int i,j,k;
i=8;
while( i<29)
{
k=0;
j=2;
while( j<=i/2)
{
if(i%j==0)
{
k=1;
break;
}
++j;
}
if(k==0)
printf("%d\n",i);
++i;
}
return 0;
}
Page 10 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
Page 11 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
#include<stdio.h>
int main (void)
{
int num=20; /* I have initialize by 20*/
if(num==20)
{
printf("\nHello, I love C programming\n");
}
return 0;
Page 12 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
Output
Page 13 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
10. It accepts two integer inputs and calculate quotient and reminder
#include<stdio.h>
int main (void)
{
int devider;
int devisor;
printf("Enter the first number\n\t\t");
scanf("%d",&devider);
printf("Enter the second number\n\t\t");
scanf("%d",&devisor);
printf("\n\tThe quotient is %d\n",devider/devisor);
Page 14 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
Page 15 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
#include <stdio.h>
#include <conio.h>
main()
{
clrscr();
printf(“HELLO WELCOME TO JoPaSy”);
getch();
}
13. Program To Read Two Numbers And Print The Sum Of Given Two Numbers.
#include <stdio.h>
#include <conio.h>
main()
{
int a,b, sum;
printf (“ENTER VALUE FOR A ; “);
scanf (“%d”,&a);
printf(“ENTER VALUE FOR B ;”);
scanf(“%d”,&b);
Page 16 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
sum=a+b;
printf(“Sum Of Given Two Numbers are %d”, sum);
getch();
}
14. Program To Accept Student Roll No, Marks in 3 Subjects and Calculate Total, Average
and Print it.
#include <stdio.h>
#include <conio.h>
main()
{
int r,b,c,d, tot, avg;
printf ("ENTER STUDENT RNO ; ");
scanf ("%d",&r);
printf("ENTER FIRST SUBJECT MARKS ;");
scanf("%d",&b);
printf("ENTER SECOND SUBJECT MARKS;");
scanf("%d",&c);
printf("ENTER THIRD SUBJECT MARKS ;");
scanf("%d",&d); tot=b+c+d; avg=tot/3;
printf("\n\n\t\t JoPaSy COMPUTERS \n\n");
printf("\t STUDENT RNO ; %d ",r);
printf("\t FIRST SUBJECT MARKS ;%d ",b); printf("\t SECOND SUBJECT MARKS ;%d
",c); printf("\t THIRD SUBJECT MARKS ;%d ",d); printf("\t AVERAGE MARKS ; %d", avg);
getch();
}
15. Program To Read Three Numbers And Print The Biggest Of Given Three Numbers
#include <stdio.h>
#include <conio.h>
main( )
{
int a,b,c,big=0;
printf(“ENTER VALUE FOR A:”);
scanf(“%d”,&a);
printf(“ENTER VALUE FOR B:”);
scanf(“%d”,&b);
Page 17 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
16. Program To Read A Number And Find Whether The Given Number Is Even Or Odd.
#include <stdio.h>
#include <conio.h>
main()
{
int n,r;
printf(“ENTER A NUMBER ;”);
scanf(“%d”, &n);
r=n%2;
if(r= = 0)
printf(“the above given number is even number”);
else
printf(“the above given number is odd number”);
getch();
}
17. Program to accept a number and check the given number is Armstrong or not.
#include <stdio.h>
#include <conio.h>
main( )
{
int n, a, b, c, d;
printf (“ Enter a Three Digit Number: “);
scanf (“%d”, &n);
a=n/100;
Page 18 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
b=((n/10)%10);
c=n%10;
d=a*a*a*+b*b*b +c*c*c;
if (n= =d)
printf (“The Given Number is Armstrong number”);
else
printf (“The Given Number is Not Armstrong number”);
getch( );
}
18. Program to print ODD numbers from 1 to 10
#include <stdio.h>
#include <conio.h>
main( )
{
int i;
for (i=1; i<=10; i+=2)
printf(“%d\n”,i);
getch();
}
#include <stdio.h>
#include <conio.h>
main( )
{
int i;
for (i=10; i>=1; i--)
printf(“%d\n”,i);
getch( );
}
20. Program to find biggest of two no by using ternary numbers
#include <stdio.h>
#include <conio.h>
main( )
{
int a,b,big;
printf(“enter value a”);
Page 19 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
scanf(“%d”,&a);
printf(“enter the value of b”); scanf(“%d”,&b); big=(a>b)?a:b;
printf(“biggest of the given numbers IS %d”,big);
getch();
}
21. Program to accept any single digit number and print it in words.
#include <stdio.h>
#include <conio.h>
main( )
{
int n;
printf(“enter a number :”); scanf(“%d “,&n); switch(n)
{
case 0: printf(“ZERO”);
break;
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(“please enter the number between 0 and 9”);
}
getch( );
}
22. Program to print “PASCAL TRIANGLE”.
#include<stdio.h>
#include<conio.h>
main()
{
int n,p=1,q,num,sp;
Page 20 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
23. Program to read date,month, year and print the next day’s date,month,year.
#include <stdio.h>
#include <conio.h>
main( )
{
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int d,m,y,nd,nm,ny,ndays;
printf(“enter the date,month,year”); scanf(“%d%d%d”,&d,&m,&y); ndays=month[m-1];
if(m==2)
{
if(y%100==0)
{ if(y%400==0) ndays=29;
} else if(y%4==0) ndays=29;
}
nd=nd+1; nm=m; ny= y;
if(nd>ndays)
{ nd=1; nm++;
}
if(nm>12)
{ nm=1; ny++;
}
printf(“Given date is %d:%d:%d\n”,d,m,y); printf(“next days date is %d:%d:%d”,nd,nm,ny);
getch( );
}
27. Program to accept a number and print mathematical table of the given no.
#include <stdio.h>
#include <conio.h>
main( )
{
int i,t;
printf(“which table u want:”);
scanf(“%d”,&t);
for (i=1; i<=10; i++)
printf(“\n%d*%d=%d”,t,i,i*t);
getch( );
}
Page 22 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
#include<stdio.h>
int main()
{
int i,sum=0;
for(i=5;i<=10;i++)
{
sum=sum+i;
}
printf("The sum is %d\n",sum);
return 0;
}
Page 23 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]
Page 24 of 24
http://www.jopasy.com JoPaSy Co.Ltd