SlideShare a Scribd company logo
Syllabus,
PROGRAMMING FOR
PROBLEM SOLVING
(PPS)
B.Tech I Sem
Unit
1
 Introduction to Programming :
 Compilers, compiling and executing a program.
 Representation of Algorithm –
 Algorithms for finding roots of quadratic equations,
 finding minimum and maximum numbers of a given set,
 finding if a number is a prime number
 Flowchart/Pseudocode with examples,
 Program design and structured programming.
Unit
1
 Introduction to C Programming Language:
 variables (with data types and space requirements),
 Syntax and Logical Errors in compilation,
 object and executable code,
 Operators,
 expressions and precedence,
 Expression evaluation,
 Storage classes (auto, extern, static, and register),
 type conversion,
Unit
1
 The main method and command line arguments Bitwise
operations:
 Bitwise AND, OR, XOR, and NOT operators.
 Conditional Branching and Loops:
 Writing and evaluation of conditionals and consequent branching
with if, if-else, switch-case,
 ternary operator,
 goto, Iteration with for, while, do-while loops.
 I/O: Simple input and output with scanf and printf, formatted I/O,
Introduction to stdin, stdout, and stderr. Command line arguments.
Textbooks &
References
1. Jeri R. Hanly and Elliot B. Koffman, “Problem Solving and
Program Design in C,” 7th Edition, Pearson.
2. A.B. Forouzan and F.S. Gilberg, “C Programming and Data
Structures,” Cengage Learning, 3rd Edition.
C is a programming
language developed
at Bell Laboratories
of USA in 1972 by
Dennis Ritchie
 C is a programming language developed
