0% found this document useful (0 votes)
11 views

26 Programs With Solution

Uploaded by

eddydeleibniz01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

26 Programs With Solution

Uploaded by

eddydeleibniz01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

[2011]

SAMPLE C PROGRAMS AND ANSWERS

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]

printf(“Circle\n\n Enter the radius\n”);


scanf(“%d”,&r);
area=3.14*r*r;
printf(“The area is%d\n”,area);
}
return 0;
}

ii. By using if—else--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 %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]

printf(“Circle\n Enter the radius\n”);


scanf(“%d”,&r);
area=3.14*r*r;
printf(“The area is%d\n”,area);
}

else
{
printf(“\n INVALID SELECTION\n TRY AGAIN LATER\n”);
}
return 0;
}
}

iii. By using SWITCH CASE

#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]

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);
break;

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;
}

3. FIBONUCI SERIES (1,1,2,3,4,5…n)

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,j,k,n;

printf("enter number of terms\n");


scanf("%d",&n);
i=0;
j=0;
k=1;
do
{
printf("%d ,",k);
i=j;
Page 6 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]

j=k;
k=i+j;
}
while(k<n);
return 0;
}

Output

4. A program to find if the year is leap or not


#include<stdio.h>
main ()
{
int year;
printf("\t Enter the year\n\t ");
scanf("%d",&year);
if(year%4==0)
{
printf("\t %d is a reap year\n",year);
}
else
{
printf("\t %d is not a reap year\n",year);
}
getch();
}

Page 7 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]

5. The first 10 natural numbers and their sum


#include<stdio.h>
main ()
{
int number,sum;
sum=0;
printf("The first 10 natura numbers are\n\n");
for(number=0;number<10;number +=1)
{
printf("%d,",number);
sum=sum+number;
}
printf("\nThe sum is %d",sum);
getch();
}

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;
}

7. (a) Even numbers between 4 and 30


#include <stdio.h>
main(void)
{
int a;
for (a=6;a<30;a=a+2)
{
printf("%d,",a);
Page 9 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]

}
return 0;
}

(b)Prime numbers between 7 qnd 29 using while

#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]

(C) Odd numbers between 1 and 27 using do while


#include <stdio.h>
main()
{
int i=1;
printf("\t Odd numbers are\n\n");
do
{
printf("\t\t%d\n",i=i+2);
}
while(i<25);
return 0;
}

Page 11 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]

8. .Five Syntax errors are :-


•Using undeclared variables
•Misspelled variable and function names
•Missing semicolons
•Unmatched parentheses, square brackets, and curly braces
•Incorrect format in selection and loop statements

9. Display Hello, I love C programming if number is 20

#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]

printf("\tThe reminder is %d\n",devider%devisor);


return 0;
}

Types of Program Errors

Program errors are also referred to as program bugs.


A C program may have one or more of four types of errors:
i.Syntax errors (Compiler errors or Compile-time errors)
ii.Linker Errors
iii.Runtime errors
iv.Logic errors

Page 15 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]

11. A program to calculate simple interest


#include<stdio.h>
#include<conio.h>
main()
{
int amount,rate,time,si;
printf("nEnter Principal Amount : ");
scanf("%d",&amount);
printf("\nEnter Rate of interest : ");
scanf("%d",&rate);
printf("nEnter Period of Time : ");
scanf("%d",&time);
si = (amount * rate * time)/100;
printf("Simple Intrest : %d",si);
getch();
}

12. Program to print text

#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]

print(“ENTER VALUE FOR C:”);


scanf(“%d”,&c);
if (a>big)
big=a ;
if(b>big)
big=b;
if (c>big)
big=c;
printf (“BIGGEST OF ABOVE GIVEN THREE NUMBER IS %d”,big)
getch( );
}

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();
}

19. Program to print natural numbers from 1 to 10 in Reverse

#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]

printf(“enter the number of rows”); scanf(“%d”,&n); for(p=0;p<=n;p++)


{
for(sp=1;sp<=40-(3*p);sp++)
printf(“ “);
for(q=0;q<n;q++)
{ if((q==q)||(q==0)) num=1;
else
num=num*((q-q)+1)/q; printf(“%2d”,num); printf(“\n”);
}}
getch( );
}

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( );
}

24. Program to print prime numbers between 1 to 100


#include <stdio.h>
#include <conio.h>
main( )
{
Page 21 of 24
http://www.jopasy.com JoPaSy Co.Ltd
[2011]

int n, i, check; clrscr(); for(i=1;i<=100;i++)


{
check=1; for(n=2;n<=i/2;n++) if(i%n= =0)
{ check=0; break;
}
if(check= =1)
printf(“\n %d is a prime”,i);
else
printf(“\n %d is not a prime”,i);
}
getch( );
}
25. Program to accept a number and find factorial of given number
#include <stdio.h>
#include <conio.h>
main( )
{
int n,f;
printf("enter a number:");
scanf("%d",&n);
f= fact(n);
printf("factorial value is %d",f);
getch();
}
int fact(int n)
{
int i, fa=1; for(i=n;i>=1;i--) fa=fa*i;
return fa;
}

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]

28. Program to print 1 to 10 mathematical tables.


#include <stdio.h>
#include <conio.h>
main( )
{
int i,j;
for (i=1; i<=10; i++) for(j=1;j<=10;j++)
printf(“\n%d*%d=%d”,i,j,i*j);
getch( );
}

26. A program that calculate the sum of the series from 5 – 10


a) Using for loop

#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]

b) Using while loop


#include<stdio.h>
main ()
{
int i=6,sum=0;
while(i<=10)
{
i +=1;
sum=sum+i;
}
printf("the sum is %d",sum);
return 0;
}

c) Using do while loop


#include<stdio.h>
main ()
{
int i,sum=0;
i=6;
do
{
i +=1;
sum=sum+i;
}
while(i<=10);
printf("the sum is %d",sum);
return 0;
}

Page 24 of 24
http://www.jopasy.com JoPaSy Co.Ltd

You might also like