Page 10th Computer Ch-2 Exercise Half
Page 10th Computer Ch-2 Exercise Half
Q.5
a) #include<stdio, h>
Output:
void main() 0,7,9
{
int x = 2, y = 3, z = 6;
int ans1 , ans2 ,ans3;
ans1 = x / z * y;
ans2 = y + z / y * 2;
ans3 = z / x + x * y;
printf('%d %d %d' , ans1 , ans2 , ans3);
b) #include<stdio,h>
Output:
void main() nn
{
printf('nn lnln nnn Inn Int It"); nnn
printf("nn /n/n nn/n\n"); n
t nn /n/n nn/n
(Computer 10th) USER INTERACT/ON (E-Series) 45
c) #include<stdio.h> Output:
void main() 9
{
int a =4, b;
float c = 2.3;
b = c * a;
printf("%d", b);
d) #include<stdio. h>
void main()
{
int a = 4 • 3 / ( 5 + 1 ) + 7 % 4;
printf("%d", a);
}
e) #include<stdio.h>
void main()
{
b)
Error:
Semicolon is m1ss1ng after int
variable. Invalid syntax of a + b = c;
; b, c, The correct syntax Is c=a+b;
tf("Enter First Number:');
-·scanf("%d", &a);
printf("Enter second number:');
scanf("%d', &b);
a + b = c;
}
c) #include<stdio.h>
Error:
void main() Double quotes missing in printf
{ statement and } has extra ;
int num;
printf(Enter Number:");
scanf(%d, &num);
};
/Computer 10th) USER INTERACTION (E-Series) 46
d) #include<stdio.h>
Error:
void main()
Square bracket is used instead of
{ parentheses. In printf statement float variable
float f; is used with %f in scant instead of %c.
printf['Enter value:");
scanf('%c", &f);
Programrrnng Exercises
Exercise 1
The criteria for calculation of wages in a company are given below:
Basic Salary = Pay Rate Per Hour X Working Hours of Employ
Overtime Salary = Overtime Pay Rate X Overtime Hours of Emp ...
Total Salary = Basic Salary + Overtime Salary
void main()
{
int wh,oh,rate_per_hour,oJi:
printf("Enter Working Ho
scanf("%d",&wh); /4
printf("Enter ove
scanf("%d",&o
printf("Enter urs:");
jler_hour);
Time rate per hour:");
citrate);
ry = wh*rete_per_hour;
_salary = oh*otrate;
.. _salary = basic_salary+overtlme_salary;
printf("Total salary is= %d",total_salary);
Output
Enter Working Hours of Employ: 20
Enter overtime working hours: 10
Enter rate per hours: 20
Enter over Time rate per hour: 1 O
Total Salary is = 500
(Computer 10th) USER INTERACTION (E-Series) 47
Exercise 2
Write a program that takes Celsius temperature as input, converts the temperature into
Fahrenheit and shows the output Formula for conversion of temperature from Celsius to
Fahrenheit is:
F=2.C+32
5
Program
#include<stdio.h>
int main()
Output
Enter temperature in Celsius: 37
Celsius = 37.00 Fahrenheit = 98.60
Exercise 3
. .
Write program that display the foll
2
Program
#Include <sldlo.h>
Int main()
{
Output
2 3 4
(Computer 10th} Exercise USER INTERACTION (E-Series) 48
4
Write a program that displays the following output using single printf statement:
l am aBoy
I live in Pakistan
I am a Proud Pakistani
Program
#include <stdio.h>
int main()
{
printf("I am a boy In I live in Pakistan In I am a Proud Pakistani");
Output
I amBoy
I live in Pakistan·
I am a Proud Pakistani
Exercises
A clothing brand offers 15% discount on ea lady buys 5 shirts from this brand. Write a
program that calculates total price aft
Original prices of the shirts are:
Shirt1 = 432
Shirt2 = 320
(Computer 10th) USER INTERACTION (E-Serles) 49
Program
#include<stdio.h>
int main()
{
int shirt1 = 423. shirt2 = 320, shirt3 = 270, shirt4 = 680, shirts = 520;
float totalprice, totaldiscount, finalprice;
printf("First Shirt price: %d It I t Discount is %.2f It Payable price: %.2 f ln",shirt1, (shirt1*0.15),
(shirt1-(shirt1*0.15)));
printf("Second Shirt price: %d It Discount is %.21 It Payable ln",shirt2,
(shirt2*0.15), (shirt2-(shirt2*0.15)));
printf("Third Shirt price: %d It It Discountis %.21 It Payable price:
(shirt3-(shirt3*0.15)));
printf("Fourth Shirt price: %d It Discount is %.2f It Payable
(shirt4-(shirt4*0.15)));
printf("Fifth Shirt price: %d It It Discount is %.21 It P �: %.21 ln",shirt5, (shirtS*0.15),
··
(shirt5-(shirt5*0.15)));
printf("lnln----------�
, l',,l--------ln");
totalprice = shirt1 + shirt2 + shirt3 + shiry
totaldiscount = (shirt1*0.15)+(shirt2*001 3*0.15)+(shirt4*0.15)+(shirt5*0.15);
finalprice = (shirt1-(shirt1*0.15))+ *0.15l)+(shirt3-(shirt3*0.15))+(shirt4 -
(shirt4*0.15))+(shirt5-(shirt5*
printf("Total price: %.21 It T
totaldiscount, finalprice);
Output
First Shirt Price Discount is 63.45 Payable price: 359.55
Second Shi · / Discount is 48.00 Payable price: 272.00
Third Shirt P Discount is 40.50 Payable price: 329.50
Fourth Shirt Price-. > Discount is 102.00 Payable price: 578.00
Fifth Shirt Price Discount is 78.00 Payable price: 442.00
int main()
{
Output
Before swap a = 10 b = 20
After swap a = 20 b = 10
Exercise 7
Write a program that takes a 5 digit number a
last digit of number.
Program
#include<sldio.h>
int main()
{
Output
Enter a number with length 5 digits: 12345
The sum of first and 5th digit is: 6
(Computer 10th} Exercise USER INTERACTION (E-Series) 51
8
Write a program that takes mol)thly income and monthly expenses of the user like electricity bill,
gas bill and food expense. Program should calculate the following:
Total monthly expenses • Yearly saving
Totally yearly expenses Av!lrage saving per month
Monthly savings Average expense per month
Program
#include<stdio.h>
int main()
Output
Please enter '{I
Please enter your· thly Expenses
Your Electricity Bill 2300
Your Gas Bill 500
Your Food Expense 22000
Total Monthly Expenses are 24800
Total Yearly Expenses are 297600
Your Monthly Saving is 38200
Your Yearly Saving is 458400
Average saving per month 38200
Average expense per month 24800
{Computer 10th) Exercise USER INTERACTION (E-Series) 52
9
Write a program that takes a character and number of steps as input from user. Program should
then jump number of steps from that character.
Sample output:
Enter character: a
Enter steps: 2
Enter character: c
Pro ram
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
char c;
int steps;
printf("Enter character: ");
scanf("%c",&c);
printf("lnEnter steps: ");
scanf("%d",&steps);
int x = c + steps;
printf("lnNew Character. %c",x);
getch();
Out ut
Enter character: a
Enter steps: 2
New Character: c
Exercise 10
Write a program that takes radi
the area of circle.
Pro ram
#inciude<stdio.h>
✓
int main()
;; •
{ clrs�:, �/
float!.��:-
printf("Please enter the radius of circle: ");
scanf(" %f ",&radius);
printf("ln Area of circle is: %.21 ", 3.14"radius•radius);
getch();
Out ut
Please enter the radius of circle: 20
Area of circle is: 1256.00