C-Programming

Program Solutions

UPTO HERE

  1. Write a program to calculate the volume of a pool. Assume that the pool has length of 25 ft, a width of 10 ft and depth of 6 ft.

Solution:

#iinclude<stdio.h>

#include<conio.h>

void main()

  {

int length, width, depth;

int volume;

length=25;

width=10;

depth=6;

volume=length*width*depth;

pritnf(“\n\nvolume of the pool is %d”,volume);

getch();

}

  1. Write a program to print sum, product and quotient of two numbers entered by the user.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

  {

float a,b;

float sum, quotient, product;

printf(“\nplease enter any two numbers:  “);

scanf(“%f%f”,&a,&b);

sum=a+b;

product=a*b;

quotient=a/b;

printf(“\n\nsum of %f and %f is %f”,a,b,sum);

printf(“\n\nproduct of %f and %f is %f”,a,b,product);

printf(“\n\nquotient of %f and %f is %f”,a,b,quotient);

getch();

}

 

  1. Write a program to compute the area of sphere by reading the radius of the sphere.

Solution:

#inlcude<stdio.h>

#include<conio.h>

#define PI 3.14159

void main()

    {

float radius;

float area;

printf(“enter the radius of the sphere:   “);

scanf(“%f”,&radius);

area=4*PI*radius*radius;

printf(“\n\nArea of sphere having radius %f is %f.”,radius, area);

getch();

}

  1. Write a program that will ask for the name and marks obtained by a student in seven different subjects and display the percentage score of that student.

Solution:

#inlcude<stdio.h>

#include<conio.h>

#inlcude<string.h>

void main()

{

char name[15];

int marks, total;

float percentage;

printf(“enter the name of the student:  “);

gets(name);

total=0;

for(i=1;i<=7;i++)

    {
printf(“\nenter the marks in %dth subject:  “,i);

scanf(“%d”,&marks);

total+=marks;

    }

percentage=(int) total/7;

pritnf(“\n Percentage scored by %s is %f.”,name,percentage);

getch();

}

  1. Write a program to input length and breadth of a room and print its area and perimeter.

Solution:

#inlcude<stdio.h>

#include<conio.h>

void main()

    {

float length, breadth;

flaot area, perimenter;

printf(“\nPlease enter the length and breadth of the room:\t”);

scanf(“%f%f”,&length,&breadth);

area=length*breadth;

perimeter=2*(length+breadth);

printf(“\n\nArea of the room is %f.”,area);

pritnf(“\n\nperimeter of the room is %f.”,perimeter);

getch();

}

 

  1. Write a program to calculate simple interest for the total amount p kept in bank for n years at the rate of r per annum.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

  {

float p,n,r,simple_interest;

printf(“\nenter the total amount kept in bank:\t”);

scanf(“%f”,&p);

printf(“\nenter the time for which amount is kept:\t”);

scanf(“%f”,&n);

printf(“\nenter the annum rate of the interest:\t”);

scanf(“%f”,&r);

simple_interest=p*n*r/100;

printf(“\n\nsimple of the deposit is %f”,simple_interest);

getch();

}

  1. Write a program to calculate compound interest.

Solution

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

  {

float p,n,r,I,temp;

printf(“\nplease enter value of p,n and r:\t”);

scanf(“%f%f%f”,&p,&n,&r);

temp=1+r/100;

I=p*pow(temp,n);

printf(“\n\ncompound interest of amount %f is %f”,p,I);

getch();

}

  1. Write a program to read two numbers and interchange the value and then print them.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

  {

int a,b,c;

printf(“\nenter the value of a:\t”);

scanf(“%d”,&a);

printf(“\nenter the value of b:\t”);

scanf(“%d”,&b);

clrscr();

printf(“\nberfore interchanging:\na=%d\t and b=%d.”,a,b);

c=a;

a=b;

b=a;

printf(“\nafter interchanging:\na=%d\t and b=%d.”,a,b);

getch();

}

  1. Write a program to read n numbers and find out their sum and average.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int number, sum;

int i,n;

float average;

sum=0;

printf(“\nenter the value of n:   “);

scanf(“%d”,&n);

i=1;

