10th Class Chapter 3
10th Class Chapter 3
Short Questions
UNIT # 3
Conditional Logic
Associated Code
End
Start
Q4: Draw a flowchart to show the
structure of “if else” statement.
Ans: False Condition True
End
Q5: How to write associated code in selection statement if it contains one or more
statements?
Ans: If we want to associate more than one statements to an if statement, then they need to
be enclosed inside a { } block, but if we want to associate only one statement, then
although it may be enclosed inside { } block, but it is not mandatory.
Created By: Bilal Ahmad 1
Q6: Write a program in C language that takes the percentage of student as an input
and displays “PASS” if the percentage is above 50.
Ans: #include <stdio.h>
void main()
{
float percentage;
printf(“Enter the percentage: ”);
scanf(“%f”, &percentage);
if( percentage > 50 )
printf(“PASS \n ”);
}
Q7: Which thing improves the readability of the program.
Ans: Properly indent the instructions under if statement using tab. It improves the readability of
the program.
Q8: What is block or compound statement?
Ans: A set of multiple instructions enclosed in braces is called a block or a compound
statement.
Q9: Write a program that takes a character as input and displays “DIGIT” if the
character entered by user is a digit between 0 to 9, otherwise displays “NOT
DIGIT”.
Ans: #include <stdio.h>
void main()
{
char input ;
printf(“Please enter a character:”);
scanf(“%c”, &input);
if( input >= ‘0’ && input <= ‘9’ )
printf(“DIGIT \n”);
else
printf(“NOT DIGIT \n”)
If (condition 1)
Code to execute if condition 1 is true;
else if (condition 2)
Code to execute if condition 1 is false but condition 2 is true;
.
.
.
else if (condition N)
Code to execute if all previous conditions are false but condition N is true;
else
Code to execute if all conditions are false;
Percentage Grade
80% and above A
70% - 80% B
60% - 70% C
50% - 60% D
Below 50% F
Structure of if statement:
If statement has the following structure in C language
if(condition)
Associated Code
Q4: Identify errors in the following code segments. Assume that variables have already
been declared.
a) if ( x ≥ 10 )
printf ( “ Good ” ) ;
c) if ( a < 7 < b )
printf ( “ 7 ” ) ;
d) if ( a == b & | x == y )
flag = true ;
else
flag = false ;
Ans:
Invalid syntax of AND and OR operator &|
a) int a = 7, b = 10;
a=a+b;
if( a > 20 && b < 20 )
b=a+b
printf( “ a = % d , b = %d ” , a , b ) ;
Ans:
a = 17 , b = 10
b) int x = 45 ;
if( x + 20 * 7 == 455 )
printf( “ Look’s Good ” ) ;
else
printf(“ Hope for the Best ” ) ;
Ans:
Hope for the best
d) int a =34,b=34,c=7,d=15;
a=b+c+d;
if (a<100)
a=a*2
b=b*c;
c=c+d;
if (a>b && c==d)
{ c=d;
b=c;
a=b;
}
else
if (a>b && c>d || b >=d+c)
{ d=c*c;
a=b*b;
}
printf(“a=%d,b=%d,c=%d,d=%d”,a,b,c,d);
Created By: Bilal Ahmad 8
Ans:
a=50176,b=224,c=22,d=484
e) int x= 5,y=7,z=9;
if (x%2==0)
x++;
else
x=y +z;
printf(“x=%d\n”,x);
if ( x%2==1 && y%2==1 && z%2==1 )
printf(“All are Odd”);
if (x>y || x<z)
{
if( x>y )
y++;
else
if (x<z)
x++;
}
printf(“x=%d,y=%d,z=%d”,x,y,z);
Ans: x=16
x=16, y=8, z=9
Exercise 1
Write a program that takes two integers as input and tells whether first one is a
factor of the second one?
Ans: #include<stdio.h>
void main()
{
int n1, n2;
printf("Input the first integer:");
scanf("%d",&n1);
printf("Input the seecond integer:");
scanf("%d",&n2);
if(n2%n1==0)
printf("\n%d is a Factor of %d.\n",n1,n2);
else
printf("\n%d is not a Factor of %d.\n",n1,n2);
}
Exercise 2
Write a program that takes a number as input and displays “YES” if the input
number is a multiple of 3, and has 5 in unit’s place e.g. 15,75.
Ans: #include<stdio.h>
void main()
{
int num;
printf("Enter any Number");
scanf("%d",&num);
if(num%3==0 && num%10==5)
printf("YES");
else
printf("NO");
}
Exercise 3
Exercise 4
Write a program that takes as input, the original price and sale price of a product
and tells whether the product is sold on profit or loss. The program should also
tell the profit/loss percentage.
Ans: #include<stdio.h>
void main()
{
float cp, sp, profit, loss, loss_p, profit_p;
printf("Enter Cost price of Product: ");
scanf("%f", &cp);
printf("Enter Sale price of Product: ");
scanf("%f", &sp);
if(cp<sp)
{
printf("You Got profit!\n");
profit=sp-cp;
profit_p=profit/cp*100;
printf("Profit = %.2f\n",profit);
printf("Profit percentage is %.2f\n", profit_p);
}
else if(cp>sp)
{
printf("You got loss!\n");
loss=cp-sp;
loss_p=loss/cp*100;
Created By: Bilal Ahmad 11
printf("Loss = %.2f\n",loss);
printf("Loss percentage is %.2f\n",loss_p);
}
}
Exercise 5
Write a program that takes as input, the lengths of 3 sides of a triangle and tells
whether it is a right angle triangle or not. For a right angled triangle,
Hypotenuse2=base2+height2
Ans: #include<stdio.h>
void main()
{
float b,ht,h;
printf("Enter The Base of Triangle: ");
scanf("%f",&b);
printf("Enter The Height of Triangle: ");
scanf("%f",&ht);
printf("Enter The Hypotenuse of Triangle: ");
scanf("%f",&h);
if( h*h == b*b + ht*ht )
printf("It is Right Angle Triangle");
else
printf("It is Not a Right Angle Triangle");
}
Exercise 6
p_matric=matric/t_matric*100;
p_inter=inter/t_inter*100;
p_entr=entr/t_entr*100;
Write a program that calculates the bonus an employee can get on the following
basis:
Salary Experience Bonus Tasks Bonus
10000 2 Years 5 1500
10000 3 Years 10 2500
25000 3 Years 4 2000
75000 4 Years 7 3500
100000 5 Years 10 5000
The program should take as input, the salary, experience and number of bonus
tasks of the employee. The program should display the bonus on the screen.
Ans: #include<stdio.h>
void main()
{
int salary, exp, task, bonus;
printf("Enter Your Salary: );
scanf("%d",&salary);
printf("Enter Your Experience: ");
scanf("%d",&exp);
printf("Enter Your Number of Bonus tasks: ");
scanf("%d",&task);
Activity 3.1:
Write a program that takes the age of a person as an input and displays
“Teenager” if the age lies between 13 and 19.
Ans: #include<stdio.h>
void main()
{
int age;
printf("Please Enter Your Age: ");
scanf("%d",&age);
if(age>=13 && age<=19)
printf("Teenager");
else
printf("Not a Teenager");
}
Activity 3.2:
Write a program that takes year as input and displays “Leap Year” if the input year
is leap year. Leap years are divisible by 4.
Ans: #include<stdio.h>
void main()
{
int year;
printf("Please Enter Year: ");
scanf("%d",&year);
if(year%4==0)
printf("Leap Year");
else
printf("Not a leap year");
}
Activity 3.3:
Write a program that takes the value of body temperature of a person as an input
and displays “You have fever.” If body temperature is more than 98.6 otherwise
displays “You don’t have fever.”
Activity 3.4:
The eligibility criteria of a university for its different undergraduate programs is as
follows:
BSSE Program: 80% or more marks in Intermediate
BSCS Program: 70% or more marks in Intermediate
BSIT Program: 60% or more marks in Intermediate
Otherwise the university do not enroll a student in any of its programs. Write a
program that takes the percentage of Intermediate marks and tells for which
programs the student is eligible to apply.
Ans: #include<stdio.h>
void main()
{
float percent;
char a;
ag:
printf("Please Enter the Percentage of Intermediate Marks: ");
scanf("%f",&percent);
if(percent>=80)
printf("You are Eligible for BSSE, BSCS and BSIT\n");
else if(percent>=70)
printf("You are Eligible for BSCS and BSIT\n");
else if(percent>=60)
printf("You are Eligible for BSIT only\n");
else
printf("You are Not Eligible\n");
}
Activity 3.5:
Write a program that takes two integers as input and asks the user to enter a
choice from 1 to 4. The program should perform the operation according to the
given table and display the result.
Choice Operation
1 Addition
2 Subtraction
3 Multiplication
4 Division
if(choice==1)
printf("Addition is %d", a+b);
else if(choice==2)
printf("Subtraction is %d", a-b);
else if(choice==3)
printf("Multiplication is %d", a*b);
else if(choice==4)
printf("Division is %d", a/b);
}
Activity 3.6:
Write a program that finds and displays area of a triangle, parallelogram, rhombus
or trapezium according to the choice of user.
Ans: #include<stdio.h>
void main()
{
float length, width, height, b1, b2, area;
int choice;
printf("Select option of calculate area: \n");
printf("1:Triangle: \n2:Parallelogram: \n3:Rhombus: \n4:Trapezium: \n");
scanf("%d",&choice);
if(choice==1)
{
printf("Enter length of triangle: \n");
scanf("%f", &length);
printf("Enter width of triangle: \n");
scanf("%f", &width);
area=0.5*length*width;
printf("Area of triangle = %.2f\n",area);
}
else if(choice==2)
{
printf("Enter length of parallelogram: \n");
scanf("%f", &length);
printf("Enter width of parallelogram: \n");
scanf("%f", &width);
area=length*width;
printf("Area of parallelogram = %.2f\n",area);
}
else if(choice==3)
{
printf("Enter 1st diagnal of rhombus: \n");
Q1: A marketing firm calculates the salary of its employees according to the following
formula.
Gross Salary = Basic Salary +(Number of Items Sold X 8) + Bonus
If the number of sold items are more than 100 and the number of broken items are
0, then bonus is Rs. 10000, otherwise bonus is 0.
Write a program that takes basic salary, the number of sold and broken items as
input from user, then calculates and displays the gross salary of the employee.
Ans: #include <stdio.h>
void main()
{
int basic_salary, items_sold, items_broken, gross_salary;
int bonus;
printf(“Enter the basic salary: ”);
scanf(“%d”, &basic_salary);
printf(“Enter the number of items sold: ”);
scanf(“%d”, &item_sold);
printf(“Enter the number of items broken: ”);
scanf(“%d”, &items_broken);
if(items_sold > 100 && items_broken == 0)
bonus = 10000;
gross_salary = basic_salary + ( items_sold * 8 ) + bonus ;
printf( “ Gross salary of the employee is %d ” , gross_salary );
}
Q2: An electricity company calculates the electricity bill according to the following
formula.
Q3: Write a program that displays larger one out of the three given numbers.
Ans: #include <stdio.h>
void main()
{
int n1, n2, n3;
printf(“ Enter three numbers ”);
scanf (“%d%d%d” , &n1, &n2, &n3);
if( n1 > n2 && n1 > n3 )
printf(“ The largest number is %d ”, n1 );
else if( n2 > n3 && n2 > n1 )
printf(“ The largest number is %d ”, n2 );
else
printf(“ The largest number is %d ”, n3 );
}
Q4: Write a program that calculates the volume of cube, cylinder or sphere, according
to the choice of user.
Ans: #include <stdio.h>
void main()
{
int choice;
float volume;
printf(“Find Volume \n”);
printf(“1.Cube \n 2.Cylinder \n 3.Sphere \nEnter your choice”);
Created By: Bilal Ahmad 19
scanf(“%d” , &choice);
if( choice ==1 )
{
float length;
printf(“ Enter Length: ”);
scanf(“ %f ” , &lenth);
volume = length * length * length;
printf(“ Volume is %f ” , volume);
}
else if ( choice == 2 )
{
float length1, radius1;
printf(“ Enter Length: ”);
scanf(“ %f ” , &length1);
printf(“ Enter Raduis: ”);
scanf(“ %f ” , &radius1);
volume = 3.142 * radius1 *radius * length1;
printf(“ Volume is %f ” , volume );
}
else if( choice == 3 )
{
float radius;
printf(“Enter Radius: ”);
scanf(“ %f ” , &radius);
volume = 3.142 * radius * radius * radius ;
printf(“ Volume is %f ” , volume );
}
else
printf(“ Invalid Choice ”);
}