C programming Solution (Basic I/O & Decision Control Structure Questions)

  1. Write a program to enter two number and find product.
#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

printf("Enter first number:");

scanf("%d",&a);

printf("Enter second number:");

scanf("%d",&b);

c=a*b;

printf("The Product is: %d", c);

getch();

}

 

2) Write a program to calculate area of triangle.

#include<stdio.h>
#include<conio.h>
void main()
{

int b,h;

float a;

printf("Enter breadth of triangle:");

scanf("%d",&b);

printf("Enter height of triangle: ");

scanf("%d",&h);

a= 0.5*b*h;

printf("Area is %f",a);

getch();

}

 

3) Write a program to enter two number and perform all arithmetic numbers.

#include<stdio.h>

#include<conio.h>

void main()

{

int x,y,a,d,p,di,mo;

printf("Enter first number:");

scanf("%d",&x);

printf("Enter second number:");

scanf("%d",&y);

a=x+y;

d=x-y;

p=x*y;

di=x/y;

mo=x%y;

printf("The sum is: %d \n", a);

printf("The difference is: %d \n", d);

printf("The product is: %d\n", p);

printf("The Division is: %d\n", di);

printf("The Remainder is: %d\n", mo);

getch();

}

 

  1. Write a program to calculate compound Interest.

 

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()

{

int p,t,r;

int c;

printf("Enter principal, time and rate");

scanf("%d%d%d",&p,&t,&r);

c=p*(pow((1+r*0.01),t)) -p;

printf("Compound Interest is %d",c);

getch();

}

 

 

  1. Write a program to enter length of any one side and calculate area and perimeter of square.
#include<stdio.h>
#include<conio.h>

void main()

{

                int l,a,p;

                printf("Enter length of a square:");

                scanf("%d",&l);

                a=l*l;

                p=4*l;

                printf("Area of square is %d \n",a);

                printf("Perimeter of square is %d",p);

getch();

}

 

  1. Write a program to display folloing outputs using printf() function.

##########

#

#

## #######

#

#

 

#include<stdio.h>

#include<conio.h>




void main()

{

               

printf("##########\n");

printf("#\n");

printf("#      \n");

printf("##########\n");

printf("#  \n");

printf("#     ");

getch();

}

 

 

Question Related To Library Function

Write a program to show the use of.

  1. <ctype.h> (tolower(char),toupper(char), isupper(),islower(),isdigit(),isalmun() )
#include<stdio.h>

#include<conio.h>

#include<ctype.h>

void main()

{

char lower='a';

char upper='B';

char digit='5';

char alnum='*';




printf("lower to upper: %c \n",toupper(lower));

printf("Upper to lower: %c \n",tolower(upper));

//use of isupper() and islower()

if(islower(lower))

{

                printf("Character is lower\n");

}

if(isupper(upper))

{

                printf("Character is Upper\n");

}

if(isalnum(alnum))

{

                printf("This is alphanumeric");

}

else{

                printf("This is not alphanumeric");

}

getch();

}

 

 

  1. b) <Math.h>

floor(), ceil(), squr(),log(),pow(base,pow)

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()

{

                double num=3.5,base=2,power=3;




                printf("The Ceil of %f is %f \n",num,ceil(num));

                printf("The floor of %f is %f \n",num,floor(num));

printf("The Square Root of %f is %f \n",num,sqrt(num));

                printf("The Logarithm of %f is %f \n",num,log(num));

                printf(" %f raised to power %f is %f \n",base,power,pow(base,power));




getch();

}

 

 

Ternary Operators

Solve the following by using ternary operator.

  1. WAP to enter a number and check whether the number is odd or even.
#include<stdio.h>
#include<conio.h>

void main()

{

int a;

printf("Enter a no:");

scanf("%d",&a);




(a%2==0)?printf("The Number is even"):printf("The number is odd");




getch();

}

WAP to check whether the number is positive or negative;

#include<stdio.h>

#include<conio.h>

