C Programming Questions and Solutions - Computer Notes | Computer Notes for 11 and 12 | PLK Computer Sir

Breaking

Home Top Ad

Post Top Ad

Responsive Ads Here

C Programming Questions and Solutions

  1. Write a C program to calculate area and circumference of a real circle.


#include <stdio.h>

void main()

{

float pi,r,a,c;

pi = 3.14;

printf("Enter radius : ");

scanf("%f",&r);

if(r>0)

{

a=pi*r*r;

c=2*pi*r;

printf("Area = %.2f \n",a);

printf("Circumference = %.2f",c);

}

else

printf("Sorry, given circle is not a real circle");

}

 


  1. Write a program in C to check whether the given number is odd or even.


#include <stdio.h>

void main()

{

int num,rem;

printf("Enter any number : ");

scanf("%d",&num);

rem=num%2;

if(rem==0)

printf("%d is even number.",num);

else

printf("%d is odd number.",num);

}



  1. Write a C program to input a number and check whether it is positive, negative or zero.


#include <stdio.h>
void main ()
{
int num;
printf("Enter any number : ");
scanf("%d",&num);
if(num>0)
printf("%d is positive number.",num);
else if(num<0)
printf("%d is negative number.",num);
else
printf("Given number is zero.");
}


  1. Write a C program that stores cost price(cp) and selling price(sp) and determines whether there is loss or gain.

#include <stdio.h>

void main ()

{

float sp,cp,gain,loss;

printf("Enter selling price : ");

scanf("%f",&sp);

printf("Enter cost price : ");

scanf("%f",&cp);

if(sp>cp)

{

gain=sp-cp;

printf("Gain = NPR. %.2f",gain);

}

else

{

loss=cp-sp;

printf("Loss = NPR. %.2f",loss);

}

}


  1. Write a C program that stores three different numbers and display the largest and smallest among them.

#include <stdio.h>

void main ()

{

int a,b,c;

printf(" Enter three numbers \n");

scanf("%d%d%d",&a,&b,&c);

if(a>b && a>c)

printf("%d is largest number\n ",a);

else if(b>a && b>c)

printf("%d is largest number\n ",b);

else

printf("%d is largest number\n ",c);

if(a<b && a<c)

printf("%d is smallest number\n ",a);

else if(b<a && b<c)

printf("%d is smallest number\n ",b);

else

printf("%d is smallest number\n ",c);

}


  1. Write a C program to find the middle number among three different numbers.

#include <stdio.h>

void main()

{

int a,b,c;

printf("Enter three numbers \n");

scanf("%d%d%d",&a,&b,&c);

if(a<b&&a>c ||a>b&&a<c)

printf("%d is middle number",a);

else if(b<a&&b>c || b>a&&b<c)

printf("%d is middle number",b);

else

printf("%d is middle number",c);

}


  1. Write a C program in C to calculate and display real roots of quadratic equation.


#include <stdio.h>

#include<math.h>

void main()

{

float a,b,c,root1,root2;

printf("Enter value of a : ");

scanf("%f",&a);

printf("Enter value of b: ");

scanf("%f",&b);

printf("Enter value of c : ");

scanf("%f",&c);

if((b*b-4*a*c)>0)

{

root1=(-b+sqrt(b*b-4*a*c))/2*a;

root2=(-b-sqrt(b*b-4*a*c))/2*a;

printf("First root= %.2f \n",root1);

printf("Second root= %.2f",root2);

}

else

printf("Sorry, can not calculate real roots");

}



  1. Write a C program to find the commission amount on the basis of sales amount as per the following conditions:



Sales Amount

Comission

0-1000

5%

1001-2000

10%

>2000

12%

  


#include <stdio.h>

void main()

{

float sa,c;

printf("Enter sales amount : ");

scanf("%f",&sa);

if(sa<1000)

c=sa*5/100;

else if(sa>1001 && sa<=2000)

c=sa*10/100;

else

c=sa*12/100;

printf("Comission amount is %.2f ",c);

}



  1. Write an interactive C program that takes length and breadth and performs the following task.

  1. Area of rectangle

  2. Perimeter of rectangle

  3. Exit

#include<stdio.h>

#include<stdlib.h>

void main()

 {

int l,b,area,perimeter;

char ch;

printf("Menu\n");

printf("a. Area of rectangle \n");

printf("b. perimeter of rectangle \n");

printf("c. Exit \n");

printf("Enter your choice :");

scanf("%c",&ch);

switch(ch)

{

case 'a':

printf("Enter length :");

scanf("%d",&l);

    printf("Enter breadth :");

scanf("%d",&b);

area=l*b;

printf(" Area of rectangle is %d ",area);

break;

case 'b':

printf("Enter length :");

scanf("%d",&l);

    printf("Enter breadth :");

scanf("%d",&b);

perimeter=2*(l+b);

printf(" Perimeter of rectangle is %d ",perimeter);

break;

case 'c':

printf("Your choice is exit!");

exit(0);

default:

printf("Wrong Choice");

  }

}


  1. Write a C program that takes a number less than 10 and displays its multiplication table.


