SlideShare a Scribd company logo
1 of 86
Download to read offline
PROGRAMMING FOR
PROBLEM SOLVING (PPS)
06Dec2022: nested if, switch, for; Lab Programs
PROBLEM SOLVING (PPS)
B.Tech I Sem CST
Dr. C. Sreedhar
Today’s Topics
 Operators in C
 Control Structures:
if
 Loops
for
do while
if
if else
if else if ladder
nested if
switch
do while
Operators in C
 Arithmetic operators
+ - * / %
Relational operators
 Bitwise operators
& | ^ ~ << >>
Assignment operators
 Relational operators
== > < >= <= !=
 Logical operators
&& "" !
 Assignment operators
= += -= *= /= %=
<<= >>= &= ^= |=
 Misc operators
sizeof() & * , ?:
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
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
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
Bitwise operators
Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< left shift
>> right shift
~ Bitwise Not
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
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
Assignment operators
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");
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).
Operator Description Associativity
()
[ ]
.

Parentheses (grouping)
Brackets (array subscript)
Member selection
Member selection via pointer
LR
++ --
+ -
Unary preincrement/predecrement
Unary plus/minus
+ -
! ~
(type)
*
&
sizeof
Unary plus/minus
Unary logical negation/bitwise
complement
Unary cast (change type)
Dereference
Address
Determine size in bytes
RL
= Assignment operator
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
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
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");
}
#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;
}
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);
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);
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);
switch
switch (expression)
{
case constant1:
stmt1;
stmt2;
break;
case constant2:
case constant2:
stmt1;
stmt2;
break;
default:
// default statements
}
Ex:1: Print choice
based on input using
switch
Input
Choices available:
1. CST
// Print choices available
//Accept number
switch (num)
{
case 1:
printf("You selected CST "); break;
case 2:
printf("You selected CSE "); break;
2. CSE
3. ECE
4. CSBS
Enter your choice: 1
Output
You selected CST
printf("You selected CSE "); break;
case 3:
printf("You selected ECE "); break;
case 4:
printf("You selected CSBS "); break;
default:
printf("Wrong choice ");
}
 Program to perform arithmetic operations on two
given numbers using swtich case
switch(op)
{
case '+':
printf("Additionn");
c=a+b;
printf("Sum=%dn",c);
break;
case '-':
printf("Subtractionn");
case '/':
printf("Divisionn");
c=a/b;
printf("Quotient=%dn",c);
break;
case '%':
printf("Remaindern");
printf("Subtractionn");
c=a-b;
printf("Difference=%dn",c);
break;
case '*':
printf("Multiplicationn");
c=a*b;
printf("Product=%dn",c);
break;
printf("Remaindern");
c=a%b;
printf("Remainder=%dn",c);
break;
default:
printf("Invalid Optionn");
break;
}
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.
Triangles: Angles
Triangles: Sides
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
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
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! ! !
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
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
If choice = 1 ie., Angles
 Equilateral triangle
 Isosceles Perpendicular triangle
Isosceles triangle
 Isosceles triangle
 Perpendicular triangle
 Scalene triangle
 It is not a triangle
If choice = 1 ie., Angles
 Equilateral triangle
a1==a2 && a2 == a3
a1==a2 && a2 == a3
a1
a2
a3
If choice = 1 ie., Angles
 Isosceles Perpendicular triangle
(a1==a2||a2==a3||a1==a3)
(a1==a2||a2==a3||a1==a3)
&&
(a1==90||a2==90||a3==90)
If choice = 1 ie., Angles
 Isosceles triangle
a1==a2||a2==a3||a1==a3
If choice = 1 ie., Angles
 Equilateral triangle
 Isosceles Perpendicular triangle
Isosceles triangle
(a1==90||a2==90||a3==90)
 Isosceles triangle
 Perpendicular triangle
 Scalene triangle
 It is not a triangle
Choice =2 ie., sides
if(s1==s2 && s2==s3) Equilateral triangle
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))
Isoceles Perpendicular triangle
Choice =2 ie., sides
 else if(s1==s2||s2==s3||s1==s3)