void main()

{

int a;

printf("Enter a no:");

scanf("%d",&a);

(a>0)?printf("The Number is Positive"):printf("The number is Negative");

getch();

}

 

Sequential Control Structure

  1. Write a program to ask two ask a number from user and display cube of the number without using library function.
#include<stdio.h>
#include<conio.h>

void main()

{

int a,c;

printf("Enter a no:");

scanf("%d",&a);

c=a*a*a;

printf("The cube of the number is:%d",c);

getch();

}

 

  1. Write a program to input a number and fint square with and without using math.h libray function.
#include<stdio.h>
#include<conio.h>

#include<math.h>

void main()

{

int a;

printf("Enter a number:");

scanf("%d",&a);

//Without using library function

printf("Square without using library function: %d\n",a*a);

//using library function

int ans=pow(a,2);

printf("Square using library function: %d",ans);

getch();

}

 

  1. Write a program to enter the length, breadth and height of a box and calculate the total surface area and volume of the cuboid.
#include<stdio.h>
#include<conio.h>
void main()
{

int l,b,h,v,t;

printf("Enter Length of a cuboid:");

scanf("%d",&l);

printf("Enter Breadth of the cuboid:");

scanf("%d",&b);

printf("Enter Height of the cuboid:");

scanf("%d",&h);


t=2*(l*b+b*h+h*l);

v=l*b*h;

printf("The TSA of cuboid is %d \n The Volume of Cuboid is %d",t,v);

getch();

}

WAP to enter radious of circle and calculate area and circumference.

#include<stdio.h>

#include<conio.h>




void main()

{

float r,a,c;

const int pi=22/7;

printf("Enter radius of circle:");

scanf("%f",&r);

a=pi*r*r;

c=2*pi*r;

printf("The area of circle is: %f \n The circumference of circle is: %f",a,c);

getch();

}

 

Question Related To Decision Control Structure

  1. A) Question related to If() Statement
  2. Write a program to enter the marks of English Subject and print “Pass” if it is 40 or more.
#include<stdio.h>

#include<conio.h>




void main()

{

float m;




printf("Enter your mark:");

scanf("%f",&m);

if(m>=40)

{

                printf("Pass");

}




getch();

}

 

  1. Write a program to read a number and find odd or even by using if ()
#include<stdio.h>

#include<conio.h>




void main()

{

int n;




printf("Enter a number");

scanf("%d",&n);

if(n%2==0)

{

                printf("Even");

}

if(n%2!=0)

{

                printf("Odd");

}

getch();

}

 

  1. Write a program to enter the age of a person and print “He can vote”, if the age is 18 or above.
#include<stdio.h>

#include<conio.h>

void main()

{

int age;

printf("Enter a number");

scanf("%d",&age);

if(age>=18)

{

                printf("He can vote");

}

getch();

}

 

  1. B) Question related to If..Else condition
  2. WAP to enter marks of English and print “Pass” if it is 40 or more, otherwise “Fail”.
#include<stdio.h>

#include<conio.h>

void main()

{

int mark;

printf("Enter your mark:");

scanf("%d",&mark);

if(mark>=40)

{

                printf("Pass");

}

else

{

                printf("Fail");

}




getch();

}

 

  1. WAP to check input number is negative or non-negative.
#include<stdio.h>

#include<conio.h>




void main()

{

int n;




printf("Enter a number:");

scanf("%d",&n);

if(n>0)

{

                printf("Positive");

}

else

{

                printf("Negative");

}




getch();

}

 

  1. WAP to enter the age of a person and print “He can vote” if the age is 18 or above, otherwise ” He cannot vote”
#include<stdio.h>

#include<conio.h>

void main()

{

int age;

printf("Enter a number");

scanf("%d",&age);

if(age>=18)

{

                printf("He can vote");

}

else

{

                printf("He can not vote");

}

getch();

}

 

  1. WAP to check whether given number is odd or even.
#include<stdio.h>