while(i<=n)

  {

printf(“\nenter %dth number:   “,i);

scanf(“%d”,&number);

sum+=number;

  }

average=(flaot)sum/number;

printf(“\n\nsum of the entered is %d”,number);

printf(“\nAverage of the number is %f”,average”;

getch();

}

  1. Write a program that count the number of digits present in the integer number entered by the user and sum of the digits.

Solution:

#include<stdio.h>

#include<stdio.h>

void main()

    {

int num,count=1,sum=0,rem;

printf("\nenter a integer number:\t");

scanf("%d",&num);

do

    {

rem=num%10;

num=num/10;

sum+=rem;

if(num>0)count++;

    }

while(num>0);

printf("\n\nno. of digits in the number entered:  %d",count);

printf("\nsum of the digits =%d",sum);

getch();

}

 

  1. Design a program in C to prepare multiplication table according to the value given from the key board using do-while loop.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int n,i;

printf(“enter a number whose multiplication table is to be displayed:\t”);

scanf(“%d”,&n);

clrscr();

printf(“\nmultiplication of %d is:\n\n\n”);

i=1;

do

{

printf(“\n%d*d=*d”,i,n,i*n);

i++;

}while(i<=10);

getch();

}

  1. Design a program in C to print the series 5, 10, 15,………………10th term using for loop.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int i=;

printf(“\nthe required series is:\n”);

for(i=1;i<=10;i++)

printf(“%d\t”,i*5);

getch();

}

  1. Write a program that reads two integers a & b and then compute ab.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,i,result=1;

printf(“\nenter the value of a and b:\t”);

scanf(“%d%d”,&a,&b);

for(i=1;i<=b;i++)

        result*=a;

  printf(“\nThe result of the program is %d”,result”);        

getch();

}

 

  1. 14. Write a program for Nepal electricity Board, where user can supply name, address, and total unit of electricity consumption. Your program should print the bill using the following rates:

Unit                               rates

For the first 20units rs 75

For the next 50 unit Rs 6.5 per unit for next next units Rs 8 per unit. Service charge is Rs 100, which is compulsory.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

float units, rs;

char name[15],address[15];

printf(“\nenter the name:  “);

scanf(“%s”,name);

printf(“\nenter the address:   “);

scanf(“%s”,address);

printf(“\neneter the total units consumed:   “);

scanf(“%f”,&units);

rs=100;

if(units<=20)

rs+=75;      

else if(units>20 &&units<=70)

{  

                       units=units-20;

rs+=75+units*6.5;

}

else

{

units=units-70;

rs+=75+6.5*50+units*8;

}

clrscr();

printf(“\nname of the consumer : %s”,name);

printf(“\nAddress of the consumer:  %s”,address);

printf(‘\ntotal amount to be paid:  %f”,rs);

getch();

}

 

 

  1. Design a program that finds out whether the given number is Prime or not.

/* a number is said to be prime if the number is not multiple of other any number except 1 and that number. */

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int num,i;

clrscr();

printf(“please enter the number:\t”);

scanf(“%d”,&num);

for(i=2;i<=num;i++)

{

if(num%i==0)

     {

printf(“\n\nThe entered number %d is not prime number.”,num);

break;

}

}

if(i==num)

printf(“\n\n\nThe entered number %d is prime number.”);

getch();

}

 

  1. Write a program to input an integer number and find the sum of all digits of number. Suppose supplied number is 1234, then the sum should be 1+2+3+4=10.

solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int num,dig,sum=0;

printf(“\nPlease enter an integer:\t”);

scanf(“%d”,&num);

for( ; ; )

{

dig=num%10;

num=num/10;

sum+=dig;

if(num==0)

break;

}

printf(“\n\n the sum of digits is %d”,sum);

getch();

}

 

  1. Write a program that reverses and sums up the digits of an integer.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int num,dig,sum=0,rev=0;

printf(“\nPlease enter an integer:\t”);

scanf(“%d”,&num);

for( ; ; )

{

dig=num%10;

num=num/10;

sum+=dig;

rev*=10+dig;

if(num==0)

break;

}

printf(“\n\n the reverse of the given number is %d and the sum of its digits is %d”,reverse, sum);