#include <stdio.h>

void main()

{

int n,i;

printf("Enter any number : ");

scanf("%d",&n);

if(n<10)

{

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

{

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

}

}

else

printf("Given number is greater than 10 ");

}


11.  Write a program to input three numbers and print sum and average using C program.


#include<stdio.h>
void main ()
{
int a,b,c,sum=0;
float average=0;
printf("Enter three numbers: ");
scanf("%d%d%d",&a,&b,&c);
sum=a+b+c;
average=(float)sum/3;
printf(" Sum of three numbers is %d \n ",sum);
printf(" Average of three numbers is %.2f ",average);
} 


12. Write a program to find out the square root of a number using the C program.


#include<stdio.h>
#include<math.h>
void main ()
{
int n,a;
printf(" Enter a number : ");
scanf("%d",&n);
a=sqrt(n);
printf(" Square root of %d is %d ",n,a);
}

13. Write a program in C to calculate and print simple interest (SI) and net amount(A), given that SI=PTR/100 and A=SI+P.


#include<stdio.h>
void main ()
{
int p,t,r,si,a;
printf("Enter principal, time and rate : ");
scanf("%d%d%d",&p,&t,&r);
si=p*t*r/100;
a=si+p;
printf(" Simple interest is %d \n ",si);
printf(" Net amount is %d ",a);
}


14. Write a program in C to calculate distance using s=ut+1/2at.


#include<stdio.h>

void main ()

{

float u,t,a,s;

printf("Enter initial velocity , time and acceleration \n");

scanf("%f%f%f",&u,&t,&a);

s=u*t+0.5*a*t*t;

printf("Distance covered is %0.2f ",s);

} 


15. Write a program in C to calculate area and circumference of a circle.


#include <stdio.h>

void main ()

{

float pi,r,a,c;

pi = 3.14;

printf("Enter radius of circle \n");

scanf("%f",&r);

a=pi*r*r;

c=2*pi*r;

printf("Area = %.2f \n",a);

printf("Circumference = %.2f",c);

}

16.Write a program in C to convert temperature from centigrade(c) into Fahrenheit(f).


#include<stdio.h>

void main()

{

float c,f;

printf(" Enter temperature in centigrade ");

scanf("%f",&c);

f=1.8*c+32;

printf(" Temperature in Fahrenheit = %.2f ",f);

}


17. Write a program in C to calculate the sum of two distances and the distance is measured in terms of feet and inches.

#include<stdio.h>

void main ()

{

int feet1, feet2, feet3, totalfeet, inch1, inch2, inch3, totalinch;

printf("Enter feet1 and feet2 \n");

scanf("%d%d",&feet1,&feet2);

printf("Enter inch1 and inch2 \n");

scanf("%d%d",&inch1,&inch2);

feet3=feet1+feet2;

inch3=inch1+inch2;

totalfeet=inch3/12+feet3;

totalinch=inch3%12;

printf("Feet=%d Inch=%d ",totalfeet,totalinch);

}


18. Write a program in C to enter a number of days and convert it into years, months and days.

#include<stdio.h>

void main ()

{

int days,y,m,d,rd;

printf(" Enter the days : ");

scanf("%d",&days);

y=days/365;

rd=days%365;

m=rd/30;

d=rd%30;

printf(" Year=%d Month=%d Day=%d",y,m,d);

}


19. Complete the following programs ans discuss the outputs.

First Program

float x;
int x1=5;
int x2=2;
x=x1/x2;
printf("%f",x);

Answer

#include<stdio.h>
void main ()
{
float x;
int x1=5;
int x2=2;
x=x1/x2;
printf("%.2f",x);
}


Second Program

float x;
int x1=5;
int x2=2;
x=(float) x1/x2;
printf("%f",x);

Answer

#include<stdio.h>
void main ()
{
float x;
int x1=5;
int x2=2;
x=(float)x1/x2;
printf("%.2f",x);
}



20. Complete the following programs ans discuss the outputs.

First Program

int j;
int i=5;
j=++i;
printf(" i=%d\n j=%d",i,j)

Answer

#include<stdio.h>
void main()
{
int j;
int i=5;
j=++i;
printf(" i=%d\n j=%d",i,j);
}


Second Program

int j;
int i=5;
j=i++;
printf(" i=%d\n j=%d",i,j);

Answer

#include<stdio.h>
void main ()
{
int j;
int i=5;
j=i++;
printf(" i=%d\n j=%d",i,j);
}

No comments:

Post a Comment

Post Bottom Ad

Responsive Ads Here

Pages