#include<conio.h>




void main()

{

int n;




printf("Enter a number:");

scanf("%d",&n);

if(n%2==0)

{

                printf("Even");

}

else

{

                printf("Odd");

}




getch();

}

 

  1. WAP to find the greatest number among two numbers entered by the user.
#include<stdio.h>

#include<conio.h>

void main()

{

int a,b;




printf("Enter a number:");

scanf("%d",&a);




printf("Enter Another number:");

scanf("%d",&b);




if(a>b)

{

                printf("%d is greater",a);

}

else

{

                printf("%d is greater",b);

}




getch();

}

 

  1. c) Question Related to Nested if() else.
  2. WAP to check the greatest among 3 inputs from user by using nested if() else
#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;




printf("Enter first number:");

scanf("%d",&a);

printf("Enter second number:");

scanf("%d",&b);

printf("Enter third number:");

scanf("%d",&c);




if(a>b)

{

                if(a>c)

                {

                                printf("a is greatest");

                }

                else

                {

                                printf("c is greatest");

                }

}

else{

                if(b>c)

                {

                                printf("b is greatest");

                }

                else

                {

                                printf("c is greatest");

                }

}

getch();

}

 

  1. D) Questions related to If.. else if
  2. WAP to enter a number and print “Positive” if it is greater than 0, “Negative” if it is less than 0 , otherwise print “Zero”.
#include<stdio.h>

#include<conio.h>

void main()

{

int a;




printf("Enter first number:");

scanf("%d",&a);




if(a>0)

{

                printf("Positive");

}

else if(a<0)

{

                printf("Negative");

}

else

{

                printf("Zero");

}




getch();

}

 

  1. WAP to read marks of 5 subjects and calculate percentage as follows.
  2. percentage>=75 and <=100 Distinction
  3. percentage>= 60 and <75 First division

iii. percentage>=45 and <60 second division

  1. percentage>=35 and <45 Pass division
  2. Else fail
#include<stdio.h>

#include<conio.h>

void main()

{

int m1,m2,m3,m4,m5,percentage;




printf("Enter mark of subject 1: ");

scanf("%d",&m1);

printf("Enter mark of subject 2: ");

scanf("%d",&m2);

printf("Enter mark of subject 3: ");

scanf("%d",&m3);

printf("Enter mark of subject 4: ");

scanf("%d",&m4);

printf("Enter mark of subject 5: ");

scanf("%d",&m5);




percentage=(m1+m2+m3+m4+m5)/5;

printf("Percentage: %d",percentage);




if(percentage>=75 && percentage<=100)

{

                printf("Distinction");

}

else if(percentage>=60 && percentage<75)

{

                printf("First");

}

else if(percentage>=45 && percentage<60)

{

                printf("Second");

}

else if(percentage>=35 && percentage<45)

{

                printf("\n Pass");

}

else

{

                printf("\n Fail");

}







getch();

}

 

  1. WAP to enter 3 numbers and print the greatest number using if… else if
#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;




printf("Enter first number:");

scanf("%d",&a);

printf("Enter second number:");

scanf("%d",&b);

printf("Enter third number:");

scanf("%d",&c);




if(a>b && a>c)

{

                printf("a is greatest");

}

else if(b>c)

{

                printf("b is greatest");

}

else

{

                 printf("c is greatest");

}




getch();

}

 

  1. WAP to input any three numbers and find the middle number.
#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;




printf("Enter first number:");

scanf("%d",&a);

printf("Enter second number:");

scanf("%d",&b);

printf("Enter third number:");

scanf("%d",&c);




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

{

                printf("b is middle number ");

}

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

{

                printf("a is middle number");

}

else

{

                 printf("c is middle number");

}




getch();

}

 

  1. E) Switch()
  2. WAP to print name of days according as number input from keyboard such as 1 for Sunday, 2 for Monday…… 7 for Saturday and other for wring input.
#include<stdio.h>

#include<conio.h>

void main()

{

int choice;




printf("1. sunday \n 2. Monday \n 3. Tuesday \n 4. Wednesday \n 5. Thursday \n 6. Friday \n 7. Saturday");

printf("\n choose anyone, enter number (1,2....7):");




scanf("%d",&choice);




switch(choice)

{

case 1:

printf("Sunday");

break;




case 2:

printf("Monday");

break;




case 3:

printf("Tuesday");

break;




case 4:

printf("Wednesday");

break;




case 5:

printf("Thursday");

break;




case 6:

printf("Sunday");

break;




case 7:

printf("Friday");

break;




default:

printf("Not valid Input");

break;

}

getch();

}

 

  1. WAP to print the following menu and do the task accordingly.
  2. a) Area of Rectangle
  3. b) Perimeter of Rectangle
  4. c) Area of circle
  5. d) Area of square
#include<stdio.h>
#include<conio.h>

void main()

{

int l,b,r;

char choice;

printf("a. Area of Rectangle \n b. Perimeter of Rectangle \n c. Area of circle \n d. Area of square ");

printf("\n choose anyone, (a,b,c,d):");

scanf("%c",&choice);

switch(choice)

{

case 'a':

printf("Enter lenght of Rectangle: ");

scanf("%d",&l);

printf("Enter Breadth of Rectangle: ");

scanf("%d",&b);

printf("Area of the Rectangle is %d",l*b);

break;

case 'b':

printf("Enter lenght of Rectangle: ");

scanf("%d",&l);

printf("Enter Breadth of Rectangle: ");

scanf("%d",&b);

printf("Perimeter of the Rectangle is %d",2*(l+b));

break;

case 'c':

printf("Enter radious of circle: ");

scanf("%d",&r);

printf("Perimeter of the circle is is %d",2*22/7*r);

break;

case 'd':

printf("Enter length of square: ");

scanf("%d",&l);

printf("area of the suare is is %d",l*l);

break;

default:

printf("Not valid Input");

break;

}

getch();

}

 

  1. WAP to print the following menu and do the task accordingly

Menu

 Addition of two numbers

Subtraction of two numbers

Multiplication of two numbers

Division of two numbers

Exit

Note: user’s choice should be ‘a’ for addition, ‘s’ for subtraction, ‘m’ for multiplication, ‘d’ for division and ‘e’ for exit.

#include<stdio.h>
#include<conio.h>

void main()

{

int a,b;

char choice;

printf(" Addition of two numbers \n Substraction of two numbers \n Multiplication of two numbers \n Division Of Two Numbers \n Exit");

printf("'a' for addition, 's' for subtraction, 'm' for multiplication, 'd' for division and 'e' for exit.");

scanf("%c",&choice);

switch(choice)

{

case 'a':

printf("Enter first Number: ");

scanf("%d",&a);

printf("Enter second Number: ");

scanf("%d",&b);

printf("Additon of two number is %d",a+b);

break;

case 's':

printf("Enter first Number: ");

scanf("%d",&a);

printf("Enter second Number: ");

scanf("%d",&b);

printf("Substraction of two number is %d",a-b);

break;

case 'm':

printf("Enter first Number: ");

scanf("%d",&a);

printf("Enter second Number: ");

scanf("%d",&b);

printf("Multiplication of two number is %d",a*b);

break;

case 'd':

printf("Enter first Number: ");

scanf("%d",&a);

printf("Enter second Number: ");

scanf("%d",&b);

printf("Divison of two number is %d",a/b);

break;

case 'e':

break;


default:

printf("Invalid Input ");

break;

}


getch();

}

Question related To Unconditional Statement.
1. WAP to allow user to enter only value under 100 by using Goto statement.

#include<conio.h>
#include<stdio.h>
void main()
{
int n;
label:
 printf("Enter a number ");
 scanf("%d",&n);

if(n>99)
{
  goto label;
}
else
{
  printf("You have entered value under 100");
}
getch();
}