getch();

}

//here the “for loop” is constructed for looping only.

Sample output of the program:

Please enter an integer:   2345

The reverse of the given number is 5432 and the sum of its digits is 14

 

  1. Write a program using nested loop to print the multiplication table for even numbers from 2 to 10.

Solution:

#include<conio.h>

#include<stdio.h>

void main()

{

int i,j,mul;

for(j=1;j<=10;j++)

{

for(i=2;i<=10;i=i+2)

     {

mul=i*j;

printf(“%d*%d=%d\t”,i,j,mul);

}

printf(“\n”);

}

getch();

}

SAMPLE OUTPUT OF THE PROGRAM

2*1=2   4*1=4   6*1=6   8*1=8   10*1=10

2*2=4   4*2=8   6*2=12  8*2=16  10*2=20

2*3=6   4*3=12  6*3=18  8*3=24  10*3=30

2*4=8   4*4=16  6*4=24  8*4=32  10*4=40

2*5=10  4*5=20  6*5=30  8*5=40  10*5=50

2*6=12  4*6=24  6*6=36  8*6=48  10*6=60

2*7=14  4*7=28  6*7=42  8*7=56  10*7=70

2*8=16  4*8=32  6*8=48  8*8=64  10*8=80

2*9=18  4*9=36  6*9=54  8*9=72  10*9=90

2*10=20 4*10=40 6*10=60 8*10=80 10*10=100

 

  1. Write a program to display the following series:

1     5     9     13   ………………………………… Up to the term value is less than 75.

    Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

for(i=1; ;i=i+4)

{ if(i>75)

break;

printf(“\t%d”,i);

}

getch();

}

sample output:

      1       5       9       13      17      21      25      29      33      37

      41      45      49      53      57      61      65      69      73

  1. Write a program to find factorial of a supplied number.

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int num;

int i;

long int fact=1;

printf(“\nPlease enter a number whose factorial is to be determined:\t”);

scanf(“%d”,&num);

printf(“\n\n\nfactorial of %d is :\n”);

for(i=1;i<=num;i++)

{

fact*=i;

if(i<num)

printf(“%d*”,i);

else

printf(“%d”,i);

}

         printf(“=%ld”,fact);

         getch();

    }

Sample output of the program:

 

Please enter a number whose factorial is to be determined:     6

factorial of 1326 is :

1*2*3*4*5*6=720_

 

  1. Write a program to display this pattern:

111111

11111

1111

111

11

1

Solution:

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j,k=1;

for(i=1;i<=6;i++)

    {

printf(“\n”);

for(j=6-i;j>=0;j--)

printf(“%d”,k);

    }

getch();

    }

 

 

 

  1. Write a program to display this pattern:

1

12

123

1234

12345

123456

    #include<stdio.h>

#include<conio.h>

void main()

{

int i,j;

for(i=1;i<=6;i++);

{

printf(“\n”);

for(j=1;j<=i;j++)

    printf(“%d”,j);

    }

getch();

}

 

  1. Write a program that read two numbers and display all the even numbers between them.

#include<stdio.h>

#include<conio.h>

void main()

{

int i,a,b,temp;

clrscr();

printf(“\nPlease enter the two numbers:\t”);

scanf(“%d%d”,&a,&b);

if(a>b)

     {

temp=a;

a=b;

b=temp;

     }

printf(“\nThe even numbers between the range %d to %d are:  \n”,a,b);

for(i=a;i<=b;i++)

{

if(i%2==0)

printf(“%d\t”,i);

}

getch();

}

sample output of the program:

Please enter the two numbers:   17 124

The even numbers between the range 17 to 124 are:

18      20      22      24      26      28      30      32      34      36

38      40      42      44      46      48      50      52      54      56

58      60      62      64      66      68      70      72      74      76

78      80      82      84      86      88      90      92      94      96

98      100     102     104     106     108     110     112     114     116    118     120     122     124

  1. Write a program that displays alphabets from a to z.

 

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

clrscr();

for(i='a';i<='z';i++)

    {

printf("%c\t",i);

}

getch();

}

sample output of the program:

a       b       c       d       e       f       g       h       i       j

k       l       m       n       o       p       q       r       s       t

