SlideShare a Scribd company logo
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() & * , ?:
Programming for Problem Solving
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);
Programming for Problem Solving
switch
switch (expression)
{
case constant1:
stmt1;
stmt2;
break;
case constant2:
case constant2:
stmt1;
stmt2;
break;
default:
// default statements
}
Programming for Problem Solving
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
Programming for Problem Solving
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");
Programming for Problem Solving
Programming for Problem Solving
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);
Programming for Problem Solving
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

PDF
PPS Arrays Matrix operations
PDF
C Recursion, Pointers, Dynamic memory management
PDF
Programming For Problem Solving Lecture Notes
PDF
C Programming Storage classes, Recursion
PDF
Chapter 5 exercises Balagurusamy Programming ANSI in c
PDF
LET US C (5th EDITION) CHAPTER 2 ANSWERS
PDF
PPS Notes Unit 5.pdf
PDF
Let us c chapter 4 solution
PPS Arrays Matrix operations
C Recursion, Pointers, Dynamic memory management
Programming For Problem Solving Lecture Notes
C Programming Storage classes, Recursion
Chapter 5 exercises Balagurusamy Programming ANSI in c
LET US C (5th EDITION) CHAPTER 2 ANSWERS
PPS Notes Unit 5.pdf
Let us c chapter 4 solution

What's hot (20)

DOC
C lab-programs
PDF
Chapter 4 : Balagurusamy Programming ANSI in C
DOCX
C Control Statements.docx
PDF
Contoh soal uts struktur data
PPT
structure and union
PDF
DSC program.pdf
PDF
Python programs - first semester computer lab manual (polytechnics)
DOCX
Chapter 8 c solution
PPTX
Structure & union
PDF
LET US C (5th EDITION) CHAPTER 1 ANSWERS
PDF
Introduçãso a linguagem c
PPT
C++ oop
PDF
Implementation of oop concept in c++
PDF
Algoritma dan Pemrograman C++ (Perulangan)
PDF
Chapter 2 : Balagurusamy_ Programming ANsI in C
DOC
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
PPTX
C programming - Pointers
PDF
Introduction to c++ ppt 1
PPT
Function overloading(C++)
PDF
Algoritma Pemrograman 2
C lab-programs
Chapter 4 : Balagurusamy Programming ANSI in C
C Control Statements.docx
Contoh soal uts struktur data
structure and union
DSC program.pdf
Python programs - first semester computer lab manual (polytechnics)
Chapter 8 c solution
Structure & union
LET US C (5th EDITION) CHAPTER 1 ANSWERS
Introduçãso a linguagem c
C++ oop
Implementation of oop concept in c++
Algoritma dan Pemrograman C++ (Perulangan)
Chapter 2 : Balagurusamy_ Programming ANsI in C
Let us c (5th and 12th edition by YASHVANT KANETKAR) chapter 2 solution
C programming - Pointers
Introduction to c++ ppt 1
Function overloading(C++)
Algoritma Pemrograman 2
Ad

Similar to Programming for Problem Solving (20)

PPTX
C - programming - Ankit Kumar Singh
DOCX
Programming Fundamentals lecture 7
PDF
C language concept with code apna college.pdf
PPTX
introduction to c programming and C History.pptx
PDF
Introduction to programming c and data-structures
PPTX
Expressions using operator in c
PPT
6 operators-in-c
PPT
6 operators-in-c
PDF
Introduction to programming c and data structures
DOC
2. operator
PPTX
the refernce of programming C notes ppt.pptx
PDF
C and Data structure lab manual ECE (2).pdf
PPTX
C programming
PPTX
PROGRAMMING IN C - Operators.pptx
PPT
Token and operators
PPT
Cbasic
PPTX
Programming in C Basics
DOCX
C Programming
PPTX
C Programming Language Part 4
C - programming - Ankit Kumar Singh
Programming Fundamentals lecture 7
C language concept with code apna college.pdf
introduction to c programming and C History.pptx
Introduction to programming c and data-structures
Expressions using operator in c
6 operators-in-c
6 operators-in-c
Introduction to programming c and data structures
2. operator
the refernce of programming C notes ppt.pptx
C and Data structure lab manual ECE (2).pdf
C programming
PROGRAMMING IN C - Operators.pptx
Token and operators
Cbasic
Programming in C Basics
C Programming
C Programming Language Part 4
Ad

More from Sreedhar Chowdam (20)

PDF
DBMS Nested & Sub Queries Set operations
PDF
DBMS Notes selection projection aggregate
PDF
Database management systems Lecture Notes
PPT
Advanced Data Structures & Algorithm Analysi
PDF
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
PPTX
Design and Analysis of Algorithms Lecture Notes
PDF
Design and Analysis of Algorithms (Knapsack Problem)
PDF
DCCN Network Layer congestion control TCP
PDF
Data Communication and Computer Networks
PDF
DCCN Unit 1.pdf
PDF
Data Communication & Computer Networks
PDF
Big Data Analytics Part2
PDF
Python Programming: Lists, Modules, Exceptions
PDF
Python Programming by Dr. C. Sreedhar.pdf
PDF
Python Programming Strings
PDF
Python Programming
PDF
Python Programming
PDF
Big Data Analytics
PDF
Data Structures Notes 2021
PDF
Computer Networks Lecture Notes 01
DBMS Nested & Sub Queries Set operations
DBMS Notes selection projection aggregate
Database management systems Lecture Notes
Advanced Data Structures & Algorithm Analysi
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms (Knapsack Problem)
DCCN Network Layer congestion control TCP
Data Communication and Computer Networks
DCCN Unit 1.pdf
Data Communication & Computer Networks
Big Data Analytics Part2
Python Programming: Lists, Modules, Exceptions
Python Programming by Dr. C. Sreedhar.pdf
Python Programming Strings
Python Programming
Python Programming
Big Data Analytics
Data Structures Notes 2021
Computer Networks Lecture Notes 01

Recently uploaded (20)

PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
737-MAX_SRG.pdf student reference guides
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Sustainable Sites - Green Building Construction
PPT
Total quality management ppt for engineering students
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
PPT on Performance Review to get promotions
PPTX
additive manufacturing of ss316l using mig welding
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
737-MAX_SRG.pdf student reference guides
Fundamentals of Mechanical Engineering.pptx
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
III.4.1.2_The_Space_Environment.p pdffdf
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
R24 SURVEYING LAB MANUAL for civil enggi
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Automation-in-Manufacturing-Chapter-Introduction.pdf
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Sustainable Sites - Green Building Construction
Total quality management ppt for engineering students
Internet of Things (IOT) - A guide to understanding
PPT on Performance Review to get promotions
additive manufacturing of ss316l using mig welding
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf

Programming for Problem Solving

  • 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() & * , ?:
  • 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);
  • 23. switch switch (expression) { case constant1: stmt1; stmt2; break; case constant2: case constant2: stmt1; stmt2; break; default: // default statements }
  • 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
  • 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
  • 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);
  • 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