Questions are:
-------------------------------------------------------------------------------------------------------------
C Lab Work 2 : Branching Statements
- Write a C program to calculate area and circumference of real circle(hint: real circle is a circle having positive radius).
- Write a C program to check whether the given number is odd or even.
- Write a C program to read a number and check whether it is positive, negative or zero.
- Write a C program that inputs cost price(CP) and selling price(SP) and determines whether there is loss or gain.
- Write a C program that reads three numbers and displays the largest and the smallest among them.
- Write a C program that takes three different numbers and find out the middle number.
- Write a C program to calculate and display real roots of a quadratic equation. (Hint: check the condition (b2-4ac)>0 for real root.)
- Write a C program to find the commission amount on the basis of sales amount as per the following conditions:
Sales amount Commission
0-1000 5%
1001-2000 10%
>2000 12% - Write a interactive C program that takes length and breadth and performs the following task.
(a) Area of rectangle
(b) Perimeter of rectangle
(c) Exit - Write a C program that takes a number less than 10 and displays its multiplication table.
Write a C program to calculate area and circumference of a real circle.(Hint: real circle is a circle having positive radius).
ANSWER:
Algorithm to find area and circumference of a real circle
Step1: Start
Step 2: Input radius as r
Step 3: Check, is r>0?
3.1 If yes, calculate area and circumference and display it.
3.2 If no, display “This is not a real circle”.
Step 4: Stop
/*C Code to find area and circumference of a real circle*/
#include <stdio.h>
int main()
{
float r,a,c;
printf("Enter radius : ");
scanf("%f",&r);
if(r>0)
{
a=3.14*r*r;
c=2*3.14*r;
printf("Area = %0.2f and Circumference = %0.2f",a,c);
}
else
{
printf("Sorry, given circle is not a real circle");
}
return 0;
}
Write a program in C to check whether the given number is odd or even.
ANSWER:
Algorithm to check odd or even
Step1: Start
Step 2: Input number as num
Step 3: Calculate remainder by dividing num by 2
Step 4: Check is rem equal to zero?
4.1 If yes, Display “given number is even”
4.2 If no, Display “given number is odd”
Step 5: Stop
/*C Code to check odd or even*/
#include <stdio.h>
int 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);
}
return 0;
}
Write a C program to input a number and check whether it is positive, negative or zero.
ANSWER:
Algorithm to check whether the given number is positive, negative or zero.
Step1: Start
Step 2: Input a number as num
Step 3: Check that, is num greater than zero?
3.1 If yes, Display “ given number is positive”
3.2 If no, goto step 4
Step 4: Check that, is num less than zero?
4.1 If yes, Display “ given number is negative”
4.2 If no, Display “given number is zero”
Step 5: Stop
/*C Code to check whether the given number is positive , negative or zero.*/
#include <stdio.h>
int main()
{
int num;
printf("Enter any number : ");
scanf("%d",&num);
if(num>0)
{
printf("%d is positive.",num);
}
else if(num<0)
{
printf("%d is negative.",num);
}
else
{
printf("Given number is zero.");
}
return 0;
}
Write a C program that stores cost price(cp) and selling price(sp) and determines whether there is loss or gain.
ANSWER:
Algorithm to check loss or gain
Step1: Start
Step 2: Input sp and cp
Step 3: Check, is sp greater than cp?
3.1 If yes, calculate gain=sp-cp and display gain
3.2 If no, calculate loss=cp-sp and display loss.
Step 4: Stop
/*C Code to check loss or gain*/
#include <stdio.h>
int main()
{
float sp,cp,gain,loss;
printf("Enter selling price and cost price : ");
scanf("%f%f",&sp,&cp);
if(sp>cp)
{
gain=sp-cp;
printf("Gain = NPR. %0.2f",gain);
}
else
{
loss=cp-sp;
printf("Loss = NPR. %0.2f",loss);
}
return 0;
}
Write a C program that stores three different numbers and display the largest and smallest among them.
ANSWER:
Algorithm to find the largest and smallest among three different numbers
Step1: Start
Step 2: Input three different numbers as a,b and c
Step 3: Check, is a>b and a>c?
3.1 If yes, Display “First number is largest”
3.2 If no, goto step 4
Step 4: Check is b>a and b>c?
4.1 If yes, Display “Second number is largest”
4.2 If no, Display “ Third number is largest”
Step 5: Check is a<b and a<c?
5.1 If yes, Display “First number is smallest”
5.2 If no, goto step 6
Step 6: Check is b<a and b<c?
6.1 If yes, Display “Second number is smallest”
6.2 If no, Display “Third number is smallest”
Step 7: Stop
/*C Code to check the largest and smallest number among three different numbers
*/
#include <stdio.h>
int main()
{
int a,b,c;
printf(" Enter three numbers : ");
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 ",a);
}
else if(b<a && b<c)
{
printf(" %d is smallest number ",b);
}
else
{
printf(" %d is smallest number ",c);
}
return 0;
}
Write a C program to find the middle number among three different numbers.
ANSWER:
Algorithm to find middle number among three different numbers
Step1: Start
Step 2: Input three numbers as a,b and c
Step 3: Check, is a<b AND a>c OR a>b AND a<c?
3.1 If yes, Display “First number is middle”
3.2 If no, go to step 4
Step 4: Check is b<a AND b>c OR b>a AND b<c?
4.1 If yes, Display “Second number is middle”
4.2 If no, Display “Third number is middle”
Step 5: Stop
/*C Code to find middle number among three different numbers*/
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers : ");
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);
}
return 0;
}
Write a C program in C to calculate and display real roots of quadratic equation. (Hint: Check the condition (b2-4ac)>0 for real root)
ANSWER:
Algorithm to calculate and display real roots of quadratic equation
Step1: Start
Step 2: Input the value of a , b and c
Step 3: Check, is (b2-4ac)>0?
3.1 If yes, use formula quadratic formula and display the roots
3.2 If no, Display “Sorry, we can not calculate real root”
Step 4: Stop
/*C Code to calculate and display real roots of quadratic equation*/
#include <stdio.h>
#include<math.h>
int main()
{
float a,b,c,r1,r2;
printf("Enter value of a , b and c : ");
scanf("%f%f%f",&a,&b,&c);
if((b*b-4*a*c)>0)
{
r1=(-b+sqrt(b*b-4*a*c))/2*a;
r2=(-b-sqrt(b*b-4*a*c))/2*a;
printf(" First root= %0.2f and Second root=%0.2f",r1,r2);
}
else
{
printf("Sorry, can not calculate real roots");
}
return 0;
}
Write a C program to find the commission amount on the basis of sales amount as per the following conditions:
Sales amount Commision
0-1000 5%
1001-2000 10%
>2000 12%
ANSWER:
Algorithm to find commission amount
Step1: Start
Step 2: Input sales amount(sa)
Step 3: Check, is sa<1000?
3.1 If yes, calculate and print 5% commission.
3.2 If no, go to step 4
Step 4: Check, is sa>1001 and sa<2000?
4.1 If yes, calculate and print 10% commission.
4.2 If no, calculate and print 12% commission.
Step 5 : Stop
/*C Code to find commission amount*/
#include <stdio.h>
int 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("Commision amount is %0.2f ",c);
return 0;
}
Write an interactive C program that takes length and breadth and performs the following task.
Area of rectangle
Perimeter of rectangle
Exit
ANSWER:
Algorithm for menu driven program
Step1: Start
Step 2: Input length(l) and breadth(b)
Step 3: Display menu and Enter your choice
Step 4: Check the user’s choice.
4.1 If the user enters (a) calculate the area of the rectangle and display.
4.2 If the user enters (b) calculate the perimeter and display.
4.3 If the user enters (c) exit from the program.
Step 5 : Stop
/*C Code for menu driven program*/
#include <stdio.h>
#include<stdlib.h>
int main()
{
int l,b,area,perimeter;
char ch;
printf(" you have following 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);
printf("Enter length and breadth : ");
scanf("%d%d",&l,&b);
switch(ch)
{
case 'a':
area=l*b;
printf("Area of rectangle is %d ",area);
break;
case 'b':
perimeter=2*(l+b);
printf("Perimeter of rectangle is %d ",perimeter);
break;
default:
exit(0);
}
return 0;
}
Write a C program that takes a number less than 10 and displays its multiplication table.
ANSWER:
Algorithm to that takes a number less than 10 and displays its multiplication table
Step1: Start
Step 2: Input any number(n)
Step 3: Check, is n<10?
3.1 If yes,
for(i=1;i<=10;i++)
{
Print num*i
}
3.2 If no,
Print “Number is greater than 10”
Step 4 : Stop
/*C Code to that takes a number less than 10 and displays its multiplication table*/
#include <stdio.h>
int 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 ");
}
return 0;
}
--------------------------------------------------
case study:
one:
q:-Nepal Telcom calculates bill according to following rules.
no. of calls rate
0-180 Rs.150
181-500 Rs 1 per call
above Rs. 0.5 per call
calculate total amount,service tax(10%),VAT(13%)and finally net amount.
Ans:-
/*case study one*/ #include<stdio.h> int main() { float total_call,vat_amount,net_amount; float service_tax_amount; float total_amount; printf("enter total calls used\n"); scanf("%f",&total_call); if(total_call>=0 && total_call<=180) { total_amount=150; service_tax_amount=0.1*total_amount; vat_amount=0.13*total_amount; net_amount=total_amount+service_tax_amount+vat_amount; } else if(total_call>=181 && total_call<=500) { total_amount=150; service_tax_amount=0.1*total_amount; vat_amount=0.13*total_amount; net_amount=total_amount+service_tax_amount+vat_amount; } printf("total_amount=%f\n",total_amount); printf("service tax amount=%f\n",service_tax_amount); printf("vat_amount=%f\n",vat_amount); printf("net amount=%f\n",net_amount); return 0; }
two:-
q:-Write a menu driven program to read a number and to perform following task.
a.calculate factorial of a number
b.check whether the number is prime or composite
c.to print multiplication table of a number.
d.exit from the program.
ans:
/* case study 2*/ #include<stdio.h> #include<stdlib.h> int main() { int number,i,facto=1,tem,rem,sum=0; int choice; printf("you have following menu\n"); printf("1.for factorial value of a number\n"); printf("2.for prime or composite \n"); printf("3.for multiplication table of a number\n"); printf("4.for Palidromeness of a number\n"); printf("any other number to exit\n"); printf("--------------------------------\n"); printf("enter your choice\n"); scanf("%d",&choice); switch(choice) { case 1: printf("we are going to find factorial value of a number\n"); printf("enter a number\n"); scanf("%d",&number); for(i=1;i<=number;i++) { facto=facto*i; } printf("factorial value is=%d\n",facto); break; case 2: printf("we are going to test prime or composite of a number\n"); printf("enter a number\n"); scanf("%d",&number); for(i=2;i<=number;i++) { if(number%i==0) { break; } } if(i==number) { printf("%d is prime\n",number); } else { printf("it(%d) is composite\n",number); } break; case 3: printf("we are going to print multiplication table of a number\n"); printf("enter a number\n"); scanf("%d",&number); for(i=1;i<=10;i++) { printf("%d*%d=%d\n",number,i,number*i); } break; case 4: printf("we are going to test Palidromeness of a number\n"); printf("enter a number\n"); scanf("%d",&number); tem=number; while(number!=0) { rem=number%10; sum=sum*10+rem; number=number/10; } if(sum==tem) { printf("%d is palindrome\n",tem); } else { printf("it(%d) is not palindrome\n",tem); } break; default: printf("this choice is not in the list!\n"); exit(0); } return 0; }
-------------------------------------------------
For this(program), credit goes to PLK sir:
https://plkcomputersir.blogspot.com/2021/01/ans-of-c-lab-work-2.html
Sir can you please upload its flowchart too.
ReplyDelete