u       v       w       x       y       z

 

  1. Write a program that displays alphabets from A to Z.

#include<stdio.h>

#include<conio.h>

void main()

{

int i;

clrscr();

for(i='A';i<='Z';i++)

    {

printf("%c\t",i);

}

getch();

}

sample output of the program:

A       B       C       D       E       F       G       H       I       J

K       L       M       N       O       P       Q       R       S       T

U       V       W       X       Y       Z

 

  1. Write a program that assigns a word westernregioncampus and display it in the following pattern.

W

We

Wes

West

Weste

Wester

Western

Western

Western r

Western re

Western reg

Western regi

Western regio

Western region

Western region

Western region c

Western region ca

Western region cam

Western region camp

Western region campu

Western region campus

 

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

int i,j,len;

char string[]=”western region campus”;

len=strlrn(string);

for(i=0;i<=len;i++)

{

for(j=0;j<i;j++)

printf(“%c”,string[j]);

printf(“\n”);

}

getch();

}

  1. Write a program to calculate the total salary of the employee with the following conditions:

basic salary     HRA DA

Rs<1500      0   0

>=1500 & <2300   10% 3%

>=2300 & <3500   15% 5%

>=3500 & <5000   20% 7%

>=5000       25% 700

Total Salary= Bs+HRA+DA */

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

char nam[50],post[50];

int bs;

float hra,da,ts;

clrscr();

printf("Enter the employee name:  ");

scanf("%s",nam);

printf("\nEnter the employee's post:  ");

scanf("%s",post);

printf("\nEnter basic salary of the employee:  ");

scanf("%d",&bs);

if(bs<1500)

{

hra=0;

da=0;

}

if(bs>=1500&&bs<2300)

{

hra=bs*10/100;

da=bs*3/100;

}

if(bs>=2300&&bs<3500)

{

hra=bs*15/100;

da=bs*5/100;

}

if(bs>=3500&&bs<5000)

{

hra=bs*20/100;

da=bs*7/100;

}

if(bs>=5000)

{

hra=bs*25/100;

da=700;

}

ts=bs+hra+da;

clrscr();

printf("\nName of employee:   %s",strupr(nam));

printf("\nPost:   %s",strupr(post));

printf("\nBasic Salary:  %d",bs);

printf("\nHRA:  %.2f",hra);

printf("\nDA:   %.2f",da);

printf("\nTotal salary:   %.2f",ts);

}

  1. write a program to read the age of 100 persons and count the no. of peresons

 in the group as below:

 0-10        40-50

 10-20              50-60

 20-30      60-70

 30-40      70-80   */

 #include<stdio.h>

 #include<conio.h>

 void main()

 {

 int i,j,n,a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,age[150];

 for(i=1;i<=100;i++)

 {

 printf("\nEnter the %dth age:   ");

 scanf("%d",&age[i]);

 if(age[i]>=0&&age[i]<10)

 a++ ;

 if(age[i]>=10&&age[i]<20)

 b++;

 if(age[i]>=20&&age[i]<30)

 c++;

 if(age[i]>=30&&age[i]<40)

 d++;

 if(age[i]>=40&&age[i]<50)

 e++;

 if(age[i]>=50&&age[i]<60)

 f++;

 if(age[i]>=60&&age[i]<70)

 g++;

 if(age[i]>=70&&age[i]<80)

 h++;

 }

 clrscr();

 printf("\nNo. of persons in the group (0-10):  %d",a);

 printf("\nNo. of persons in the group (10-20):  %d",b);

 printf("\nNo. of persons in the group (20-30):  %d",c);

 printf("\nNo. of persons in the group (30-40):  %d",d);

 printf("\nNo. of persons in the group (40-50):  %d",e);

printf("\nNo. of persons in the group (50-60):  %d",f);

printf("\nNo. of persons in the group (60-70):  %d",g);

printf("\nNo. of persons in the group (70-80):  %d",h);

 getch();

}

Control statement (Prev Lesson)
(Next Lesson) Some important theory questions for the final examination
Back to C-Programming

No Comments

Post a Reply

Teacher
Bhim Gautam
Role : Lecturer
Read More
error: Content is protected !!