at Bell Laboratories of USA in 1972 by
Dennis Ritchie
UNIT 1.pptx Programming for Problem Solving
Numbering
System
Decimal to
Binary
Decimal to
Binary
Decimal to
Binary
156 (10)
10011100 (2)
Binary representation (Decimal to
Binary
Decimal no.= 123
Binary no. =
1111011
Decimal no. = 255
Binary no. = 1111
1111
Binary to
Decimal
Binary to
Decimal
Binary to
Decimal
Binary to
Decimal
Binary to
Decimal
Binary no. =
110011 Decimal
no. = 51
Binary no. = 1111
0001 Decimal no. =
241
Algorith
m
Algorithm
 Algorithm is a finite set of instructions that if
followed accomplishes a particular task.
Problem
Inpu
t
Outpu
t
Comput
er
Problem: Shortest
distance Input
n nodes
connecting
nodes
dista
nce between
nodes
O
utput
Problem:
Search
Problem:
Search
Problem:
Sort
Problem:
Sort
Characteristics of
Algorithm
 Input
 Output
 Definitenes
s
 Effectivenes
s
 Finiteness
OD
euftipnui
tte−n
eAsns
a−lSghoorui
tlhdmbseh
oc
luel
da
rhaanv
de 1 or
lead to only one
meaning.
IEnfpf
euctt
i−v
eAnneas
sl
g−o
rAi
tnh
ma
lsgh
oorui
tl
dh
mh
as
hvoeu0l
dohra
v
e
ms
t
eopr
e-
bwye-
sl
lt-
edpe
fdi
ni
reedc
t
iion
pn
su,t
sw. hich should
be
independent of any programming
code
Finiteness − An algorithm must terminate
after a finite number of steps
Characteristics of
Algorithm
 Input − Should have 0 or more well-defined inputs.
 Output - Should have 1 or more well-defined outputs, and
should match the desired output.
 Definiteness −Should be clear and unambiguous.
 Effectiveness − Should have step-by-step directions, which
should be independent of any programming code.
 Finiteness − Must terminate after a finite number of steps.
Program to display “Welcome
to C”
#include<stdio.
h> int main()
{
printf(“Welcome to
C”); return 0;
}
Problem 1: Addition of two
numbers
 Input :
🞑 two numbers (num1
= 4, num2 = 6)
 Output:
🞑 Sum = 10
Algorithm
 Step 1: Start
 Step 2: Read the first
number(num1)
 Step 3: Read second
number(num2)
 Step 4: Sum  num1+num2
 Step 5: Print Sum

Sum = num1 +
num2
Program to accept two numbers from the user and
display the sum using C language
/*
Program to accept two numbers and display
the sum Programmer : --Your name--
Roll no: --your roll number--
Date of compilation:
27Jan2021 Class : B.Tech I
Sem „CST'
*/
#include<stdio.h>
/ / Main function begins
int main()
{
int num1,num2,sum;
printf("Enter first value:");
scanf("%d",&num1);
printf("Enter Second
value:");
scanf("%d",&num2);
sum=num1+num2;
printf(“Addition of %d and %d is:
%d",num1,num2,sum); return 0;
}
Problem 2: Algorithm to find the area of
rectangle
 Input :
🞑 two numbers.
(length = 8
breadth = 4)
 Output:
🞑 Area of rect = 32
Algorith
m
 Step 1: Start
 Step 2: Read first number(length)
 Step 3: Read second number
(breadth)
 Step 4: Area  length * breadth
 Step 5: Print “Area of rect”
, Area
 Step 6: Stop
Area = length *
breadth
Ex3: Problem: Find greater of two
no‟s
 Algorithm:
🞑 Step 1: Start
🞑 Step 2: Declare variables A, B
🞑 Step 3: Accept two values from user and store in A
and B respectively.
🞑 Step 4: Check whether A > B; True: 4.1; False: 4.2
1. Yes  Print A is greater
2. No  Print B is greater
🞑 Step5: Stop
Ex4: Find Largest of three
numbers
Step 1: Start
Step 2: Declare variables a,b
and c. Step 3: Read variables
a,b and c.
Step 4: If a>b ; True: 4.1; False:
4.2
4.1 If a>c True: 4.1.1;
False: 4.1.2
1. Display a is
the largest
number.
else
2. Display c is
the largest
number.
Ex5: Calculate Gross, Net
Salary
 Problem:
 Acceptthe employee details such as empid and
basic salary. Consider DA with 120% of basic,
HRA with 10% of basic. Calculate the gross
salary and net salary considering tax
deduction of Rs. 2% of gross.
Example: Gross,
Net
 Empid: 1001
 Basic: 10000
 DA: 120% of Basic=1.20*10000= 12000
 HRA: 10% of Basic=0.1*10000 = 1000
 Gross=Basic+DA+HRA =
10000+12000+1000=23000
 Tax: 2% of Gross = 0.02*23000=460
Algorithm: Gross,
Net
Step 1: Start
Step 2: Declare variables
empid,basic,da,hra,tax,gross,net Step 3: Print "Enter
Employee ID"
Step 4: Accept empid
Step 5: Print "Enter basic
salary" Step 6: Accept basic
Step 7: Compute
da=(120/100)*basic Step 8:
Compute hra=(10/100)*basic Step
9: Compute gross=(basic+da+hra)
Step 10: Print "Gross Salary"
Step 11: Compute
tax=(2/100)*gross Step 12:
#include<stdio.h>
int main()
{
int empid;
float
basic,da,hra,tax,gross,net;
printf("Enter Employee id:");
scanf("%d",&empid);
printf("Enter Basic Salary:");
scanf("%f",&basic);
da=1.2*basic;
hra=0.1*basic;
gross=(basic+da+hra)
printf("Gross Salary=
%f",gross); tax=0.02*gross;
net=gross-tax;
printf("Net Salary=%f",net);
return
0;
}
Ex 6: Calculate Simple
Interest
 Si = (P * T * R) /
100
Flowchar
t
 A Flowchart is a type of diagram (graphical or symbolic)
that represents an algorithm or process.
 Each step in the process is represented by a different
symbol and contains a short description of the process
step.
 The flow chart symbols are linked together with arrows
showing the process flow direction. A flowchart typically
shows the flow of data in a process, detailing the
operations/steps in a pictorial format which is easier to
Flowchar
t
 Flowchart is the pictorial representation of an
algorithm
49
Identif
y
 What do each of the following symbols
represent?
Decision
Terminal
Input/Output
Operation
Process
Connector
Module
Example 1: Algorithm &
Flowchart
 Problem: Display the Sum, Average,
Product of three given numbers
Algorithm
 Step1: Start
 Step 2: Read X, Y, Z
 Step 3: Compute Sum (S)  X + Y + Z
 Step 4: Compute Average (A)  S / 3
 Step 5: Compute Product (P) as X x Y x Z
 Step 6: Print S, A, P
 Step 7: Stop
Example 2: Algorithm &
Flowchart
 Problem: Display the largest of two given
numbers
Algorithm
 Step1: Start
 Step 2: Read A, B
 Step 3: If A is less than B
🞑 True: Assign A to BIG and B to SMALL
🞑 False: Assign B to BIG and A to SMALL
 Step 6: Print S, A, P
 Step 7: Stop
Control
Structures
 Sequence: A series of steps or statements that are
executed in the order they are written in an algorithm.
 Selection: Defines two courses of action depending
on the outcome of a condition. A condition is an
expression that is, when computed, evaluated to
either true or false. (ex: if, if else, nested if)
 Repetition/Loop: Specifies a block of one or more
statements that are repeatedly executed until a
condition is satisfied. (Ex: for, while, do while)
Ex: Sequence Control
Structure
 A series of steps or statements that are executed
in the order they are written in an algorithm.
 The beginning and end of a block of statements
can be optionally marked with the keywords
begin and end.
begin
statement 1
statement 2
statement n
.....
Sequence Ex: Calculate Person‟s
age
Sequence
Exampleint main()
{
int first, second, temp;
printf("Enter first number: ");
scanf("%d", &first);
printf("Enter second number: ");
scanf("%d", &second);
temp = first;
first = second;
second = temp;
printf("After swapping, firstno. = %dn", first);
printf("After swapping, secondno. = %d", second);
}
Swap two
numbers
using
temp var
Sequence
Example
int main()
{
int a, b;
printf("Enter a and b: ");
scanf("%d %d", &a, &b);
a = a - b;
b = a + b;
a = b - a;
printf("After swapping, a = %dn", a);
printf("After swapping, b = %d", b);
return 0;
}
Swap two
numbers
without
using
temp var
Selection (Decision) Control
structure
is
conditio
n
Y N
Print
stmt
1
Print
stmt
2
Syntax: if
if
(condition)
stmt1
else
stmt2
C programming
if (a>b)
printf(“a is greater than b”);
else
printf(“b is greater than a”);
Selection (Decision) Control
structure
Selection Ex: Program to print the given
number is negative or positive using if
else
int main()
{
int num;
printf("Enter a number to check.n");
scanf("%d",&num);
if (num<0)
printf(“Given Number = %d is negativen",num);
else
printf(“Given Number = %d is positiven",num);
return 0;
}
Selection
Ex:
 Problem: Write an algorithm to determine a students
‟ final
grade and display pass or fail. The final grade is calculated
as the average of four subject marks.
Algorithm
 Step1: Start
 Step 2: Input M1,M2,M3,M4
 Step 3: GRADE  (M1+M2+M3+M4)/4
 Step 4: if (GRADE > 60) then Print “Pass” else “Fail”
 Step 5: Stop
STA
RT
Input
M1,M2,M3,M
4
GRADE(M1+M2+M3+M4)/4
IS
GRADE>6
0
STO
P
Y
N
Print
“Fail
”
Print
“Pass
”
Algorithm
Step1: Start
Step 2: Input M1,M2,M3,M4
Step 3: GRADE  (M1+M2+M3+M4)/4
Step 4: if (GRADE > 60) then Print “Pass” else
“Fail”
Step 5: endif
Step 6: Stop
Selection
Ex:
Selection Ex:
Program to check for odd or
even int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is even.",
num);
else
printf("%d is odd.", num);
return 0;
Selection Ex: Program to check
odd or
even using ternary operator
? :
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.",
num);
return 0;
}
Selection Ex: Problem: Find greater of two
no‟s
 Algorithm:
🞑 Step 1: Start
🞑 Step 2: Declare variables A, B
🞑 Step 3: Accept two values from user and store in A
and B respectively.
🞑 Step 4: Check whether A > B; True: 4.1; False: 4.2
1. Yes  Print A is greater
2. No  Print B is greater
🞑 Step5: Stop
Selectio
n
Selection Ex: Find Largest of three
numbers
Step 1: Start
Step 2: Declare variables a,b
and c. Step 3: Read variables
a,b and c.
Step 4: If a>b ; True: 4.1; False:
4.2
4.1 If a>c True: 4.1.1;
False: 4.1.2
1. Display a is
the largest
number.
else
2. Display c is
the largest
number.
int main()
{
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}
Selection:
if
int main()
{
double n1, n2, n3; printf("Enter
three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%.2lf is the largest number.", n1);
else if (n2 >= n1 && n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
return 0;
}
Selection: if else
if
int main()
{
double n1, n2, n3; printf("Enter
three numbers: ");
scanf("%lf %lf %lf", &n1, &n2,
&n3);
if (n1 >= n2)
{ if (n1 >= n3)
else
printf("%.2lf is the largest number.", n1);
printf("%.2lf is the largest number.", n3);
}
else
{
if (n2 >= n3)
else
}
return 0;
printf("%.2lf is the largest number.", n2);
printf("%.2lf is the largest number.", n3);
}
Selection: nested
if
Repetition: While
condition
Loop:
for
for(initialization, condition,
incrementation)
{
code
statements;
}
int main()
{
int i;
for (i=0; i<10; i++)
{
printf("i=%dn",i);
}
return 0;
}
Loop Example: Multiplication
table
int main()
{
int n, i;
printf("Enter no. to print multiplication table: ");
scanf("%d",&n);
for(i=1;i<=10;++i)
{
printf("%d * %d = %dn", n, i, n*i);
}
}
UNIT 1.pptx Programming for Problem Solving
for(i = 0; i < 5; i++)
{
printf("tttt");
for(j = 0; j < 5; j++)
printf("* ");
printf("n");
}
ANSI C:
Syntax
and
construc
ts
C basic
elements
 Valid character
set
 Identifiers
 Keywords
 Basic data types
 Constants
 Variables
C Valid character
set
 uppercase English alphabets A to Z,
 lowercase letters a to z,
 digits 0 to 9,
 certain special characters as building blocks to
form basic program elements viz. constants,
variables, operators, expressions and
statements.
C
Identifiers
 Identifiers are names given to various items in the program,
such as variables, functions and arrays.
 An identifier consists of letters and digits, in any order, except
that the first character must be a letter.
 Both upper and lowercase letters are permitted.
 C is a case sensitive, the upper case and lower case
considered different, for example code, Code, CODE etc.
are different identifiers.

The underscore character _ can also be included.
 Keywords like if, else, int, float, etc., have special meaning and
they cannot be used as identifier names.
Valid and invalid C
identifiers
 ANSI standard recognizes 31 characters
 valid identifiers: A, ab123, velocity,
stud_name, circumference, Average,
TOTAL.
 Invalid identifiers:
🞑 1st
🞑 "Jamshedpur"
🞑 stud-name
C Data
types
C Data types and
sizes
Data type Description Size Range
char single character 1 byte 0 - 255
int integer number 4 bytes
-2147483648 to
2147483647
float
single precision floating
point number (number
containing fraction & or
an exponent)
4 bytes 3.4E-38 to 3.4E+38
double
double precision floating
point number
8 bytes 1.7E-308 to 1.7E+308
C Data types and
sizesData type Size Range
short int 2 bytes -32768 to 32767
long int 4 bytes
-2147483648 to
2147483647
unsigned short int 2 bytes 0 to 65535
unsigned int 4 bytes 0 to 4294967295
unsigned long int 4 bytes 0 to 4294967295
long double (extended
precision)
8 bytes 1.7E-308 to1.7E+308
C
Constants
 C can be classified into four categories namely
integer constants, floating point constants,
character constants and string constants.
 A character constant is written as for example -
'A'
 A normal integer constant is written as 1234.
 A long int uses L (uppercase or lowercase) at the
end of the constant, e.g. 2748723L
Escape sequence
characters
Character Escape
Sequence
ASCII Value
Bell a 007
Backspace b 008
Null 0 000
Newline n 010
Carriage return r 013
Vertical tab v 011
Horizontal tab t 009
Form feed f 012
Symbolic
constants
 #define PI 3.141593
 #define TRUE 1
 #define PROMPT "Enter Your
Name :"
Accept name from the
userMethod 1
char name[20];
printf("Enter your
name:");
scanf("%s",name);
printf("Your name is:
%s",name);
Method 2
char name[20]
printf(“Enter your
name:”)
gets(name);
printf(“Your name
is:”);
Accept name from the
userMethod
3 #define
MAX_LIMIT 20 int
main()
{
char
name[MAX_LIMIT];
printf("Enter your
Method 4
char name[20];
printf("Enter your
name:");
scanf("%[^n]
%*c",name);
printf("Your name is:
Operators in
C
 Arithmetic
operators
 Relational
operators
 Logical operators
 Bitwise operators
 Assignment
UNIT 1.pptx 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) );
printf(“ %d” (c % a) );
printf
(“%d“
printf(“%d
“
(a+
+) );
(a--) );
(d+
OUTPU
T
30
-10
20
0
2
0
5
10
11
25
UNIT 1.pptx Programming for Problem Solving
UNIT 1.pptx Programming for Problem Solving
UNIT 1.pptx Programming for Problem Solving
UNIT 1.pptx Programming for Problem Solving
Example: Bitwise
Operators
int a =
60; int b =
13;
int c =
0; c = a &
b; c = a |
b;
c = a
^ b; c =
~a;
c = a
printf(“%d" + c
);
printf(“%d" +
c );
printf(“%d" + c
);
printf(“%d" +
c );
c = a >> 2; printf(“%d" +
c );
OUTPU
T
a= 60 = 0011
1100
b= 13 = 0000
1101
c = a & b; 0000 1100
= 12
c = a | b; 0011 1101
= 61
c = a ^ b; 0011 0001
= 49
c = ~a; 1100 0011
= -61
UNIT 1.pptx Programming for Problem Solving
Misc
Operators
sizeof(
) &
:
:
:
:
Returns the size of the
variable Returns the address
of a variable Pointer
variable
Conditional / Ternary
operator
*
?:
Operator Precedence int a = 20, b = 10, c = 15, d = 5;
e = (a + b) * c / d;
/ / Print value of e
e = ((a + b) * c) /
d;
/ / Print value of e
e = (a + b) * (c /
d);
/ / Print value of e
e = a + (b * c) / d;
/ / Print value of e
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 Value of a + (b * c) / d is
: 50
( 30 * 15 ) /
5
(30 * 15 ) /
5
(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
++ --
+ -
!
~
(type
)
Unary
preincrement/predecrement
Unary plus/minus
Unary logical
negation/bitwise
complement
Unary cast (change type)
Dereferen
RL
Arithmetic Operators
Multiplication operator, Divide
by, Modulus
*, /, % LR
Add, Subtract +, – LR
Relational Operators
Less Than <
Greater than >
Less than equal to <=
Greater than equal to >= LR
Equal to ==
Not equal !=
Logical Operators
AND && LR
OR || LR
NOT ! RL
1 == 2 != 3
operators ==
and
!= have the
same
precedence, LR
Hence, 1 == 2
is executed
first
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.
 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,
Endiannes
s
 The endianness of a particular computer system is
generally described by whatever convention or
set of 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.

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
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
Type
Casting
 Type casting is a way to convert a variable
from one data type to another data type.
🞑 Implicit Conversions
🞑 Explicit Conversions
Implicit
Conversions
int main()
{
int i = 17;
char c = 'c'; /* ascii value is 99
*/ int sum;
sum = i + c;
printf("Value of sum : %dn",
sum );
Explicit
Conversions
General format /
Syntax: (type_name)
expression
Example:
main()
{
int sum = 17, count =
5; double mean;
mean = (double) sum /
count;
Examples: Type
Conversions
float a =
5.25;
int b = (int)a;
char c =
‟A ;
‟ int x =
(int)c;
int x=7,
y=5 ; float
z; z=x/y;
int x=7,
y=5; float
z;
z =
(float)x/(flo
Loop Example: Multiplication
table
int main()
{
int n, i;
printf("Enter no. to print multiplication table: ");
scanf("%d",&n);
for(i=1;i<=10;++i)
{
printf("%d * %d = %dn", n, i, n*i);
}
}
break
Statement
The break statement in C programming language has the
following two usages:
When the break statement is encountered inside a loop, the
loop is immediately terminated and program control
resumes at the next statement following the loop.
It can be used to terminate a case in the switch statement
If you are using nested loops, the break statement will stop
the execution of the innermost loop and start executing the
next line of code after the block.
Syntax:
break;
int a =
10;
while( a < 20 )
{
printf("value of a: %dn",
a);
a++;
if( a > 15)
{
break;
}
}
OUTPUT
value of a:
10 value of
a: 11 value
of a: 12
value of a:
13 value of
a: 14 value
of a: 15
int num =0;
while(num<=10
0)
{
printf("value of variable num is: %d n",
num); if (num==2)
{ break
;
}
num++;
}
printf("Out of while-
loop");
Outpu
t:
value of variable num
is: 0 value of variable
num is: 1 value of
variable num is: 2 Out
of while-loop
int var;
for (var =100; var>=10; var
--)
{
printf("var: %dn",
var); if (var==99)
{
break;
}
}
printf("Out of for-loop");
Outpu
t:
var: 100
var: 99
Out of for-
loop
int a = 4;
while( a <
10 )
{
printf("valu
e of a:
%dn", a);
++a;
if( a > 8)
{
break;
printf("B
reak
n");
continu
e The continue statement in C
programming language works
somewhat like the break statement.
 Instead of forcing termination, continue
statement forces the next iteration of the
loop to take place, skipping any code in
between.
int a =
10;
do
{
if( a == 15)
{
a = a + 1;
continue;
}
printf("value of a: %dn",
a);
a++;
} while( a < 20 );
value of a:
10 value of
a: 11 value
of a: 12
value of a:
13 value of
a: 14 value
of a: 16
value of a:
17 value of
a: 18 value
for (int j=0; j<=8; j+
+)
{
if (j==4)
{
continu
e;
}
printf("%d ",
j);
}
Outpu
t:
0 1 2 3 5 6 7
8
int counter=10;
while (counter >=0)
{
if (counter==7)
{
counter-
-;
continue
;
}
printf("%d ",
counter); counter--;
}
Outpu
t:
10 9 8 6 5 4 3 2 1
0
int a = 10;
d
o
{
i
f
(
a
=
=
goto
statement
 The goto statement is used to alter the normal sequence of
program execution by transferring control to some other
part of the program unconditionally.
 Syntax: goto label;

Control may be transferred to anywhere within
the current function.
 The target statement must be labeled, and a colon must
follow the label.
 label : statement;
int w;
printf("Enter the input 1 or 0:
n"); scanf("%d", &w);
if (w == 1)
goto CST;
if (w == 0) printf("Value entered
is 0 n"); return 0;
CST : printf("You belong to CST
Section n"); goto end;
end : printf("Have a nice Dayn");
return 0;
}
Example: Nested
Loop
int i, j;
for(i=2; i<10; i++)
{
for(j=2; j <= (i/j); j++)
if(!(i%j)) break;
if(j > (i/j)) printf("%d is
primen", i);
}
Program to check alphabet, digit or special character
Alphabet: a to z (or)
A to Z Digit : 0 to 9
else it is special character
if((ch >= 'a' && ch <= 'z') | | (ch >= 'A' && ch <= 'Z'))
printf("'%c' is alphabet.", ch);
else if(ch >= '0' && ch <= '9')
printf("'%c' is digit.", ch);
else
printf("'%c' is special
Program to check vowel of
consonant
Vowel : a (or) e (or) i (or) o (or)
u Consonant : a to z or A to Z
else: not an alphabet
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'))
Sum of digits of given number using
while
sum = 0;
/ / Accept number and store
in n num = n;
while( n > 0 )
{
rem = n % 10;
sum +=
rem; n /=
10;
}
printf("Sum of digits of %d is %d", num,
OUTPUT
Enter a number: 456
Sum of digits of 456 is
15
Sum of digits of given number using
while
sum = 0;
/ / Accept number and store
in n num = n;
while( n > 0 )
{
rem = n % 10;
sum +=
rem; n /=
10;
}
printf("Sum of digits of %d is %d", num,
OUTPUT
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);
Print its multiplication
table
i=1;
while(i<=10)
{
printf("%dn",
(num*i)); i++;
}
count total digits in a given integer using
loop
do
{
count++;
num /=
10;
} while(num
!= 0);
printf("Total
Program to print numbers between 1 and
100 which are multiple of 3 using the do while
loop
int i =
1; do
{
if(i %
3 ==
0)
{
prin
find sum of odd numbers from 1
to n
for(i=1; i<=n; i+=2)
{
sum += i;
}
printf("Sum of odd numbers = %d",
sum);
Exponential
series
int i, n;
float x, sum=1, t=1;
// Accept x
// Accept n
for(i=1;i<=n;i++)
{
t=t*x/i;
sum=sum+t;
}
printf(“Exponentia
l Value of %f =
Program to print its multiplication
table
i=1;
while(i<=10)
{
printf("%d n",
(num*i)); i++;
}
i=1;
do
{
printf("%dn",
(num*i)); i++;
}while(i<=10);
for(i=1;i<=10;i++)
{
printf("%d n",
(num*i));
k = 1;
for(i=1; i<=rows; i++)
{
for( j=1; j<=cols; j++, k++)
{
printf("%-3d", k);
}
printf("n");
}
Print number pattern as
shown
int main()
{
int i;
for (i=0; i<10; i+
+)
{
printf("i=%dn",i);
}
return 0;
Factorial using for
loop
fact=1;
for(i=num; i>=1; i--)
fact=fact*i;
printf("Factorial of %d is = %ld",num,fact);
S
u
m of n natural no‟s
using for
sum = 0;
for(i = 1; i <= n; i+
+) sum += i;
printf("Sum is: %d
n", sum);
Program to print its multiplication
table
i=1;
while(i<=10)
{
i++;
}
i=1;
do
{
printf("%dn",(num*i)); printf("%dn",(num*i));
i++;
}while(i<=10);
for(i=1;i<=10;i++)
{
printf("%dn",(num*i));
}
Ex: 2: Nested for
loop
for (int i=0; i<2; i++)
{
for (int j=0; j<4; j++)
{
printf("%d, %d
n",i ,j);
}
Ex: 3: Nested for
loop
k=1;
for (i=0; i<2; i++)
{
for (j=0; j<4; j++,k++)
{
printf("%d, %d, %dn",i ,j, k);
}
}
Print number pattern as
shown Output
:
1
12
123
1234
for(i=1; i<=rows; i++)
{
for( j=1; j<=i; j++)
printf("%d", j);
printf("n");
}
Structured vs Unstructured Programming
Structured Programming is a
programming paradigm which
divides the code into modules or
function.
Unstructured Programming
is the paradigm in which
the code is considered as
one single block.
Readabilit
y
Structured Programming
based
programs are easy to
read.
Unstructured Programming
based
programs are hard to read.
Purpos
e
Structured Programming is to
make the code more efficient
and easier to understand.
Unstructured programming is
just to program to solve the
problem. It does not create a
logical structure.
Complexit
y
Structured Programming is
easier because of modules.
Unstructured programming is
harder when comparing with
the structured programming
Application
Structured programming can be
used for small and medium scale
projects.
Unstructured programming is not
applicable for medium and complex
projects.
Modification
It is easy to do changes in
Structured Programming.
It is hard to do modifications in
Unstructured Programming.
Data Types
Structured programming uses
many data types.
Unstructured programming has a
limited number of data types.
Code Duplication
Structured programming
avoids code duplication.
Unstructured programming can
have code duplication.
Testing and Debug
It is easy to do testing and It is hard to do testing and
UNIT 1.pptx Programming for Problem Solving
Palindrome
number
rev=0
origin=n;
while (n != 0)
{
rem = n
% 10;
rev = rev * 10 + rem;
n /= 10;
}
if (orig == rev) printf("%d is a palindrome.“,orig);
int i, j;
for(i = 2; i<100; i++)
{
for(j = 2; j <= (i/j); j++)
if(!(i%j)) break; // if factor found, not prime
if(j > (i/j)) printf("%d is primen", i);
}
Print Prime numbers upto
100
Fibonacci
series
for(i=3;i<=n;i++)
{
trm=prv+pre;
printf("% 5d",trm);
prv=pre;
pre=trm;
}
printf("n");
Palindrome
number
rev=0
origin=n;
while (n != 0)
{
rem = n
% 10;
rev =
rev * 10
+ rem;
n /= 10;
}
if (origin == rev) printf("%d is a palindrome.“,orig);
else printf("%d is not a palindrome.", orig);
goto statement: Unstructured
programming
 The goto statement is used to alter the normal
sequence of program execution by transferring
control to some other part of the program
unconditionally.
 Syntax: goto label;
Control may be transferred to anywhere
within the current function.
 The target statement must be labeled, and a
colon must follow the label.
UNIT 1.pptx Programming for Problem Solving
Ad

More Related Content

Similar to UNIT 1.pptx Programming for Problem Solving (20)

java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
ishorishore
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
racha49
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
SamridhiGulati4
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.ppt
BhargaviDalal4
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
Rohit Shrivastava
 
Cp module 2
Cp module 2Cp module 2
Cp module 2
Amarjith C K
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
Don Dooley
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx
ssuser586772
 
UNIT 1- Design Analysis of algorithms and its working
UNIT 1- Design Analysis of algorithms and its workingUNIT 1- Design Analysis of algorithms and its working
UNIT 1- Design Analysis of algorithms and its working
Bobby Pra A
 
UNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUER
UNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUERUNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUER
UNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUER
Salini P
 
c-programming
c-programmingc-programming
c-programming
Zulhazmi Harith
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
Madishetty Prathibha
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
SONU KUMAR
 
Problem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study materialProblem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study material
To Sum It Up
 
Algorithm for computational problematic sit
Algorithm for computational problematic sitAlgorithm for computational problematic sit
Algorithm for computational problematic sit
Saurabh846965
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
Sreedhar Chowdam
 
Chap6
Chap6Chap6
Chap6
artipradhan
 
UNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.pptUNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.ppt
GovindUpadhyay25
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowcharts
Samuel Igbanogu
 
Data structures algorithms basics
Data structures   algorithms basicsData structures   algorithms basics
Data structures algorithms basics
ayeshasafdar8
 
java or oops class not in kerala polytechnic 4rth semester nots j
java or oops class not in kerala polytechnic  4rth semester nots jjava or oops class not in kerala polytechnic  4rth semester nots j
java or oops class not in kerala polytechnic 4rth semester nots j
ishorishore
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
racha49
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.ppt
BhargaviDalal4
 
2 EPT 162 Lecture 2
2 EPT 162 Lecture 22 EPT 162 Lecture 2
2 EPT 162 Lecture 2
Don Dooley
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx
ssuser586772
 
UNIT 1- Design Analysis of algorithms and its working
UNIT 1- Design Analysis of algorithms and its workingUNIT 1- Design Analysis of algorithms and its working
UNIT 1- Design Analysis of algorithms and its working
Bobby Pra A
 
UNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUER
UNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUERUNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUER
UNIT-1-PPTS-DAA INTRO WITH DIVIDE AND CONQUER
Salini P
 
Programming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptxProgramming in C by SONU KUMAR.pptx
Programming in C by SONU KUMAR.pptx
SONU KUMAR
 
Problem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study materialProblem solving using computers - Unit 1 - Study material
Problem solving using computers - Unit 1 - Study material
To Sum It Up
 
Algorithm for computational problematic sit
Algorithm for computational problematic sitAlgorithm for computational problematic sit
Algorithm for computational problematic sit
Saurabh846965
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
Sreedhar Chowdam
 
Algorithms and flowcharts
Algorithms and flowchartsAlgorithms and flowcharts
Algorithms and flowcharts
Samuel Igbanogu
 
Data structures algorithms basics
Data structures   algorithms basicsData structures   algorithms basics
Data structures algorithms basics
ayeshasafdar8
 

Recently uploaded (20)

ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx2025 The Senior Landscape and SET plan preparations.pptx
2025 The Senior Landscape and SET plan preparations.pptx
mansk2
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Ad

UNIT 1.pptx Programming for Problem Solving

  • 2. Unit 1  Introduction to Programming :  Compilers, compiling and executing a program.  Representation of Algorithm –  Algorithms for finding roots of quadratic equations,  finding minimum and maximum numbers of a given set,  finding if a number is a prime number  Flowchart/Pseudocode with examples,  Program design and structured programming.
  • 3. Unit 1  Introduction to C Programming Language:  variables (with data types and space requirements),  Syntax and Logical Errors in compilation,  object and executable code,  Operators,  expressions and precedence,  Expression evaluation,  Storage classes (auto, extern, static, and register),  type conversion,
  • 4. Unit 1  The main method and command line arguments Bitwise operations:  Bitwise AND, OR, XOR, and NOT operators.  Conditional Branching and Loops:  Writing and evaluation of conditionals and consequent branching with if, if-else, switch-case,  ternary operator,  goto, Iteration with for, while, do-while loops.  I/O: Simple input and output with scanf and printf, formatted I/O, Introduction to stdin, stdout, and stderr. Command line arguments.
  • 5. Textbooks & References 1. Jeri R. Hanly and Elliot B. Koffman, “Problem Solving and Program Design in C,” 7th Edition, Pearson. 2. A.B. Forouzan and F.S. Gilberg, “C Programming and Data Structures,” Cengage Learning, 3rd Edition.
  • 6. C is a programming language developed at Bell Laboratories of USA in 1972 by Dennis Ritchie
  • 7.  C is a programming language developed at Bell Laboratories of USA in 1972 by Dennis Ritchie
  • 13. Binary representation (Decimal to Binary Decimal no.= 123 Binary no. = 1111011 Decimal no. = 255 Binary no. = 1111 1111
  • 18. Binary to Decimal Binary no. = 110011 Decimal no. = 51 Binary no. = 1111 0001 Decimal no. = 241
  • 19. Algorith m Algorithm  Algorithm is a finite set of instructions that if followed accomplishes a particular task. Problem Inpu t Outpu t Comput er
  • 20. Problem: Shortest distance Input n nodes connecting nodes dista nce between nodes O utput
  • 25. Characteristics of Algorithm  Input  Output  Definitenes s  Effectivenes s  Finiteness OD euftipnui tte−n eAsns a−lSghoorui tlhdmbseh oc luel da rhaanv de 1 or lead to only one meaning. IEnfpf euctt i−v eAnneas sl g−o rAi tnh ma lsgh oorui tl dh mh as hvoeu0l dohra v e ms t eopr e- bwye- sl lt- edpe fdi ni reedc t iion pn su,t sw. hich should be independent of any programming code Finiteness − An algorithm must terminate after a finite number of steps
  • 26. Characteristics of Algorithm  Input − Should have 0 or more well-defined inputs.  Output - Should have 1 or more well-defined outputs, and should match the desired output.  Definiteness −Should be clear and unambiguous.  Effectiveness − Should have step-by-step directions, which should be independent of any programming code.  Finiteness − Must terminate after a finite number of steps.
  • 27. Program to display “Welcome to C” #include<stdio. h> int main() { printf(“Welcome to C”); return 0; }
  • 28. Problem 1: Addition of two numbers  Input : 🞑 two numbers (num1 = 4, num2 = 6)  Output: 🞑 Sum = 10 Algorithm  Step 1: Start  Step 2: Read the first number(num1)  Step 3: Read second number(num2)  Step 4: Sum  num1+num2  Step 5: Print Sum  Sum = num1 + num2
  • 29. Program to accept two numbers from the user and display the sum using C language /* Program to accept two numbers and display the sum Programmer : --Your name-- Roll no: --your roll number-- Date of compilation: 27Jan2021 Class : B.Tech I Sem „CST' */
  • 30. #include<stdio.h> / / Main function begins int main() { int num1,num2,sum; printf("Enter first value:"); scanf("%d",&num1); printf("Enter Second value:"); scanf("%d",&num2); sum=num1+num2; printf(“Addition of %d and %d is: %d",num1,num2,sum); return 0; }
  • 31. Problem 2: Algorithm to find the area of rectangle  Input : 🞑 two numbers. (length = 8 breadth = 4)  Output: 🞑 Area of rect = 32 Algorith m  Step 1: Start  Step 2: Read first number(length)  Step 3: Read second number (breadth)  Step 4: Area  length * breadth  Step 5: Print “Area of rect” , Area  Step 6: Stop Area = length * breadth
  • 32. Ex3: Problem: Find greater of two no‟s  Algorithm: 🞑 Step 1: Start 🞑 Step 2: Declare variables A, B 🞑 Step 3: Accept two values from user and store in A and B respectively. 🞑 Step 4: Check whether A > B; True: 4.1; False: 4.2 1. Yes  Print A is greater 2. No  Print B is greater 🞑 Step5: Stop
  • 33. Ex4: Find Largest of three numbers Step 1: Start Step 2: Declare variables a,b and c. Step 3: Read variables a,b and c. Step 4: If a>b ; True: 4.1; False: 4.2 4.1 If a>c True: 4.1.1; False: 4.1.2 1. Display a is the largest number. else 2. Display c is the largest number.
  • 34. Ex5: Calculate Gross, Net Salary  Problem:  Acceptthe employee details such as empid and basic salary. Consider DA with 120% of basic, HRA with 10% of basic. Calculate the gross salary and net salary considering tax deduction of Rs. 2% of gross.
  • 35. Example: Gross, Net  Empid: 1001  Basic: 10000  DA: 120% of Basic=1.20*10000= 12000  HRA: 10% of Basic=0.1*10000 = 1000  Gross=Basic+DA+HRA = 10000+12000+1000=23000  Tax: 2% of Gross = 0.02*23000=460
  • 36. Algorithm: Gross, Net Step 1: Start Step 2: Declare variables empid,basic,da,hra,tax,gross,net Step 3: Print "Enter Employee ID" Step 4: Accept empid Step 5: Print "Enter basic salary" Step 6: Accept basic Step 7: Compute da=(120/100)*basic Step 8: Compute hra=(10/100)*basic Step 9: Compute gross=(basic+da+hra) Step 10: Print "Gross Salary" Step 11: Compute tax=(2/100)*gross Step 12:
  • 37. #include<stdio.h> int main() { int empid; float basic,da,hra,tax,gross,net; printf("Enter Employee id:"); scanf("%d",&empid); printf("Enter Basic Salary:"); scanf("%f",&basic); da=1.2*basic; hra=0.1*basic; gross=(basic+da+hra) printf("Gross Salary= %f",gross); tax=0.02*gross; net=gross-tax; printf("Net Salary=%f",net); return 0; }
  • 38. Ex 6: Calculate Simple Interest  Si = (P * T * R) / 100
  • 39. Flowchar t  A Flowchart is a type of diagram (graphical or symbolic) that represents an algorithm or process.  Each step in the process is represented by a different symbol and contains a short description of the process step.  The flow chart symbols are linked together with arrows showing the process flow direction. A flowchart typically shows the flow of data in a process, detailing the operations/steps in a pictorial format which is easier to
  • 40. Flowchar t  Flowchart is the pictorial representation of an algorithm
  • 41. 49 Identif y  What do each of the following symbols represent? Decision Terminal Input/Output Operation Process Connector Module
  • 42. Example 1: Algorithm & Flowchart  Problem: Display the Sum, Average, Product of three given numbers Algorithm  Step1: Start  Step 2: Read X, Y, Z  Step 3: Compute Sum (S)  X + Y + Z  Step 4: Compute Average (A)  S / 3  Step 5: Compute Product (P) as X x Y x Z  Step 6: Print S, A, P  Step 7: Stop
  • 43. Example 2: Algorithm & Flowchart  Problem: Display the largest of two given numbers Algorithm  Step1: Start  Step 2: Read A, B  Step 3: If A is less than B 🞑 True: Assign A to BIG and B to SMALL 🞑 False: Assign B to BIG and A to SMALL  Step 6: Print S, A, P  Step 7: Stop
  • 44. Control Structures  Sequence: A series of steps or statements that are executed in the order they are written in an algorithm.  Selection: Defines two courses of action depending on the outcome of a condition. A condition is an expression that is, when computed, evaluated to either true or false. (ex: if, if else, nested if)  Repetition/Loop: Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied. (Ex: for, while, do while)
  • 45. Ex: Sequence Control Structure  A series of steps or statements that are executed in the order they are written in an algorithm.  The beginning and end of a block of statements can be optionally marked with the keywords begin and end. begin statement 1 statement 2 statement n .....
  • 46. Sequence Ex: Calculate Person‟s age
  • 47. Sequence Exampleint main() { int first, second, temp; printf("Enter first number: "); scanf("%d", &first); printf("Enter second number: "); scanf("%d", &second); temp = first; first = second; second = temp; printf("After swapping, firstno. = %dn", first); printf("After swapping, secondno. = %d", second); } Swap two numbers using temp var
  • 48. Sequence Example int main() { int a, b; printf("Enter a and b: "); scanf("%d %d", &a, &b); a = a - b; b = a + b; a = b - a; printf("After swapping, a = %dn", a); printf("After swapping, b = %d", b); return 0; } Swap two numbers without using temp var
  • 50. Syntax: if if (condition) stmt1 else stmt2 C programming if (a>b) printf(“a is greater than b”); else printf(“b is greater than a”); Selection (Decision) Control structure
  • 51. Selection Ex: Program to print the given number is negative or positive using if else int main() { int num; printf("Enter a number to check.n"); scanf("%d",&num); if (num<0) printf(“Given Number = %d is negativen",num); else printf(“Given Number = %d is positiven",num); return 0; }
  • 52. Selection Ex:  Problem: Write an algorithm to determine a students ‟ final grade and display pass or fail. The final grade is calculated as the average of four subject marks. Algorithm  Step1: Start  Step 2: Input M1,M2,M3,M4  Step 3: GRADE  (M1+M2+M3+M4)/4  Step 4: if (GRADE > 60) then Print “Pass” else “Fail”  Step 5: Stop
  • 53. STA RT Input M1,M2,M3,M 4 GRADE(M1+M2+M3+M4)/4 IS GRADE>6 0 STO P Y N Print “Fail ” Print “Pass ” Algorithm Step1: Start Step 2: Input M1,M2,M3,M4 Step 3: GRADE  (M1+M2+M3+M4)/4 Step 4: if (GRADE > 60) then Print “Pass” else “Fail” Step 5: endif Step 6: Stop Selection Ex:
  • 54. Selection Ex: Program to check for odd or even int main() { int num; printf("Enter an integer: "); scanf("%d", &num); if (num % 2 == 0) printf("%d is even.", num); else printf("%d is odd.", num); return 0;
  • 55. Selection Ex: Program to check odd or even using ternary operator ? : int main() { int num; printf("Enter an integer: "); scanf("%d", &num); (num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num); return 0; }
  • 56. Selection Ex: Problem: Find greater of two no‟s  Algorithm: 🞑 Step 1: Start 🞑 Step 2: Declare variables A, B 🞑 Step 3: Accept two values from user and store in A and B respectively. 🞑 Step 4: Check whether A > B; True: 4.1; False: 4.2 1. Yes  Print A is greater 2. No  Print B is greater 🞑 Step5: Stop
  • 58. Selection Ex: Find Largest of three numbers Step 1: Start Step 2: Declare variables a,b and c. Step 3: Read variables a,b and c. Step 4: If a>b ; True: 4.1; False: 4.2 4.1 If a>c True: 4.1.1; False: 4.1.2 1. Display a is the largest number. else 2. Display c is the largest number.
  • 59. int main() { double n1, n2, n3; printf("Enter three different numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); if (n1 >= n2 && n1 >= n3) printf("%.2f is the largest number.", n1); if (n2 >= n1 && n2 >= n3) printf("%.2f is the largest number.", n2); if (n3 >= n1 && n3 >= n2) printf("%.2f is the largest number.", n3); return 0; } Selection: if
  • 60. int main() { double n1, n2, n3; printf("Enter three numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); if (n1 >= n2 && n1 >= n3) printf("%.2lf is the largest number.", n1); else if (n2 >= n1 && n2 >= n3) printf("%.2lf is the largest number.", n2); else printf("%.2lf is the largest number.", n3); return 0; } Selection: if else if
  • 61. int main() { double n1, n2, n3; printf("Enter three numbers: "); scanf("%lf %lf %lf", &n1, &n2, &n3); if (n1 >= n2) { if (n1 >= n3) else printf("%.2lf is the largest number.", n1); printf("%.2lf is the largest number.", n3); } else { if (n2 >= n3) else } return 0; printf("%.2lf is the largest number.", n2); printf("%.2lf is the largest number.", n3); } Selection: nested if
  • 64. Loop Example: Multiplication table int main() { int n, i; printf("Enter no. to print multiplication table: "); scanf("%d",&n); for(i=1;i<=10;++i) { printf("%d * %d = %dn", n, i, n*i); } }
  • 66. for(i = 0; i < 5; i++) { printf("tttt"); for(j = 0; j < 5; j++) printf("* "); printf("n"); }
  • 68. C basic elements  Valid character set  Identifiers  Keywords  Basic data types  Constants  Variables
  • 69. C Valid character set  uppercase English alphabets A to Z,  lowercase letters a to z,  digits 0 to 9,  certain special characters as building blocks to form basic program elements viz. constants, variables, operators, expressions and statements.
  • 70. C Identifiers  Identifiers are names given to various items in the program, such as variables, functions and arrays.  An identifier consists of letters and digits, in any order, except that the first character must be a letter.  Both upper and lowercase letters are permitted.  C is a case sensitive, the upper case and lower case considered different, for example code, Code, CODE etc. are different identifiers.  The underscore character _ can also be included.  Keywords like if, else, int, float, etc., have special meaning and they cannot be used as identifier names.
  • 71. Valid and invalid C identifiers  ANSI standard recognizes 31 characters  valid identifiers: A, ab123, velocity, stud_name, circumference, Average, TOTAL.  Invalid identifiers: 🞑 1st 🞑 "Jamshedpur" 🞑 stud-name
  • 73. C Data types and sizes Data type Description Size Range char single character 1 byte 0 - 255 int integer number 4 bytes -2147483648 to 2147483647 float single precision floating point number (number containing fraction & or an exponent) 4 bytes 3.4E-38 to 3.4E+38 double double precision floating point number 8 bytes 1.7E-308 to 1.7E+308
  • 74. C Data types and sizesData type Size Range short int 2 bytes -32768 to 32767 long int 4 bytes -2147483648 to 2147483647 unsigned short int 2 bytes 0 to 65535 unsigned int 4 bytes 0 to 4294967295 unsigned long int 4 bytes 0 to 4294967295 long double (extended precision) 8 bytes 1.7E-308 to1.7E+308
  • 75. C Constants  C can be classified into four categories namely integer constants, floating point constants, character constants and string constants.  A character constant is written as for example - 'A'  A normal integer constant is written as 1234.  A long int uses L (uppercase or lowercase) at the end of the constant, e.g. 2748723L
  • 76. Escape sequence characters Character Escape Sequence ASCII Value Bell a 007 Backspace b 008 Null 0 000 Newline n 010 Carriage return r 013 Vertical tab v 011 Horizontal tab t 009 Form feed f 012
  • 77. Symbolic constants  #define PI 3.141593  #define TRUE 1  #define PROMPT "Enter Your Name :"
  • 78. Accept name from the userMethod 1 char name[20]; printf("Enter your name:"); scanf("%s",name); printf("Your name is: %s",name); Method 2 char name[20] printf(“Enter your name:”) gets(name); printf(“Your name is:”);
  • 79. Accept name from the userMethod 3 #define MAX_LIMIT 20 int main() { char name[MAX_LIMIT]; printf("Enter your Method 4 char name[20]; printf("Enter your name:"); scanf("%[^n] %*c",name); printf("Your name is:
  • 80. Operators in C  Arithmetic operators  Relational operators  Logical operators  Bitwise operators  Assignment
  • 82. 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) ); printf(“ %d” (c % a) ); printf (“%d“ printf(“%d “ (a+ +) ); (a--) ); (d+ OUTPU T 30 -10 20 0 2 0 5 10 11 25
  • 87. Example: Bitwise Operators int a = 60; int b = 13; int c = 0; c = a & b; c = a | b; c = a ^ b; c = ~a; c = a printf(“%d" + c ); printf(“%d" + c ); printf(“%d" + c ); printf(“%d" + c ); c = a >> 2; printf(“%d" + c ); OUTPU T a= 60 = 0011 1100 b= 13 = 0000 1101 c = a & b; 0000 1100 = 12 c = a | b; 0011 1101 = 61 c = a ^ b; 0011 0001 = 49 c = ~a; 1100 0011 = -61
  • 89. Misc Operators sizeof( ) & : : : : Returns the size of the variable Returns the address of a variable Pointer variable Conditional / Ternary operator * ?:
  • 90. Operator Precedence int a = 20, b = 10, c = 15, d = 5; e = (a + b) * c / d; / / Print value of e e = ((a + b) * c) / d; / / Print value of e e = (a + b) * (c / d); / / Print value of e e = a + (b * c) / d; / / Print value of e 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 Value of a + (b * c) / d is : 50 ( 30 * 15 ) / 5 (30 * 15 ) / 5 (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).
  • 91. Operator Description Associativity () [ ] .  Parentheses (grouping) Brackets (array subscript) Member selection Member selection via pointer LR ++ -- + - ! ~ (type ) Unary preincrement/predecrement Unary plus/minus Unary logical negation/bitwise complement Unary cast (change type) Dereferen RL
  • 92. Arithmetic Operators Multiplication operator, Divide by, Modulus *, /, % LR Add, Subtract +, – LR Relational Operators Less Than < Greater than > Less than equal to <= Greater than equal to >= LR Equal to == Not equal != Logical Operators AND && LR OR || LR NOT ! RL 1 == 2 != 3 operators == and != have the same precedence, LR Hence, 1 == 2 is executed first
  • 93. 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.  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,
  • 94. Endiannes s  The endianness of a particular computer system is generally described by whatever convention or set of 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. 
  • 95. 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
  • 96. 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 Address Value 1000 78 1001 56 1002 34 1003 12
  • 97.  For example 4A3B2C1D at address 100, they store the bytes within the address range 100 through 103 in the following order:m
  • 98. Type Casting  Type casting is a way to convert a variable from one data type to another data type. 🞑 Implicit Conversions 🞑 Explicit Conversions
  • 99. Implicit Conversions int main() { int i = 17; char c = 'c'; /* ascii value is 99 */ int sum; sum = i + c; printf("Value of sum : %dn", sum );
  • 100. Explicit Conversions General format / Syntax: (type_name) expression Example: main() { int sum = 17, count = 5; double mean; mean = (double) sum / count;
  • 101. Examples: Type Conversions float a = 5.25; int b = (int)a; char c = ‟A ; ‟ int x = (int)c; int x=7, y=5 ; float z; z=x/y; int x=7, y=5; float z; z = (float)x/(flo
  • 102. Loop Example: Multiplication table int main() { int n, i; printf("Enter no. to print multiplication table: "); scanf("%d",&n); for(i=1;i<=10;++i) { printf("%d * %d = %dn", n, i, n*i); } }
  • 103. break Statement The break statement in C programming language has the following two usages: When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop. It can be used to terminate a case in the switch statement If you are using nested loops, the break statement will stop the execution of the innermost loop and start executing the next line of code after the block. Syntax: break;
  • 104. int a = 10; while( a < 20 ) { printf("value of a: %dn", a); a++; if( a > 15) { break; } } OUTPUT value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15
  • 105. int num =0; while(num<=10 0) { printf("value of variable num is: %d n", num); if (num==2) { break ; } num++; } printf("Out of while- loop"); Outpu t: value of variable num is: 0 value of variable num is: 1 value of variable num is: 2 Out of while-loop
  • 106. int var; for (var =100; var>=10; var --) { printf("var: %dn", var); if (var==99) { break; } } printf("Out of for-loop"); Outpu t: var: 100 var: 99 Out of for- loop
  • 107. int a = 4; while( a < 10 ) { printf("valu e of a: %dn", a); ++a; if( a > 8) { break; printf("B reak n");
  • 108. continu e The continue statement in C programming language works somewhat like the break statement.  Instead of forcing termination, continue statement forces the next iteration of the loop to take place, skipping any code in between.
  • 109. int a = 10; do { if( a == 15) { a = a + 1; continue; } printf("value of a: %dn", a); a++; } while( a < 20 ); value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value
  • 110. for (int j=0; j<=8; j+ +) { if (j==4) { continu e; } printf("%d ", j); } Outpu t: 0 1 2 3 5 6 7 8
  • 111. int counter=10; while (counter >=0) { if (counter==7) { counter- -; continue ; } printf("%d ", counter); counter--; } Outpu t: 10 9 8 6 5 4 3 2 1 0
  • 112. int a = 10; d o { i f ( a = =
  • 113. goto statement  The goto statement is used to alter the normal sequence of program execution by transferring control to some other part of the program unconditionally.  Syntax: goto label;  Control may be transferred to anywhere within the current function.  The target statement must be labeled, and a colon must follow the label.  label : statement;
  • 114. int w; printf("Enter the input 1 or 0: n"); scanf("%d", &w); if (w == 1) goto CST; if (w == 0) printf("Value entered is 0 n"); return 0; CST : printf("You belong to CST Section n"); goto end; end : printf("Have a nice Dayn"); return 0; }
  • 115. Example: Nested Loop int i, j; for(i=2; i<10; i++) { for(j=2; j <= (i/j); j++) if(!(i%j)) break; if(j > (i/j)) printf("%d is primen", i); }
  • 116. Program to check alphabet, digit or special character Alphabet: a to z (or) A to Z Digit : 0 to 9 else it is special character if((ch >= 'a' && ch <= 'z') | | (ch >= 'A' && ch <= 'Z')) printf("'%c' is alphabet.", ch); else if(ch >= '0' && ch <= '9') printf("'%c' is digit.", ch); else printf("'%c' is special
  • 117. Program to check vowel of consonant Vowel : a (or) e (or) i (or) o (or) u Consonant : a to z or A to Z else: not an alphabet 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'))
  • 118. Sum of digits of given number using while sum = 0; / / Accept number and store in n num = n; while( n > 0 ) { rem = n % 10; sum += rem; n /= 10; } printf("Sum of digits of %d is %d", num, OUTPUT Enter a number: 456 Sum of digits of 456 is 15
  • 119. Sum of digits of given number using while sum = 0; / / Accept number and store in n num = n; while( n > 0 ) { rem = n % 10; sum += rem; n /= 10; } printf("Sum of digits of %d is %d", num, OUTPUT Enter a number: 456 Sum of digits of 456 is 15
  • 120. Print all ODD numbers from 1 to N using while loop. number=1; while(number<= n) { if(number%2 != 0) printf("%d ",number);
  • 122. count total digits in a given integer using loop do { count++; num /= 10; } while(num != 0); printf("Total
  • 123. Program to print numbers between 1 and 100 which are multiple of 3 using the do while loop int i = 1; do { if(i % 3 == 0) { prin
  • 124. find sum of odd numbers from 1 to n for(i=1; i<=n; i+=2) { sum += i; } printf("Sum of odd numbers = %d", sum);
  • 125. Exponential series int i, n; float x, sum=1, t=1; // Accept x // Accept n for(i=1;i<=n;i++) { t=t*x/i; sum=sum+t; } printf(“Exponentia l Value of %f =
  • 126. Program to print its multiplication table i=1; while(i<=10) { printf("%d n", (num*i)); i++; } i=1; do { printf("%dn", (num*i)); i++; }while(i<=10); for(i=1;i<=10;i++) { printf("%d n", (num*i));
  • 127. k = 1; for(i=1; i<=rows; i++) { for( j=1; j<=cols; j++, k++) { printf("%-3d", k); } printf("n"); } Print number pattern as shown
  • 128. int main() { int i; for (i=0; i<10; i+ +) { printf("i=%dn",i); } return 0;
  • 129. Factorial using for loop fact=1; for(i=num; i>=1; i--) fact=fact*i; printf("Factorial of %d is = %ld",num,fact);
  • 130. S u m of n natural no‟s using for sum = 0; for(i = 1; i <= n; i+ +) sum += i; printf("Sum is: %d n", sum);
  • 131. Program to print its multiplication table i=1; while(i<=10) { i++; } i=1; do { printf("%dn",(num*i)); printf("%dn",(num*i)); i++; }while(i<=10); for(i=1;i<=10;i++) { printf("%dn",(num*i)); }
  • 132. Ex: 2: Nested for loop for (int i=0; i<2; i++) { for (int j=0; j<4; j++) { printf("%d, %d n",i ,j); }
  • 133. Ex: 3: Nested for loop k=1; for (i=0; i<2; i++) { for (j=0; j<4; j++,k++) { printf("%d, %d, %dn",i ,j, k); } }
  • 134. Print number pattern as shown Output : 1 12 123 1234 for(i=1; i<=rows; i++) { for( j=1; j<=i; j++) printf("%d", j); printf("n"); }
  • 135. Structured vs Unstructured Programming Structured Programming is a programming paradigm which divides the code into modules or function. Unstructured Programming is the paradigm in which the code is considered as one single block. Readabilit y Structured Programming based programs are easy to read. Unstructured Programming based programs are hard to read. Purpos e Structured Programming is to make the code more efficient and easier to understand. Unstructured programming is just to program to solve the problem. It does not create a logical structure. Complexit y Structured Programming is easier because of modules. Unstructured programming is harder when comparing with the structured programming
  • 136. Application Structured programming can be used for small and medium scale projects. Unstructured programming is not applicable for medium and complex projects. Modification It is easy to do changes in Structured Programming. It is hard to do modifications in Unstructured Programming. Data Types Structured programming uses many data types. Unstructured programming has a limited number of data types. Code Duplication Structured programming avoids code duplication. Unstructured programming can have code duplication. Testing and Debug It is easy to do testing and It is hard to do testing and
  • 138. Palindrome number rev=0 origin=n; while (n != 0) { rem = n % 10; rev = rev * 10 + rem; n /= 10; } if (orig == rev) printf("%d is a palindrome.“,orig);
  • 139. int i, j; for(i = 2; i<100; i++) { for(j = 2; j <= (i/j); j++) if(!(i%j)) break; // if factor found, not prime if(j > (i/j)) printf("%d is primen", i); } Print Prime numbers upto 100
  • 141. Palindrome number rev=0 origin=n; while (n != 0) { rem = n % 10; rev = rev * 10 + rem; n /= 10; } if (origin == rev) printf("%d is a palindrome.“,orig); else printf("%d is not a palindrome.", orig);
  • 142. goto statement: Unstructured programming  The goto statement is used to alter the normal sequence of program execution by transferring control to some other part of the program unconditionally.  Syntax: goto label; Control may be transferred to anywhere within the current function.  The target statement must be labeled, and a colon must follow the label.
  翻译: