10th Class Com Science Notes 2024 CH 2
10th Class Com Science Notes 2024 CH 2
OPERATORS
* PROGRAMMING TIME 52
* SOLVED ACTIVITIES 54
* EXERCISE 57
* PROGRAMMING EXERCISES 64
2.1.1 PRINTF ()
2.1.2 FORMAT SPECIFIERS
2.1.3 SCANF ()
2.1.4 GETCH ()
2.1.5 STATEMENT TERMINATOR
2.1.6 ESCAPE SEQUENCE
LONG QUESTIONS
1. Explain printf ( ) function with some examples. (K.B+U.B+A.B)
Ans. printf is a built-in function in ‘C’ programming language to show output on screen. Its
name comes from “Print formatted” that is used to print the formatted output on screen.
All data types can be displayed with printf function.
Syntax:
printf (control string, list of arguments) The control string consists of the following:
Text
The format specifier
The escape sequence.
Text is written within double quotes. It specifies the message that is to be printed along
with the values. Format specifier specifies the format according to which a value is to be
printed. Escape sequence controls the printing on the output device. List of arguments
consists of a list of variables or arithmetic expressions, separated by commas, whose values
are to be printed. The values are printed according to the corresponding format specifier. The
first format specifier applies to the first argument, the second to the second argument and so
on. The arguments the printf ( ) function are optional. When printf ( ) function is used to
print only a message, then the arguments are omitted.
Example:
# include <stdio. h>
void main ()
{
printf (“Hello World”);
}
Output:
Hello World
In this example, printf function is used to display Hello World on computer screen.
Whatever we write inside the double quotes (“ ”) in the printf () function, gets displayed
on computer screen.
2. Explain format specifier in C language in detail. (K.B+U.B)
Ans: A format specifier is computer code that tells about the data type, field width and the
format according to which a value is to be printed or read from an input device. A list of
commonly used format specifiers is given below.
%d (decimal integer)
%i (integer)
%1d (long decimal integer)
COMPUTER SCIENCE-10 32
Chapter – 2 User Interface
COMPUTER SCIENCE-10 35
Chapter – 2 User Interface
COMPUTER SCIENCE-10 37
Chapter – 2 User Interface
#include <stdio.h>
int main( )
{
int i = -3;
int k = i % 2’
printf(“%d\n”, k);
}
(A) Compile Time Error (B) -1
(C) 1 (D) Implementation Defined
16. What will be the output of following ‘C’ code? (K.B+U.B+A.B)
#include <stdio.h>
int main( )
{
int i = 5;
i = i / 3;
printf(“%d/n”, i);
return 0;
}
(A) Compile Time Error (B) 3
(C) 1 (D) Implementation Defined
17. A symbol used in ‘C’ with scanf( ) function: (K.B+A.B)
(A) # (B)! (C) & (D) i
18. Which one of the following symbol is used as a statement terminator? (K.B)
(A) : (B) # (C) ; (D) }
19. A computer code that tells about data type field width and function is called ______.(K.B)
(A) Reserve Word (B) Format Specifier (C) Escape Sequence (D) String Field
20. Which format specifier is used for decimal integer data? (K.B+A.B)
(A) % (B) % Id (C) %d (D) %C
21. A format specifier use for integer________. (K.B+A.B)
(A) %d (B) i% (C) %f (D) %c
22. A format specifier use for long decimal integer ________. (K.B+A.B)
(A) %d (B) %i (C) %id (D) %f
23. Format specifier used for string variable ________. (K.B+A.B)
(A) %s (B) %c (C) %e (D) %g
24. A format specifier used for floating point exponential notation ________. (K.B+A.B)
(A) %e (B) %g (C) %c (D) %f
25. A format specifier is used for single character ________. (K.B+A.B)
(A) %c (B) %s (C) %e (D) %
26. The special character used in C language to control printing on output devices are called
________. (K.B+A.B)
(A) Format Specifier (B) Escape (C) String Format (D) Control String
27. Which Escape sequence is used to produce a test (bell) sound? (K.B+A.B)
(A) \a (B) \b (C) \n (D) \r
28. A escape sequence used to move cursor back word by one position. ______. (K.B+A.B)
(A) \b (B) \n (C) \r (D) \t
29. Which escape sequence is used to move cursor to the beginning? (K.B+A.B)
COMPUTER SCIENCE-10 39
Chapter – 2 User Interface
COMPUTER SCIENCE-10 43
Chapter – 2 User Interface
Ans: DIFFERENTIATION
Following are the differences between relational and logical operators:
Relational Operator Logical Operator
Relational operators are used to Logical operators are used for building
compare two values of the same types. compound conditions.
After evaluation of a relational These operator always evaluates results in
expression, the result produced is either the form of true and false.
True or False.
Relational operator include ==, !=, <, Logical operators include, AND (&&), OR
>, <= and >=. (||), and NOT (!).
Q.8 Enlist name of Relational operators. (K.B)
Ans: RELATIONAL OPERATORS
Relational Operator Description
== Equal to
!= Not equal
> Greater than
< Less than
>= Greater than equal to
<= Less than equal to
Q.9 Enlist name of logical operators. (K.B)
Ans: LOGICAL OPERATORS
There are three types of logical operators. These are described below.
Operator Definition
&& AND
|| OR
! NOT
Q.10 Draw Table for AND Operator. (K.B)
Ans: AND OPERATOR
Expression Result
FALSE && FALSE FALSE
FALSE && TRUE FALSE
TRUE && FALSE FALSE
TRUE && TRUE TRUE
Q.11 Draw Table for OR Operator. (K.B)
Ans: OR OPERATORS
Expression Result
FALSE || FALSE FALSE
FALSE || TRUE TRUE
TRUE || FALSE TRUE
TRUE || TRUE TRUE
Q.12 Draw Table for NOT Operator. (K.B)
COMPUTER SCIENCE-10 44
Chapter – 2 User Interface
COMPUTER SCIENCE-10 46
Chapter – 2 User Interface
#include <stdio.h>
int main( )
{
int y = 3;
int x = 5 % 2 * 3 / 2;
printf(:Value of x is %d:, x);
}
(A) Value of x is 1 (B) Value of x is 2
(C) Value of x is 3 (D) Compile Time Error
16. The precedence of arithmetic operators is (from Highest to Lowest): (K.B+U.B+A.B)
(A) %, *, /, +, - (B) %, +, /, *, - (C) +, -, %, *, / (D) %, +, -, *, /
17. Which one of the following is not an arithmetic operation? (K.B+U.B+A.B)
(A) a*=10; (B) a/=10; (C) a != 10; (D) a%=10;
18. Which one of the following data type will throw an error on modulus operation (%)?(K.B+U.B+A.B)
(A) Char (B) Short (C) int (D) float
19. Which one of the following are the fundamental arithmetic operators?(K.B+U.B+A.B)
(A) +, - (B) +,-,% (C) +,-,*,/ (D) +,-,*,/,%
20. What will be the output of following ‘C’ code? (K.B+U.B+A.B)
#include <stdio.h>
int main( )
{
int a = 10;
double b = 5.6;
int c;
c = a + b;
printf(“%d”, c);
}
(A) 15 (B) 16 (C) 15.6 (D) 10
21. What will be the output of following ‘C’ code? (K.B+U.B+A.B)
#include <stdio.h>
int main( )
{
int a = 10, b = 5 , c = 5;
int d;
d = a == (b+c);
printf(“%d”, d);
(A) 1 (B) 5 (C) 5 (D) 25
22. What will be the output of following ‘C’ code? (K.B+U.B+A.B)
#include <stdio.h>
void main( )
{
int x = 1, y= 0,, z=5;
int a = x && y | | z++;
printf(“%d”, z);
}
(A) 6 (B) 5 (C) 0 (D) Varies
23. What will be the output of following ‘C’ code? (K.B+U.B+A.B)
COMPUTER SCIENCE-10 47
Chapter – 2 User Interface
#include <stdio.h>
void main( )
{
int x = 1, z= 3;
int y = x << 3;
printf(“%d\n”, y);
}
(A) -2147483648 (B) -1 (C) 1 (D) Runtime Error
24. Result of a logical or relational expression in C language is: (K.B+U.B+A.B)
(A) True or False
(B) 0 or 1
(C) 0 If Expression is False and any Positive Number if Expression is True
(D) Both a & b
25. What will be the value of d in the following C program? (K.B+U.B+A.B)
#include <stdio.h>
int main( )
{
int a = 10, b = 5 , c = 5;
int d
d = b + c == a;
printf(“%d”, d);
}
(A) 1 (B) 10 (C) 10 (D) 25
26. Which among the following is NOT a logical or relational operator? (K.B+U.B+A.B)
(A)! = (B) == (C) || (D) =
27. Relational operators cannot be used on: (K.B+U.B+A.B)
(A) Structure (B) Long (C) String (D) float
28. What will be the output of following C code? (K.B+U.B+A.B)
#include <stdio.h>
int main( )
{
int x = 2, y = 0;
int z = (y++) ? 2 : y == 1 && x ;
printf(“%d\n”, z);
return 0;
}
(A) 0 (B) 1 (C) 2 (D) Undefined Behavior
29. What will be the output of following C code? (K.B+U.B+A.B)
#include <stdio.h>
int main( )
{
int y = 2;
int z = y +(y = 10);
printf(“%d\n”, z);
}
(A) 12 (B) 20 (C) 4 (D) Either 12 or 20
30. What will be the output of following C code? (K.B+U.B+A.B)
COMPUTER SCIENCE-10 48
Chapter – 2 User Interface
#include <stdio.h>
void main( )
{
int b = 5 & 4 & 6;
printf(“%d”, b);
}
(A) 3 (B) 4 (C) 5 (D) 6
31. What will be the output of following C code? (K.B+U.B+A.B)
#include <stdio.h>
void main( )
{
int b = 5 & 4 | 6;
printf(“%d”, b);
}
(A) 0 (B) 4 (C) 1 (D) 6
32. What will be the output of following C code? (K.B+U.B+A.B)
#include <stdio.h>
void main( )
{
int b = 5 + 7 * 4- 9 * (3, 2);
printf(“%d”, b);
}
(A) 6 (B) 13 (C) 15 (D) 21
33. What will be the output of following C code? (K.B+U.B+A.B)
#include <stdio.h>
void main( )
{
int h = 8;
int b = 4 * 6 + 3 * 4 < 3 ? 4 : 3;
printf(“%d\n”, b);
}
(A) 3 (B) 33 (C) 34 (D) 11
34. What will be the output of following C code? (K.B+U.B+A.B)
#include <stdio.h>
void main( )
{
int a = 2 + 3 - 4 + 8 – 5 % 4;
printf(“%d\n”, a);
}
(A) 0 (B) 8 (C) 9 (D) 11
35. What will be the output of following C code? (K.B+U.B+A.B)
COMPUTER SCIENCE-10 49
Chapter – 2 User Interface
#include <stdio.h>
void main( )
{
chr a = ‘0’;
chr b = ‘m’;
int c = a && b | | ‘1’;
printf(“%d\n”, c);
}
(A) 0 (B) 1 (C) a (D) m
36. What will be the output of following C code? (K.B+U.B+A.B)
#include <stdio.h>
void main( )
{
chr a = ‘A’;
chr b = ‘B’;
int c = a + b % 3 - * 2;
printf(“%d\n”, c);
}
(A) 58 (B) 59 (C) 64 (D) 65
37. The variable on which the operations performed are called _______. (K.B+U.B)
(A) Product (B) Expression (C) Operands (D) Operators
38. _____ operators gives the remainder. (K.B+U.B)
(A) Division (B) Modulus (C) Integral (D) None of these
39. If both operands are type the remainder is ______ to give answer in integer. (K.B+U.B)
(A) Repeated (B) Truncated (C) Both A & B (D) None of these
40. The symbol for Modulus operator in C is ________. (K.B+U.B)
(A) * (B) % (C) ! (D) Wood
41. The symbol for multiplication in C is _______. (K.B+U.B)
(A) X (B) x (C) * (D) None of these
42. Relational operators are used to perform operations on _________. (K.B+U.B)
(A) Numeric (B) Characters (C) Strings (D) Both A & B
43. _______ can also be used to show result of relational expression. (K.B+U.B)
(A) printf( ) (B) scanf ( ) (C) Both A & B (D) None of these
44. The symbol used for ‘Equal To’ operators is ______. (K.B+U.B+A.B)
(A) = (B) == (C) Both A & B (D) None of these
45. The symbol used for ‘Not Equal To’ operator is _______. (K.B+U.B+A.B)
(A)!= (B) \= (C) ≠ (D) None of these
46. In AND operator’s expression FALSE & FALSE results. (K.B+U.B+A.B)
(A) True (B) False (C) Can be both (D) None of these
47. FALS || FALSE always return ____ in OR operator. (K.B+U.B)
(A) False (B) True
(C) can be true or false (D) None
48. ____ is also known as invertor. (K.B)
(A) AND (B) NOT (C) OR (D) None of these
49. The operator which is applied on single operands called ______. (K.B)
(A) Unity Operator (B) Unary operator (C) Binary (D) None of these
50. ____ operator require three operands. (K.B)
COMPUTER SCIENCE-10 50
Chapter – 2 User Interface
ACTIVITY 2.1
Write down the output of following code:
#include <stdio.h>
void main ()
{
printf ("I am UPPERCASE and this is lowercase");
}
Output: I am UPPERCASE and this is lowercase
ACTIVITY 2.2
Write a program that shows your first name in Uppercase and you last name in lower case
letters on screen
Solution:
#include <stdio.h>
void main()
{
printf("IMRAN khan");
}
Output: IMRAN khan
ACTIVITY 2.3
Write a program that takes roll number, percentage of marks and grade form user as input.
Program should display the formatted output like following:
Roll No : input value
Percentage : input value%
Grade : input value
ACTIVITY 2.4
Write a program that takes as input the length of one side of a square and calculates the area of
square.
Solution:
# include <stdio.h>
void main ()
{
int Area, Side;
printf ("enter the length of one side = ");
scanf ("%d”, & Side);
Area = Side * Side; // calculate area of square
printf ("Area of square = %d" , Area);
}
ACTIVITY 2. 5
COMPUTER SCIENCE-10 54
Chapter – 2 User Interface
Write a program that takes as input the number of balls in jar A and the number of balls in jar
B. The program calculates and displays the total number of balls.
Solution:
# include <stdio.h>
void main ()
{
int jar_A, jar_B, Sum;
printf ("Enter number of balls in Jar A = ");
scanf ("%d", & jar_A);
printf ("Enter number of ball in Jar B = ");
scanf ("%d", & jar_B);
Sum = jar_A + jar_B;// calculate sum Write print (“sum of jar A and jar B= %d”,sum);
printf("Sum of jar A and jar B = %d" , Sum);
}
ACTIVITY 2.6
Write a program should display the original price of shirt, discount on price and price after
discount.
Solution:
# include <stdio.h>
void main ()
{
int original_prince, price, discount_pre; float discounted_price;
printf ("Enter the original price of shirt ");
scanf ("%d", &original_prince);
printf ("enter the discount percentage ");
scanf ("%d", &discount_pre);
discounted_price = original_prince * discount_pre /100;
printf ("original price of shirt = %d discount on price %d and price after discount = %d",
original_prince, discount_pre, discounted_price);
}
ACTIVITY 2.7
Write a program that takes 2 digit number from user, computers the product of both digits and
show the output.
Solution:
# include <stdio.h>
void main ()
{
int num, rem, prod =1;
printf ("Enter a number " );
scanf ("%d" , &num);
while (num!=0)
{
rem = num % 10; // get the right most digit
prod = prod * rem; // calculate product of digits
num = num /10; // remove the right most digit
}
printf ("%d", prod);
}
COMPUTER SCIENCE-10 55
Chapter – 2 User Interface
ACTIVITY 2.8
Write a program that takes second as input and calculates equivalent number of hours, minutes
and seconds.
Solution:
#include <stdio.h>
void main()
{
int sec, hour, min, S;
printf ("input seconds:");
scanf ("%d", & sec);
hour = sec/3600;
min = (sec-/3600 * hour)160;
s= (sec - (3600*hour) - (min *60))
printf ("hour: min:s-;%d: %d:%d\n", hour, min, s);
}
ACTIVITY 2.9
Convert the following algebraic expressions into C expressions.
x 6y z x 6* y z
x yz 3 3 y x y * z * z * 3* y
y2
z x z x y * y / 3* x
3x
z x 2 3 y x * x 2* x * 2 2* 2 3* y
2
3z
x 3* z / 2 z * z * z x / z
x
y x z3
2 x
ACTIVITY 2.10
Consider the variable x = 3, y =7. Find out the Boolean result of following expression.
(2 + 5) > y (x + 4) == y
x! = (y - 4) (y / 2) > = x
-1 < x (x * 3) < = 20
ACTIVITY 2.11
Assume the following variable values x 4, y = 7 z = 8. Find out the resultant expression.
x = = 2 || y = = 8 7 > = y && z < 5
z > = 5 || x < = -3 y = = y && !(true)
x! = y || y < 5 !(z > x)
ACTIVITY 2.12
Find out the results of the following expression:
Expression Result
16 / (5 + 3) 2
7 + 3 * (12 + 12) 79
25 %3 * 4 4
34 – 9 * 2 / (3 * 3) 32
18 / (15 – 3 * 2) 2
COMPUTER SCIENCE-10 56
Chapter – 2 User Interface
EXERCISE
Q1. Multiple Choice Questions.
1) ‘printf’ is used to print __________ type of data. (U.B+A.B)
A) int B) float C) char D) All of these
2) ‘scanf’ is a __________ in C programming language. (U.B+A.B)
A) Keyword B) Library C) Function D) None of these
3) getch() is used to take __________as input from user. (U.B+A.B)
A) int B) float C) char D) All of these
4) Let the following part of code, what will be the value of variable ‘a’ after execution:
int a = 4; (U.B+A.B)
float b = 2.2;
a = a * b;
A) 8.8 B) 8 C) 8.0 D) 8.2
5) Which one of the following is a valid line of code. (U.B+A.B)
A) int = 20; B) grade = ‘A’;
C) line = this is a line; D) none of these
6) Which operator has highest precedence among the following: (K.B+U.B)
A) / B) = C) > D) !
7) Which one of the following is not a type of operator: (K.B+U.B)
A) Arithmetic operator B) Relational operator
C) Check operator D) Logical operator
8) The operator % is used to calculate __________. (K.B+U.B)
A) Percentage B) Remainder C) Factorial D) Square
9) Which one of the following is a valid character: (K.B+U.B)
A) here B) “a” C) ‘9’ D) None of these
10) What is true about C language: (K.B+U.B)
A) C is not a case sensitive language
B) Keywords can be used as variable names
C) All logical operators are binary operators
D) None of them
ANSWER KEY
1 2 3 4 5 6 7 8 9 10
D C C B B D B B C D
Q2. True or False
1) Maximum value that can be stored by an integer is 32000. (K.B) T/F
2) Format specifiers begin with a ‘%’ sign. (K.B) T/F
3) Precedence of division operator is greater than multiplication operator. (K.B) T/F
4) getch is used to take all types of data input from user. (K.B+A.B) T/F
5) scanf is used for output operations. (K.B+A.B) T/F
Q3. Define the following. (K.B)
1) Statement Terminator
Ans: STATEMENT TERMINATOR
A statement terminator is an identifier for compiler which identifies end of a line. In C
language semicolon (;) is used as statement terminator. If we do not end each statement
with a statement terminator it result into error. Error: Missing statement
COMPUTER SCIENCE-10 57
Chapter – 2 User Interface
2) Format Specifier
Ans: FORMAT SPECIFIER
A format specifier is computer code that tells about the data type, field width and the
format according to which a value is to be printed or read from an input device
A list of commonly used format specifiers is given below:
%d decimal integer
%i integer
%ld long decimal integer
%f floating-point (decimal notation)
%g floating-point (exponential notation)
%e floating-point (%f of %g, whichever is shorter)
%c single character
%s string
3) Escape Sequence
Ans: ESCAPE SEQUENCE
The special characters used in C language to control printing on the output device are
called escape sequences. Escape sequences are used in print function inside the “and.”
they force printf() to change its normal behavior of showing output. These characters are
not printed. These are used inside the control string.
4) scanf()
Ans: SCANF () FUNCTION
scanf ()is a built-in function in C language that takes input from user into the variables.
We specify the expected input data type in scanf function with the help of format
specifier. If user enters integer data type, format specifier mentioned in scanf must be %d
or %i.
Syntax:
scanf (“control string”, list of variables);
5) Modulus Operator
Ans: MODULUS OPERATOR
Modulus operator (%) performs division of left operand by the right operand and returns
the remainder value after division. Modulus operator works on integer data types.
int REM = 14 % 3;
As, when we divide 14 by 3, we get a remainder of 2, so the value stored in variable REM is 2.
Q4. Briefly answer the following questions. (U.B)
1) What is the difference between scanf and getch?
Ans:
scanf( ) function getch( ) FUNCTIONS
scanf is a built-in function in C language that getch() function is used to read a character
takes input from user into the variables. We from user. The character entered by user does
specify the expected input data type in scanf not at displayed on screen. This function is
function with the help of format specifier. If generally used to hold the execution of
user enters integer data type, format specifier program because the program does not
mentioned in scanf must be %d or %i. continue further until the user types a key.
Syntax: Syntax:
scanf(“control string”, list of variables getch();
seperrated with commas);
COMPUTER SCIENCE-10 58
Chapter – 2 User Interface
Operator Precedence
() 1
! 2
*,/,% 3
+,- 4
>,<,>= ,<= 5
==,!= 6
&& 7
|| 8
= 9
Q5. Write down output of the following code segments.
Code Output
a. # include<stdio.h>
void main ()
{
int x = 2, y = 3, z = 6;
int ans1, ans2, ans3;
ans1= x / z * y; 079
ans2 = y + z / y * 2;
ans3 = z / x +x * y;
printf(“%d %d %d”, ans1, ans2, ans3);
}
COMPUTER SCIENCE-10 61
Chapter – 2 User Interface
b. # include<stdio.h> nn
void main ()
{ nnn
printf (“nn \n\n nnn \nn\nt\t”); n
printf (“nn /n/n nn/n/n”); t
} nn/n/n nn/n
c. # include<stdio.h>
void main ()
{
int a = 4, b;
9
float c = 2.3;
b=c*a;
printf(“%d”,b);
}
d. # include<stdio.h>
void main ()
{
5
int a = 4 * 3 / (5+1) + 7 % 4;
printf(“%d”,a);
}
e. # include<stdio.h>
void main ()
1
{
printf(“%d”,(( (5 > 3 ) && (4 > 6) ) | | (7 > 3)) );
}
Q6. Identify errors in the following code segments.
Code Identified Error/s Correct Syntax
a. # include<stdio.h> Trace error in line #3 (body printf(“Value of b is :
void main () of function) “closing” %d” , b);
{ inverted commas missing
int a , b = 13;
b = a% 2;
printf(“Value of b is :
%d , b);
}
b. # include<stdio.h> Trace error in closing brace Use curly braces }
void main () of body of function instead of that ]
{
int a , b , c,
printf(“Enter First
Number: ” );
scanf(“%d”, &a);
printf(“Enter Second
Number: ” );
scanf(“%d”, &b);
a + b = c;
]
COMPUTER SCIENCE-10 62
Chapter – 2 User Interface
COMPUTER SCIENCE-10 63
Chapter – 2 User Interface
COMPUTER SCIENCE-10 64
Chapter – 2 User Interface
Exercise 3
Write a program that displays the following output using single printf statement:
* * * *
1 2 3 4
Solution:
# include <stdio.h>
void main ( )
{
printf("*\t*\t*\t*\n1\t2\t3\t4");
}
Exercise 4
Write a program that displays the following output using single printf statement:
I am a Boy
I live in Pakistan
I am a proud Pakistani
Solution:
# include <stdio.h>
void main ( )
{
printf ("I am a boy\nI live in Pakistan\nI am a proud Pakistani");
}
Exercise 5
A clothing brand offer 15% discount on each item. A lady buys 5 shirts from this brand.
Write a program that calculate total price after discount and amount of discount availed by
the lady. Original prices of the shirts are:
Shirt1 = 423
Shirt2 = 320
Shirt3 = 270
Shirt4 = 680
Shirt5 = 520
Note: Use 5 variables to store the prices of shirts.
Solution:
# include <stdio.h>
void main( )
{
int shirt1=423, shirt2=320, shirt3=270, shirt4=680, shirt5=520;
float Total_Price, Total_Discount, Final_Price;
printf("First Shirt price= %d \t\tDiscount is %.2f \tPayable price= %.2f\n",shirt1, (shirt1*0.15),
(shirt1-(shirt1*0.15)));
printf("Second Shirt price= %d \tDiscount is %.2f \tPayable price= %.2f\n",shirt2,
(shirt2*0.15), (shirt2-(shirt2*0.15)));
printf("Third Shirt price= %d \t\tDiscount is %.2f \tPayable price= %.2f\n",shirt3,
COMPUTER SCIENCE-10 65
Chapter – 2 User Interface
(shirt3*0.15), (shirt3-(shirt3*0.15)));
printf("Fourth Shirt price= %d \tDiscount is %.2f \tPayable price= %.2f\n",shirt4,
(shirt4*0.15), (shirt4-(shirt4*0.15)));
printf("Fifth Shirt price= %d \t\tDiscount is %.2f \tPayable price= %.2f\n",shirt5,
(shirt5*0.15), (shirt5-(shirt5*0.15)));
printf("\n\n----------------------------------------------------------------------------------------------------
---------\n");
Total_Price= shirt1 + shirt2 + shirt3 + shirt4 + shirt5;
Total_Discount=(shirt1*0.15)+(shirt2*0.15)+(shirt3*0.15)+(shirt4*0.15)+(shirt5*0.15);
Final_Price = (shirt1-(shirt1*0.15))+(shirt2-(shirt2*0.15))+(shirt3-(shirt3*0.15))+(shirt4-
(shirt4*0.15))+(shirt5-(shirt5*0.15));
printf("Total price: %.2f \tTotal Discount is %.2f \tFinal price: %.2f\n",Total_Price,
Total_Discount, Final_Price);
}
Exercise 6
Write a program that swaps the values of two integer variables without help of any third variable.
Solution:
# include <stdio.h>
void main( )
{
int a, b;
printf("Enter a\n");
scanf("%d", &a);
printf("Enter b\n");
scanf("%d", &b);
a = a - b;
b = a + b;
a = b - a;
printf("After swapping, a = %.2d\n", a);
printf("After swapping, b = %.2d", b);
}
Exercise 7
Write a program that takes a 5-digit number as input, calculates and displays the sum of
first and last digit of number.
Solution:
# include <stdio.h>
void main( )
{
int number;
printf("Enter a number with length 5 digits=\n");
scanf("%d",& number);
printf("The sum of first and 5th digit is=%d", (number/10000)+(number%10));
}
COMPUTER SCIENCE-10 66
Chapter – 2 User Interface
Exercise 8
Write a program that takes monthly income and monthly expenses of the user like
electricity bill, gas bill, food expense. Program should calculate the following:
Total monthly expenses
Total yearly expenses
Monthly savings
Yearly saving
Average saving per month
Average expense per month
Solution:
# include <stdio.h>
void main()
{
float monthly_income, electricity_bill, gas_bill, food_expense, monthly_Expenses,
monthly_savings;
printf("Please enter your Monthly Income=");
scanf("%f",&monthly_income);
printf("Please enter you Monthly Expenses=");
printf("\n\n\tYour Electricity Bill=");
scanf("%f",&electricity_bill);
printf("\tYour Gas Bill=");
scanf("%f",&gas_bill);
printf("\tYour Food Expense=");
scanf("%f",&food_expense);
monthly_Expenses=electricity_bill+gas_bill+food_expense;
printf("\n\n\tTotal Monthly Expenses are=%f", monthly_Expenses);
printf("\n\tTotal Yearly Expenses are=%f", (monthly_Expenses)*12);
monthly_savings =monthly_income-monthly_Expenses;
printf("\n\n\tYour Monthly savings is=%f", monthly_savings);
printf("\n\tYour Monthly savings is=%f", monthly_savings*12);
printf("\n\n\tAverage saving per month=%f", monthly_savings);
printf("\n\tAverage expense per month=%f", monthly_Expenses);
}
Exercise 9
Write a program that takes a character and number of steps as input from user. Program
should then jump number of steps from the character.
Sample output:
Enter character: a
Enter steps: 2
New character: c
COMPUTER SCIENCE-10 67
Chapter – 2 User Interface
Solution:
# include <stdio.h>
void main( )
{
char c;
int steps,x;
printf("Enter character=");
scanf("%c",&c);
printf("Enter Steps=");
scanf("%d",&steps);
x = c + steps;
printf("New Character: %c",x);
}
Exercise 10
Write a program that takes radius of a circle as input. The program should calculate and
display the area of circle.
Solution:
# include <stdio.h>
void main( )
float r, Area;
scanf(“%f”,&r);
Area = 3.14*r*r;
COMPUTER SCIENCE-10 68
Chapter – 2 User Interface
ANSWER KEY
2.1.1 PRINTF ()
2.1.2 FORMAT SPECIFIERS
2.1.3 SCANF ()
2.1.4 GETCH ()
2.1.5 STATEMENT TERMINATOR
2.1.6 ESCAPE SEQUENCE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
A D D A A C B B D C A C D A B
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
A C C B C B C A D A B A A C A
31 32 33 34 35 36 37 38
B B C B B C B A
COMPUTER SCIENCE-10 69