Unit 2 Question PPS
Unit 2 Question PPS
** When a char type is operated with an int type char is promoted to int.
** When a float type data is operated with an int , then int is promoted to float.
** When any operand is double then another operand is also converted to double
(b) Type casting : It is also called as forced conversion. It is done when value of higher data
type is to be converted to lower data type. This conversion is under programmer’s control
, not in compiler’s control.
Demotion : float f = 3.5 ;
int I ;
I=f;
Statement I = f results in f to be demoted to type int , i.e. the fractional part of f will be lost
and I will contain 3 not 3.5 ; this is called down conversion/demotion. But result is not
correct so explicit type casting is required. Explicit type casting is done by placing the target
data type in parentheses followed by the variable name that has to be converted.
Variable 2 = (data type) variable1
e.g : float salary = 10000.00 ;
int tot_sal =0;
sal = (int ) salary
** When floating point numbers are converted to integers , the digits after decimal are
truncated.
Ques 3. What is conditional branching statement ? Explain switch case statements in
detail , with a proper example.
Ans : The conditional branching statements help to jump from one part of the program to
another depending whether a particular condition is true or false. These decision control
statements include :
(i) if statement (ii) if – else statement (iii) if – else-if statement (iv) switch statement.
A switch case statement is a multi-way decision statement that is simplified version of if –
else block. Statement blocks refer to statements lists that may contain zero or more
statements. These statements are not enclosed within opening and closing braces.
Switch statements are mostly used in following situations :
(a) When there is only one variable to evaluate in the expression.
(b) When many conditions are being tested for.
(c) Switch case is preferable over if –else if many conditions are to be tested.
Break ;
Default :
Statement block D ;
Break ;
Statement x ;
Some Rules of Switch – Case statements
(i) Control expression inside switch must be integer type variable or integral
expression
E.g : switch ( int n) , switch ( int x + 6) etc.
(ii) Each case label should be followed by a constant or a constant expression.
(iii) Every case label must have unique constant expression value.
(iv) Case labels must end with a colon.
(v) Default label can be placed anywhere in switch block. There must be only one default in
switch program.
(vi) case labels should not be logical or relational expressions.
(vii) Order in which case labels are written are not fixed.
Example 1 of switch case with break Example 2 of switch case without break
# include <stdio.h> # include <stdio.h>
# include <conio.h> Int main()
Void main() {
{ Int option =1;
char grade = ‘C ‘ Switch (option)
switch (grade) {
{ Case 1 : printf (“ I am in case 1\n”) ;
Case ‘O ’ : Case 2 : printf ( “ I am in case 2\n”) ;
Printf ( “ \n Outstanding ”) ; Default : printf (“ I am in case default ”) ;
Break ; }
Case ‘ A’ : return 0;
Printf ( “ \n Excellent ”) ; getch();
Break ; }
Case ‘B’ :
Printf ( “ \n GOOD ”) ; **********************************
Break ; Output :
Case ‘C ’ : I am in case 1
Printf ( “ \n Satisfactory ”) ; I am in case 2
Break ; I am in case default.
Case ‘F ’ :
Printf ( “ \n Fail ”) ;
Break ;
Default :
Printf ( “ \n Outstanding ”) ;
Break ;
} getch (); } // End of main()
*************************************
Output : Satisfactory
Important Programs
1. Program to find the simple interest, 2. Ramesh’s basic salary is input through the keyboard.
given principle, rate of interest and time. His dearness allowance is 40% of basic salary, and house
rent allowance is 20% of basic salary. Write a program
to calculate his Gross Salary.
#include <stdio.h> #include<stdio.
#include <conio.h> #include<conio.h>
void main() void main()
{ {
float p, r, si; float bs , da= 0.0, hra=0.0 ,gross=0.0 ;
int t; clrscr() ;
clrscr(); printf(“Enter the basic salary\n”) ;
printf("Enter the values of p,r and t\n"); scanf(“%f” ,&bs) ;
scanf ("%f %f %d", &p, &r, &t); printf(“BASIC SALARY OF RAMESH=%f\n ”,bs) ;
si = (p * r * t)/ 100.0; da = (bs*40) / 100 ;
printf ("Amount = Rs. %5.2f\n", p); hra = (bs *20) /100 ;
printf ("Rate = Rs. %5.2f%\n", r); gross = bs + da + hra ;
printf ("Time = %d years\n", t);
printf ("Simple interest = %5.2f\n", si); printf(“**************************”);
getch() ; printf(“GROSS SALRY OF RAMESH = % f ”,gross) ;
} getch() ;
}
3. Program to find the area of a circle, 4. Program to find the area of a triangle,
given the radius. given three sides a, b and c.
#include <stdio.h> #include <stdio.h>
#include <conio.h> #include <conio.h>
#include <math.h>
void main() void main()
{ {
float radius, area; float a, b, c ;
float PI = 3.14; float s =0.0 , area =0.0 ;
clrscr(); clrscr();
printf("Enter the radius of a circle\n"); printf("Enter the values of a,b and c\n");
scanf ("%f", &radius); scanf ("%f %f%f ", &a, &b, &c);
area = PI * radius * radius; s = (a + b + c) / 2; /* computes perimeter */
printf ("Area of a circle = %5.2f\n", area = sqrt ( s * (s-a) * (s-b) * (s-c)); /* compute the
area); area */
getch() ; printf ("Area of a triangle = %f\n", area);
} getch();
}
5. Program to swap the values of two variables 6. Write a C program to check whether a
without using third variable. given integer is odd or even.
#include<stdio.h> #include <stdio.h>
#include<conio.h> #include <conio.h>
void main() void main()
{ {
int a , b ; int x , remainder;
clrscr() ; clrscr();
printf(“Enter the values of a and b \n”) ; printf("Enter an integer :");
scanf(“%d %d” ,&a , &b) ; scanf ("%d", x );
printf(“Values variables before swapping are
a=%d \t b=%d: \n”, a , b) ; if (x % 2 == 0)
a=a+b; {
b=a–b; printf ("%d, is an even integer\n", x );
a=a–b; }
printf(“Values of a and b after swapping Else
are:\n”) ; {
printf(“a=%d\t\t b=%d”,a ,b) ; printf ("%d, is an odd integer\n", x);
getch(); }
} getch ();
}
7. Program for finding the sum of a five digit no. 8. Write a C program to find the largest
without using loop of three numbers.
#include<stdio.h> #include <stdio.h>
#include<conio.h> #include <conio.h>
void main() #include <math.h>
{ void main()
long int num , sum =0 , rem ; {
// Max. five digit no. can be 99999, so long int a, b, c;
int declared clrscr();
clrscr() ; printf("Enter the values of a,b and c\n");
printf(“Enter the five digit number\n”) ; scanf ("%d %d %d", &a, &b, &c);
scanf(“% ld” , &num) ; printf ("a = %d\tb = %d\tc = %d\n", a,b,c);
printf(“The five digit number=%ld\n”, if ( a > b)
num) ; {
rem1 = num%10 ; if ( a > c)
num = num /10 ; {
rem2 = num%10 ; printf ("A is the greatest among three\n");
num = num /10 ; }
rem3 = num%10 ; else
num=num/10 ; {
rem4 = num%10; printf ("C is the greatest among three\n");
num=num/10 ; }
sum = rem1 + rem2 + rem3 + rem4 + num ; }
printf(“Sum of the digits of %ld =% ld”, else if (b > c)
num ,sum) ; {
getch(); printf ("B is the greatest among three\n");
} }
else
printf ("C is the greatest among three\n");
}