Isoceles triangle
else if(s1*s1==s2*s2+s3*s3 || s2*s2==s1*s1+s3*s3 ||
s3*s3==s1*s1+s2*s2)
Perpendicular triangle
Start
Print 1. Angles 2. Sides
Accept choice
ch==1
T
A
B
F
Ch=2
T
Invalid choice
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
C1
C2
P1
C3
P2
P6
T
T
T
F
F
F
F
C1
if((s1+s2>s3)&&(s2+s3>s1)&&(s1+s3>s2))
C2
if(s1==s2&&s2==s3)
P1
Equilateral Triangle
C3
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))
P2
Isosceles Perpendicular
Triangle
C4
else if(s1==s2||s2==s3||s1==s3)
P3
Isosceles Triangle
C5
else if(s1*s1==s2*s2+s3*s3||s2*s2==s1*s1+s3*s3||
s3*s3==s1*s1+s2*s2)
P4
Perpendicular triangle
P5
Scalene triangle
P6
Not a triangle
B
P2
C4
C5
P3
P4 P5
T
T
F
F
Equilateral Triangle
Triangle
Isosceles Triangle
Perpendicular triangle
Scalene triangle
Not a triangle
c
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");
}
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");
}
Lab Program 3
 Write a C program to calculate the area and
perimeter of different shapes using switch statement.
 Triangle
 Triangle
 Rectangle
 Square
 Circle
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
for(initialization; condition; incrementation)
{
code statements;
}
int main()
{
int i;
int i;
for (i=0; i<10; i++)
{
printf("i=%dn",i);
}
return 0;
}
Loop: for
for(initialization, condition, incrementation)
{
code statements;
}
int main()
{
int i;
} int i;
for (i=0; i<10; i++)
{
printf("i=%dn",i);
}
return 0;
}
Print the number format shown using for
for(i = 1; i < 5; i++)
{
printf("n");
printf("n");
for(j = i; j > 0; j--)
{
printf("%d", j);
}
}
Print the number format shown using for
int r=0, c=0;
for(r=0; r<10;r++)
{
for(c=0; c<r; c++)
{
printf(" * ");
}
printf("n");
}
Program to print its multiplication table
i=1;
while(i<=10)
{
printf("%dn",(num*i));
i++;
i=1;
do
{
printf("%dn",(num*i));
i++;
i++;
}
i++;
}while(i<=10);
for(i=1;i<=10;i++)
{
printf("%dn",(num*i));
}
Loop Example: Multiplication table
int main()
{
int n, i;
printf("Enter no. to print multiplication table: ");
scanf("%d",&n);
scanf("%d",&n);
for(i=1;i<=10;++i)
{
printf("%d * %d = %dn", n, i, n*i);
}
}
for(i=1; i<=n; i++)
{
if(i%2 == 0)
Print even numbers upto n
for(i=2; i<=n; i+=2)
{
printf("%dn",i);
if(i%2 == 0)
printf("%dn", i);
}
printf("%dn",i);
}
Print even for a given range
if(start%2 != 0)
start++;
start++;
for(i=start; i<=end; i+=2)
printf("%dn",i);
Factorial of a given no.
fact=1;
for(i=num; i>=1; i--)
fact=fact*i;
Sum of n natural no’s
sum=0;
for (i = 1; i <= n; ++i)
sum += i;
Draw flowchart and Find the output
for(i = 2; i <= 6; i = i + 2)
printf("%dt", i + 1);
printf("%dt", i + 1);
Output
3 5 7
Draw flowchart and Find the output
for(i = 2; i != 11; i = i + 3)
printf("%dt", i + 1);
printf("%dt", i + 1);
Output
3 6 9
Print even numbers upto n
for(i=1; i<=n; i++)
{
if(i%2 == 0)
for(i=2; i<=n; i+=2)
{
if(i%2 == 0)
printf("%dn", i);
}
{
printf("%dn",i);
}
Loop: while
Syntax:
while (test_expression)
while (test_expression)
{
statement/s to be executed.
}
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
Print all ODD numbers from 1 to N using while loop.
number=1;
while(number<=n)
{
{
if(number%2 != 0)
printf("%d ",number);
number++;
}
Print its multiplication table
i=1;
while(i<=10)
{
{
printf("%dn",(num*i));
i++;
}
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
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
int n,term=9,gap=4,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
for(i=1;i<=n;i++)
{
printf(" %d",term);
term=term+gap;
gap=gap+5;
}
printf("n");
rev=0;
while (n != 0)
{
Reverse of given no.
{
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
Count no. Of digits
count=0;
do {
n /= 10;
++count;
} while (n != 0);
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");
}
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%
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
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);
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);
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);
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
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
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
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
 For example 4A3B2C1D at address 100, they store
the bytes within the address range 100 through 103
in the following order:m

More Related Content

What's hot

Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File Rahul Chugh
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfsudhakargeruganti
 
Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Hazrat Bilal
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesSreedhar Chowdam
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in cMuthuganesh S
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robinAbdullah Al Naser
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 SolutionHazrat Bilal
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programmingRabin BK
 

What's hot (20)

string in C
string in Cstring in C
string in C
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
pointers
pointerspointers
pointers
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdfEasy Understanding of Structure Union Typedef Enum in C Language.pdf
Easy Understanding of Structure Union Typedef Enum in C Language.pdf
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Structures
StructuresStructures
Structures
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Oops presentation
Oops presentationOops presentation
Oops presentation
 
PPS Notes Unit 5.pdf
PPS Notes Unit 5.pdfPPS Notes Unit 5.pdf
PPS Notes Unit 5.pdf
 
User Defined Functions
User Defined FunctionsUser Defined Functions
User Defined Functions
 
Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.Printing different pyramid patterns of numbers,alphabets and stars using C.
Printing different pyramid patterns of numbers,alphabets and stars using C.
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in c
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Let us C (by yashvant Kanetkar) chapter 3 Solution
Let us C   (by yashvant Kanetkar) chapter 3 SolutionLet us C   (by yashvant Kanetkar) chapter 3 Solution
Let us C (by yashvant Kanetkar) chapter 3 Solution
 
Decision making statements in C programming
Decision making statements in C programmingDecision making statements in C programming
Decision making statements in C programming
 

Similar to Program Types of Triangles

Similar to Program Types of Triangles (20)

C - programming - Ankit Kumar Singh
C - programming - Ankit Kumar Singh C - programming - Ankit Kumar Singh
C - programming - Ankit Kumar Singh
 
C operators
C operators C operators
C operators
 
Theory3
Theory3Theory3
Theory3
 
PROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptxPROGRAMMING IN C - Operators.pptx
PROGRAMMING IN C - Operators.pptx
 
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTESUnit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
Unit 1- PROGRAMMING IN C OPERATORS LECTURER NOTES
 
C Operators and Control Structures.pdf
C Operators and Control Structures.pdfC Operators and Control Structures.pdf
C Operators and Control Structures.pdf
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
C Operators and Control Structures.pptx
C Operators and Control Structures.pptxC Operators and Control Structures.pptx
C Operators and Control Structures.pptx
 
C programming(Part 1)
C programming(Part 1)C programming(Part 1)
C programming(Part 1)
 
C Programming
C ProgrammingC Programming
C Programming
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
 
COM1407: C Operators
COM1407: C OperatorsCOM1407: C Operators
COM1407: C Operators
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
C – operators and expressions
C – operators and expressionsC – operators and expressions
C – operators and expressions
 
Simple C programs
Simple C programsSimple C programs
Simple C programs
 
6 operators-in-c
6 operators-in-c6 operators-in-c
6 operators-in-c
 
Cpl
CplCpl
Cpl
 
C programming
C programmingC programming
C programming
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 

More from Sreedhar Chowdam

Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)Sreedhar Chowdam
 
DCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCPDCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCPSreedhar Chowdam
 
Data Communication and Computer Networks
Data Communication and Computer NetworksData Communication and Computer Networks
Data Communication and Computer NetworksSreedhar Chowdam
 
Data Communication & Computer Networks
Data Communication & Computer NetworksData Communication & Computer Networks
Data Communication & Computer NetworksSreedhar Chowdam
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsSreedhar Chowdam
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfSreedhar Chowdam
 
Python Programming Strings
Python Programming StringsPython Programming Strings
Python Programming StringsSreedhar Chowdam
 
Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01Sreedhar Chowdam
 
Dbms university library database
Dbms university library databaseDbms university library database
Dbms university library databaseSreedhar Chowdam
 
Er diagram for library database
Er diagram for library databaseEr diagram for library database
Er diagram for library databaseSreedhar Chowdam
 

More from Sreedhar Chowdam (20)

Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)Design and Analysis of Algorithms (Knapsack Problem)
Design and Analysis of Algorithms (Knapsack Problem)
 
DCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCPDCCN Network Layer congestion control TCP
DCCN Network Layer congestion control TCP
 
Data Communication and Computer Networks
Data Communication and Computer NetworksData Communication and Computer Networks
Data Communication and Computer Networks
 
DCCN Unit 1.pdf
DCCN Unit 1.pdfDCCN Unit 1.pdf
DCCN Unit 1.pdf
 
