The document summarizes topics covered in a programming for problem solving (PPS) class, including control structures like if/else statements, loops, and switch cases. Example programs are provided to find the largest of three numbers, determine if a character is a vowel or consonant, and identify the type of triangle based on angle or side lengths. The last example uses nested if/else and switch statements to classify triangles as equilateral, isosceles, right-angled, or scalene depending on the problem constraints.
5. int a = 10, b = 20, c = 25, d = 25;
printf(“ %d" (a + b) );
printf(“ %d" (a - b) );
printf(“%d “ (a * b) );
printf(“ %d” (b / a) );
printf(“ %d” (b % a) );
OUTPUT
30
-10
200
2
0
printf(“ %d” (b % a) );
printf(“ %d” (c % a) );
printf (“%d“ (a++) );
printf(“%d “ (a--) );
printf(“%d “ (d++) );
printf(“%d “ (++d) );
0
5
10
11
25
27
6. int a = 10, b = 100;
float c = 10.5, d = 100.5;
printf("++a = %d n", ++a);
printf("--b = %d n", --b);
++a = 11
--b = 99
printf("++c = %f n", ++c);
printf("--d = %f n", --d);
--b = 99
++c = 11.500000
--d = 99.500000
7. int a = 10, b = 4, res;
res = a++;
printf("a is %d and res is %dn", a, res);
res = a--;
printf("a is %d and res is %dn", a,res);
a is 11 and res is 10
a is 10 and res is 11
printf("a is %d and res is %dn", a,res);
res = ++a;
printf("a is %d and res is %dn", a, res);
res = --a;
printf("a is %d and res is %dn", a, res);
a is 11 and res is 11
a is 10 and res is 10
9. Bitwise operators
a b a & b a | b a ^ b ~a
0 0 0 0 0 1
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0
10. Example: Bitwise Operators
int a = 60;
int b = 13;
int c = 0;
c = a & b; printf(“%d“, c );
c = a | b; printf(“%d" , c );
a= 60 = 0011 1100
b= 13 = 0000 1101
OUTPUT
c = a & b; 0000 1100 = 12
c = a | b; 0011 1101 = 61
c = a | b; printf(“%d" , c );
c = a ^ b; printf(“%d“, c );
c = ~a; printf(“%d“, c );
c = a << 2; printf(“%d" , c );
c = a >> 2; printf(“%d“, c );
c = a | b; 0011 1101 = 61
c = a ^ b; 0011 0001 = 49
c = ~a; 1100 0011 = -61
c = a << 2; 1111 0000 =
240
c = a >> 2; 1111 =15
12. Misc Operators
sizeof() : Returns the size of the variable
& : Returns the address of a variable
* : Pointer variable
* : Pointer variable
?: : Conditional / Ternary operator
Ex:
(a>b) ? printf("a is greater") : printf("b is greater");
13. Operator Precedence
e = (a + b) * c / d;
// Print value of e
e = ((a + b) * c) / d;
// Print value of e
e = (a + b) * (c / d);
int a = 20, b = 10, c = 15, d = 5;
int e;
Value of (a + b) * c / d is : 90
Value of ((a + b) * c) / d is : 90
Value of (a + b) * (c / d) is : 90
( 30 * 15 ) / 5
(30 * 15 ) / 5
(30) * (15/5)
e = (a + b) * (c / d);
// Print value of e
e = a + (b * c) / d;
// Print value of e
Value of (a + b) * (c / d) is : 90
Value of a + (b * c) / d is : 50
(30) * (15/5)
20 + (150/5)
associativity of operators determines the direction in which an
expression is evaluated. Example, b = a;
associativity of the = operator is from right to left (RL).
15. Arithmetic Operators
Multiplication operator, Divide
by, Modulus
*, /, % LR
Add, Subtract +, – LR
Relational Operators
Less Than <
Greater than >
Less than equal to <=
1 == 2 != 3
operators == and
!= have the same
precedence, LR
Hence, 1 == 2 is
executed first
LR
Less than equal to <=
Greater than equal to >=
Equal to ==
Not equal !=
Logical Operators
AND && LR
OR || LR
NOT ! RL
executed first
16. If if else nested if
Program to find the largest of three given numbers
using if else if ladder
Program to find the largest of three given numbers
Program to find the largest of three given numbers
using nested if
17. Flow chart : Largest
of three no’s
if (a >= b)
{
if (a >= c)
printf("a");
else
printf("c");
}
}
else
{
if (b >= c)
printf("b");
else
printf("c");
}
18. #include <stdio.h>
int main()
{
double a, b, c;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &a, &b, &c);
if (a >= b)
{
if (a >= c)
printf("%.2lf is the largest number.", a);
else
printf("%.2lf is the largest number.", c);
printf("%.2lf is the largest number.", c);
}
else
{
if (b >= c)
printf("%.2lf is the largest number.", b);
else
printf("%.2lf is the largest number.", c);
}
return 0;
}
19. Largest of three no’s
if (a >= b && a >= c)
printf("%d is largest", a);
else if (b >= a && b >= c)
else if (b >= a && b >= c)
printf("%d is largest", b);
else
printf("%d is largest ", c);
20. Program to check alphabet, digit or special character
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("'%c' is alphabet.", ch);
else if(ch >= '0' && ch <= '9')
else if(ch >= '0' && ch <= '9')
printf("'%c' is digit.", ch);
else
printf("'%c' is special character.", ch);
21. Program to check vowel or consonant
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' ||ch=='u' || ch=='A'
|| ch=='E' || ch=='I' || ch=='O' || ch=='U')
{
printf("'%c' is Vowel.", ch);
}
}
else if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
printf("'%c' is Consonant.", ch);
else
printf("'%c' is not an alphabet.", ch);
28. Lab Program 2:
Program to read angles or sides of a triangle
(based on options: 1) angles, 2) sides) and print if
it is equilateral or isosceles or isosceles
it is equilateral or isosceles or isosceles
perpendicular or just perpendicular or scalene
triangle.
31. Test Case 1
Find the type of triangle based on angles or sides.
1. Angles
2. Sides
2. Sides
Enter your choice: 1
Enter the first angle: 60
Enter the second angle: 60
Enter the third angle: 60
The triangle is: Equilateral triangle
32. Test Case 2
Find the type of triangle based on angles or sides.
1. Angles
2. Sides
2. Sides
Enter your choice: 2
Enter the first side: 60
Enter the second side: 50
Enter the third side: 60
The triangle is: Isosceles triangle
33. Test Case 3
Find the type of triangle based on angles or sides.
1. Angles
2. Sides
2. Sides
Enter your choice: 3
Invalid Choice! ! !
34. Test Case 4
Find the type of triangle based on angles or sides.
1. Angles
2. Sides
2. Sides
Enter your choice: 1
Enter the first angle: 45
Enter the second angle: 90
Enter the third angle: 45
The triangle is: Isosceles Perpendicular triangle
35. Test Case 5
Find the type of triangle based on angles or sides.
1. Angles
2. Sides
2. Sides
Enter your choice: 2
Enter the first side: 70
Enter the second side: 80
Enter the third side: 90
The triangle is: Scalene triangle
36. If choice = 1 ie., Angles
Equilateral triangle
Isosceles Perpendicular triangle
Isosceles triangle
Isosceles triangle
Perpendicular triangle
Scalene triangle
It is not a triangle
44. C1
C2
P1
C3
P2
P6
T
T
T
F
F
F
F
C1
if(a1+a2+a3==180)
C2
if(a1==a2 && a2==a3)
P1
Equilateral Triangle
C3
else if((a1==a2||a2==a3||a1==a3) &&
(a1==90||a2==90||a3==90))
P2
Isosceles Perpendicular
C4
else if(a1==a2||a2==a3||a1==a3)
P3
C5
else if(a1==90||a2==90||a3==90)
P4
P5
P6
A
P2
C4
C5
P3
P4 P5
T
T
F
F
Equilateral Triangle
Isosceles Perpendicular
Triangle
Isosceles Triangle
Perpendicular triangle
Scalene triangle
Not a triangle
B
46. if(ch==1)
{
//accept three angles and store in a1,a2,a3
if(a1+a2+a3==180)
{
if(a1==a2 && a2==a3)
{
printf("The triangle is: Equilateral trianglen");
}
else if((a1==a2||a2==a3||a1==a3) && (a1==90||a2==90||a3==90))
printf("The triangle is: Isosceles Perpendicular trianglen");
printf("The triangle is: Isosceles Perpendicular trianglen");
else if(a1==a2||a2==a3||a1==a3)
printf("The triangle is: Isosceles trianglen");
else if(a1==90||a2==90||a3==90)
printf("The triangle is: Perpendicular trianglen");
else
printf("the triangle is: Scalene trianglen");
}
else printf("It is not a triangle.n");
}
47. else if(ch==2)
{
//accept three sides and store in s1,s2,s3
if((s1+s2>s3)&&(s2+s3>s1)&&(s1+s3>s2))
{
if(s1==s2&&s2==s3)
{
printf("The triangle is: Equilateral trianglen");
}
else if((s1==s2||s2==s3||s1==s3)&&(s1*s1==s2*s2+s3*s3||s2*s2==s1*s1+s3*s3||s3*s3==s1*s1+s2*s2))
printf("The triangle is: Isosceles perpendicular trianglen");
else if(s1==s2||s2==s3||s1==s3)
else if(s1==s2||s2==s3||s1==s3)
printf("The triangle is: Isosceles trianglen");
else if(s1*s1==s2*s2+s3*s3||s2*s2==s1*s1+s3*s3||s3*s3==s1*s1+s2*s2)
printf("The triangle is: Perpendicular trianglen");
else
printf("The triangle is: Scalene trianglen");
}
else printf("It is not a triangle.n");
else
{
printf("Invalid choice!!!n");
}
48. Lab Program 3
Write a C program to calculate the area and
perimeter of different shapes using switch statement.
Triangle
Triangle
Rectangle
Square
Circle
49. while(op<5)
{
switch(op)
{
case 1:// Accept b & h
a=0.5*b*h;
// Display Area
// Accept s1,s2,s3
p=s1+s2+s3;
// Display Perimeter
case 1:
case 2:
case 3:
case 2:
//Accept l
a=l*l;
p=4*l;
// Display Area , Perimeter
case 3:
// Accept r
a=3.14*r*r;
p=2*3.14*r;
// Display Area , Perimeter
case 4:
// Accept l,b
a=l*b;
p=2*(l+b);
// Display Area , Perimeter
}
}
printf("Enter your optionn");
printf("1. trianglen2. squaren3. circlen4. rectanglen5. exitn");
scanf("%d",&op);
// Display Perimeter
break;
case 3:
case 4:
// Display Area , Perimeter
break;
// Display Area , Perimeter
break;
// Display Area , Perimeter
65. Sum of digits of given number
int n, num, sum = 0, rem;
// Accept number and store in n
num = n;
while( n > 0 ) OUTPUT
Enter a number: 456
while( n > 0 )
{
rem = n % 10;
sum += rem;
n /= 10;
}
printf("Sum of digits of %d is %d", num, sum);
Enter a number: 456
Sum of digits of 456 is 15
66. Print all ODD numbers from 1 to N using while loop.
number=1;
while(number<=n)
{
{
if(number%2 != 0)
printf("%d ",number);
number++;
}
68. Lab Program
Program to generate a series of ‘N’ numbers based
on the pattern of the numbers as : 9 13 22 36 55
79
79
69. Test Case - 1
User Output
Enter the number of terms you want: 6
The series is: 9 13 22 36 55 79
Test Case - 2
User Output
Enter the number of terms you want: 10
The series is: 9 13 22 36 55 79 108 142 181 225
Test Case - 3
User Output
Enter the number of terms you want: 20
The series is: 9 13 22 36 55 79 108 142 181 225 274 328 387 451 520 594 673 757 846 940
73. rev=0;
while (n != 0)
{
Reverse of given no.
{
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
74. Count no. Of digits
count=0;
do {
n /= 10;
++count;
} while (n != 0);
75. Print the following number pattern
k = 1;
for(i=1; i<=rows; i++)
{
{
for(j=1; j<=cols; j++, k++)
{
printf("%-3d", k);
}
printf("n");
}
76. Lab Program 4
Program to read monthly salary of an employee and
calculate Income tax to be paid based on the following
criteria:
criteria:
< 100000 per year- no tax
100001 to 200000 per year- 5%
200001 to 300000 per year- 10%
300001 to 500000 per year- 20%
> 500000 per year- 30%
77. Expected Output
Enter your monthly salary: 35000
You have to pay 39000.000000/- as income tax
Enter your monthly salary: 10000
You have to pay 1000.000000/- as income tax
Enter your monthly salary: 5000
You have to pay 0.000000/- as income tax
78. ysal=12*msal;
if(ysal<0)
{
printf("Invalid Salary!!!n");
exit(0);
}
else if(ysal<=100000)
{
printf("You are exempted from Income Tax.n");
else if(ysal<=300000)
{
it=5000+(ysal-200000)*0.1;
}
else if(ysal<=500000)
{
it=15000+(ysal-300000)*0.2;
}
printf("You are exempted from Income Tax.n");
}
else if(ysal<=200000)
{
it=(ysal-100000)*0.05;
}
}
else
{
it=55000+(ysal-500000)*0.3;
}
printf("You have to pay %lf/- as income
taxn",it);
79. Accept name from the user
Method 1
char name[20];
printf("Enter your name:");
Method 2
char name[20]
printf(“Enter your name:”)
printf("Enter your name:");
scanf("%s",name);
printf("Your name is: %s",name);
printf(“Enter your name:”)
gets(name);
printf(“Your name is:”);
puts(name);
80. Accept name from the user
Method 3
#define MAX_LIMIT 20
int main()
Method 4
char name[20];
printf("Enter your name:");
int main()
{
char name[MAX_LIMIT];
printf("Enter your name:");
fgets(name,MAX_LIMIT,stdin);
printf("Your name is: %s",name);
printf("Enter your name:");
scanf("%[^n]%*c",name);
printf("Your name is: %s",name);
81.
82. Hungarian Notation
Hungarian is a naming convention for identifiers. Each identifier
would have two parts to it, a type and a qualifier.
Each address stores one element of the memory array. Each
element is typically one byte.
element is typically one byte.
For example, suppose you have a 32-bit quantity written as
12345678, which is hexadecimal.
Since each hex digit is four bits, eight hex digits are needed to
represent the 32-bit value. The four bytes are: 12, 34, 56, and 78.
There are two ways to store in memory: Bigendian and little endian
83. Endianness
The endianness of a particular computer system is
generally described by whatever convention or set of
conventions is followed by a particular processor or
conventions is followed by a particular processor or
combination of processor/architecture and possibly
operating system or transmission medium for the
addressing of constants and the representations of
memory addresses.
Often referred to as byte order
84. Big Endian storage
Big-endian: Stores most significant byte in smallest address.
The following shows how 12345678 is stored in big endian
Big Endian Storage
Address Value
1000 12
1001 34
1002 56
1003 78
85. Little Endian storage
Little-endian: Stores least significant byte in smallest
address.
The following shows how 12345678 is stored in big
endian Little Endian Storage
Little Endian Storage
Address Value
1000 78
1001 56
1002 34
1003 12
86. For example 4A3B2C1D at address 100, they store
the bytes within the address range 100 through 103
in the following order:m