Data Communication & Computer Networks
Data Communication & Computer NetworksData Communication & Computer Networks
Data Communication & Computer Networks
 
Big Data Analytics Part2
Big Data Analytics Part2Big Data Analytics Part2
Big Data Analytics Part2
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, Exceptions
 
Python Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdfPython Programming by Dr. C. Sreedhar.pdf
Python Programming by Dr. C. Sreedhar.pdf
 
Python Programming Strings
Python Programming StringsPython Programming Strings
Python Programming Strings
 
Python Programming
Python Programming Python Programming
Python Programming
 
Big Data Analytics
Big Data AnalyticsBig Data Analytics
Big Data Analytics
 
Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01Computer Networks Lecture Notes 01
Computer Networks Lecture Notes 01
 
Dbms university library database
Dbms university library databaseDbms university library database
Dbms university library database
 
Er diagram for library database
Er diagram for library databaseEr diagram for library database
Er diagram for library database
 
Dbms ER Model
Dbms ER ModelDbms ER Model
Dbms ER Model
 
DBMS Notes: DDL DML DCL
DBMS Notes: DDL DML DCLDBMS Notes: DDL DML DCL
DBMS Notes: DDL DML DCL
 
GPREC DBMS Notes 1
GPREC DBMS Notes 1GPREC DBMS Notes 1
GPREC DBMS Notes 1
 
Computer Networks Unit 5
Computer Networks Unit 5Computer Networks Unit 5
Computer Networks Unit 5
 
Jp notes
Jp notesJp notes
Jp notes
 

Recently uploaded

Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 

Recently uploaded (20)

Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 

Program Types of Triangles

  • 1. PROGRAMMING FOR PROBLEM SOLVING (PPS) 06Dec2022: nested if, switch, for; Lab Programs PROBLEM SOLVING (PPS) B.Tech I Sem CST Dr. C. Sreedhar
  • 2. Today’s Topics  Operators in C  Control Structures: if  Loops for do while if if else if else if ladder nested if switch do while
  • 3. Operators in C  Arithmetic operators + - * / % Relational operators  Bitwise operators & | ^ ~ << >> Assignment operators  Relational operators == > < >= <= !=  Logical operators && "" !  Assignment operators = += -= *= /= %= <<= >>= &= ^= |=  Misc operators sizeof() & * , ?:
  • 4.
  • 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
  • 8. Bitwise operators Operator Description & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR << left shift >> right shift ~ Bitwise Not
  • 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).
  • 14. Operator Description Associativity () [ ] .  Parentheses (grouping) Brackets (array subscript) Member selection Member selection via pointer LR ++ -- + - Unary preincrement/predecrement Unary plus/minus + - ! ~ (type) * & sizeof Unary plus/minus Unary logical negation/bitwise complement Unary cast (change type) Dereference Address Determine size in bytes RL = Assignment operator
  • 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);
  • 22.
  • 23. switch switch (expression) { case constant1: stmt1; stmt2; break; case constant2: case constant2: stmt1; stmt2; break; default: // default statements }
  • 24.
  • 25. Ex:1: Print choice based on input using switch Input Choices available: 1. CST // Print choices available //Accept number switch (num) { case 1: printf("You selected CST "); break; case 2: printf("You selected CSE "); break; 2. CSE 3. ECE 4. CSBS Enter your choice: 1 Output You selected CST printf("You selected CSE "); break; case 3: printf("You selected ECE "); break; case 4: printf("You selected CSBS "); break; default: printf("Wrong choice "); }
  • 26.  Program to perform arithmetic operations on two given numbers using swtich case
  • 27. switch(op) { case '+': printf("Additionn"); c=a+b; printf("Sum=%dn",c); break; case '-': printf("Subtractionn"); case '/': printf("Divisionn"); c=a/b; printf("Quotient=%dn",c); break; case '%': printf("Remaindern"); printf("Subtractionn"); c=a-b; printf("Difference=%dn",c); break; case '*': printf("Multiplicationn"); c=a*b; printf("Product=%dn",c); break; printf("Remaindern"); c=a%b; printf("Remainder=%dn",c); break; default: printf("Invalid Optionn"); break; }
  • 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
  • 37. If choice = 1 ie., Angles  Equilateral triangle a1==a2 && a2 == a3 a1==a2 && a2 == a3 a1 a2 a3
  • 38. If choice = 1 ie., Angles  Isosceles Perpendicular triangle (a1==a2||a2==a3||a1==a3) (a1==a2||a2==a3||a1==a3) && (a1==90||a2==90||a3==90)
  • 39. If choice = 1 ie., Angles  Isosceles triangle a1==a2||a2==a3||a1==a3
  • 40. If choice = 1 ie., Angles  Equilateral triangle  Isosceles Perpendicular triangle Isosceles triangle (a1==90||a2==90||a3==90)  Isosceles triangle  Perpendicular triangle  Scalene triangle  It is not a triangle
  • 41. Choice =2 ie., sides if(s1==s2 && s2==s3) Equilateral triangle 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)) Isoceles Perpendicular triangle
  • 42. Choice =2 ie., sides  else if(s1==s2||s2==s3||s1==s3) Isoceles triangle else if(s1*s1==s2*s2+s3*s3 || s2*s2==s1*s1+s3*s3 || s3*s3==s1*s1+s2*s2) Perpendicular triangle
  • 43. Start Print 1. Angles 2. Sides Accept choice ch==1 T A B F Ch=2 T Invalid choice
  • 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
  • 45. C1 C2 P1 C3 P2 P6 T T T F F F F C1 if((s1+s2>s3)&&(s2+s3>s1)&&(s1+s3>s2)) C2 if(s1==s2&&s2==s3) P1 Equilateral Triangle C3 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)) P2 Isosceles Perpendicular Triangle C4 else if(s1==s2||s2==s3||s1==s3) P3 Isosceles Triangle C5 else if(s1*s1==s2*s2+s3*s3||s2*s2==s1*s1+s3*s3|| s3*s3==s1*s1+s2*s2) P4 Perpendicular triangle P5 Scalene triangle P6 Not a triangle B P2 C4 C5 P3 P4 P5 T T F F Equilateral Triangle Triangle Isosceles Triangle Perpendicular triangle Scalene triangle Not a triangle c
  • 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
  • 50.
  • 51. for(initialization; condition; incrementation) { code statements; } int main() { int i; int i; for (i=0; i<10; i++) { printf("i=%dn",i); } return 0; }
  • 52. Loop: for for(initialization, condition, incrementation) { code statements; } int main() { int i; } int i; for (i=0; i<10; i++) { printf("i=%dn",i); } return 0; }
  • 53. Print the number format shown using for for(i = 1; i < 5; i++) { printf("n"); printf("n"); for(j = i; j > 0; j--) { printf("%d", j); } }
  • 54. Print the number format shown using for int r=0, c=0; for(r=0; r<10;r++) { for(c=0; c<r; c++) { printf(" * "); } printf("n"); }
  • 55. Program to print its multiplication table i=1; while(i<=10) { printf("%dn",(num*i)); i++; i=1; do { printf("%dn",(num*i)); i++; i++; } i++; }while(i<=10); for(i=1;i<=10;i++) { printf("%dn",(num*i)); }
  • 56. Loop Example: Multiplication table int main() { int n, i; printf("Enter no. to print multiplication table: "); scanf("%d",&n); scanf("%d",&n); for(i=1;i<=10;++i) { printf("%d * %d = %dn", n, i, n*i); } }
  • 57. for(i=1; i<=n; i++) { if(i%2 == 0) Print even numbers upto n for(i=2; i<=n; i+=2) { printf("%dn",i); if(i%2 == 0) printf("%dn", i); } printf("%dn",i); }
  • 58. Print even for a given range if(start%2 != 0) start++; start++; for(i=start; i<=end; i+=2) printf("%dn",i);
  • 59. Factorial of a given no. fact=1; for(i=num; i>=1; i--) fact=fact*i;
  • 60. Sum of n natural no’s sum=0; for (i = 1; i <= n; ++i) sum += i;
  • 61. Draw flowchart and Find the output for(i = 2; i <= 6; i = i + 2) printf("%dt", i + 1); printf("%dt", i + 1); Output 3 5 7
  • 62. Draw flowchart and Find the output for(i = 2; i != 11; i = i + 3) printf("%dt", i + 1); printf("%dt", i + 1); Output 3 6 9
  • 63. Print even numbers upto n for(i=1; i<=n; i++) { if(i%2 == 0) for(i=2; i<=n; i+=2) { if(i%2 == 0) printf("%dn", i); } { printf("%dn",i); }
  • 64. Loop: while Syntax: while (test_expression) while (test_expression) { statement/s to be executed. }
  • 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++; }
  • 67. Print its multiplication table i=1; while(i<=10) { { printf("%dn",(num*i)); i++; }
  • 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
  • 71.
  • 72.
  • 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