0% found this document useful (0 votes)
6 views31 pages

(L1) Algorithm

The document provides an overview of basic C programming concepts, including examples of C programs, algorithms, pseudocode, flowcharts, decision control statements, and loop control statements. It covers syntax for various statements such as if, if-else, nested if, and loops like for, while, and do-while, along with practical examples and exercises. Additionally, it explains the use of conditional and logical operators in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views31 pages

(L1) Algorithm

The document provides an overview of basic C programming concepts, including examples of C programs, algorithms, pseudocode, flowcharts, decision control statements, and loop control statements. It covers syntax for various statements such as if, if-else, nested if, and loops like for, while, and do-while, along with practical examples and exercises. Additionally, it explains the use of conditional and logical operators in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

First C Program Output:

#include<stdio.h> #include<stdio.h> 60
void main() void main()
{ { Sum is 60
int a=10;
int a=10, b=20, Int c=30;
int b=20; Sum of 10, 20 and 30 is 60
int sum;
int c=30;
sum = a+b+c;
int sum;
sum = a+b+c; printf(“%d”, sum); printf(“Sum is %d”, sum);
printf(“%d”, sum); }
printf(“Sum of %d, %d and %d is %d”, a,b,c, sum);
}
scanf() function
#include<stdio.h>
void main() Output:
Enter three numbers 5 10 15
{
Total is 30
int a, b c;
int sum;
printf(“Enter three numbers”);
scanf(“%d”,&a);
scanf(“%d%d%d”, &a, &b, &c);
scanf(“%d”,&b);
scanf(“%d”,&c);
sum = a+b+c;
printf(“Total is %d”, sum);
}
ALGORITHM
ALGORITHM: An algorithm is a step by step procedure to solve any particular
problem.
For example: ALGORITHM FOR ADDING TWO NUMBERS
Step 1: Start.
Step 2: Declare two variables num1 and num2
Step 3: Declare third variable ‘sum’ to store sum of numbers.
Step 4: Read values of num1 and num2 from the user.
Step 5: Add num1 and num2 and assign the result to the variable sum.
Step 6: Display sum.
Step 7: Exit.
PSEUDOCODE
PSEUDOCODE: Pseudocode is an informal high-level description of algorithm.
• It is written in natural language and with the help of mathematical
notations.
• There is no particular programming language or syntax to follow while
writing pseudocode.
For example: PSEUDOCODE FOR ADDING TWO NUMBERS
Begin:
Set sum = 0.
Read: num1 and num2.
Set sum = num1 + num2.
Print sum.
End.
FLOWCHART
FLOWCHART: Flowchart is a diagrammatical representation of an algorithm.
• Various symbols are used to draw a flowchart.
OVAL Oval shape is used to denote Start and End.

RHOMBUS Rhombus shape is used to denote Input and Output.

DIAMOND
Diamond shape is used to denote decision statement.

RECTANGLE
Rectangle shape is used to denote a process.

ARROW
Arrow shape is used to denote flow of program.
Flowchart to find if the given number is odd or even.
(Also If number is even, increment it by 1 else increment it by 2 after display.)

START

INPUT NUMBER

YES IF NUMBER DIVISIBLE NO


BY 2

DISPLAY DISPLAY
“NUMBER IS EVEN” “NUMBER IS ODD”

NUMBER=NUMBER +1 NUMBER=NUMBER +2

STOP
DECISION CONTROL STATEMENT
There are three decision making statements in C. Relational Operators
• if statement if(condition is true) < less than
• if-else statement { > greater than
do this; <= less than equal to
• Conditional Operators >= greater than equal to
SYNTAX of if statement : do this;
== is equal to
For Example, ..
!= not equal to
void main() }
{
int a = 10, b=20; void main()
if (a>5) {
{ int a=10, b=30;
b= b + 30;
a= a – 5;
if(a==b)
} printf(“Numbers are equal”);
printf(“%d%d”,a, b); }
}
if else statement #include<stdio.h>
SYNTAX of if else statement #include<conio.h>
For Example, void main() void main()
if (condition is true) { {
{ int a = 2, b=20; int a;
do this; if (a>5) clrscr();
do this; { printf(“Enter number”);
.. b= b + 30; scanf(“%d”, &a) ;
a= a – 5; if (a%2 == 0)
}
} {
else
else printf(“NUMBER IS EVEN”);
{ { }
do this; b = b + 50; else
do this; a = a – 15; {
.. } printf(“NUMBER IS ODD”);
} printf(“%d%d”,a, b); }
} getch();
}
Nested if statement
SYNTAX of nested if #include<stdio.h>
void main()
Example:
{ #include<conio.h>
if (condition is true) int a = 10, b=20, c=30; void main()
{ if (a<b) {
if (condition is true) { int a = 13, b = 3, c = 2;
{ if (a<c) clrscr();
if (condition is true) { if (a%2 != 0)
{ if (a%2==0) {
{ if ( a == b*b + c*c)
if (condition is true)
printf(“a is smallest as {
{
well as a is an even if (b>c)
do this; {
number”);
do this; } printf(“ALL IS WEll”);
.. } }}}
}}}} } getch();
} }
Nested if else statement
SYNTAX of nested if else void main()
Example: {
int a ;
if (condition is true) printf(“Enter value of a”);
{ do this; scanf(“%d”, &a);
if (a < 0 )
.. {
} printf(“NEGATIVE INTEGER”);
else }
else
{ {
if (condition is true) if (a>0)
{
{ do this; }
printf(“NATURAL NUMBER”);
else }
{ else
{
do this; printf(“ZERO IS A WHOLE NUMBER”);
.. }
} }
}
CONDITIONAL OPERATOR
Conditional operator (?:) works upon three arguments. That’s why It is also
known as ternary operator.
• It returns one value if condition is true, returns another value if condition is
false.
SYNTAX: EXPRESSION 1 ? EXPRESSION 2 : EXPRESSION 3
Generally, Expression 1 is Condition, Expression 2 is returned if condition is true
and Expression 3 is returned if condition is false.
For Example,

void main() If(a==10) void main()


{ b=20; {
int a = 10, b; else int a = 10, b=40, c=80, large;
b = (a == 10 ? 20 : 30); b=30; large=(a>b)?(a>c?a:c):(b>c?b:c);
} }
LOGICAL OPERATORS
&& Logical AND void main()
{
|| Logical OR int m1,m2,m3,m4,m5,per ;
printf(“Enter marks of five subjects ”);
scanf(“%d%d%d%d%d”, &m1,&m2,&m3,&m4,&m5);
! Logical NOT per = (m1+m2+m3+m4+m5)/5;
if(per>=60)
printf(“FIRST DIVISION”);
if(per>=50 && per<60)
printf(“SECOND DIVISION”);
if(per>=40&&per<50)
printf(“THIRD DIVISION”);
If(per<40)
printf(“FAIL”);
}
else if statement
SYNTAX of nested else if if (condition)
else if ladder: {
do this;
if (condition) }
{ else if(condition)
do this; {
do this;
} }
else if(condition) else if(condition)
{ {
do this;
do this; }
} else if(condition)
else {
{ do this;
}
do this; else
} {
do this;
}
Example of else if statement
Program to find division according to marks obtained.
void main()
{
int m1,m2,m3,m4,m5,per ;
printf(“Enter marks of five subjects ”);
scanf(“%d%d%d%d%d”, &m1,&m2,&m3,&m4,&m5);
per = (m1+m2+m3+m4+m5)/5;
if(per>=60)
printf(“FIRST DIVISION”);
else if(per>=50)
printf(“SECOND DIVISION”);
else if(per>=40)
printf(“THIRD DIVISION”);
else
printf(“FAIL”);
}
LOOP CONTROL STATEMENT
There are three types of loop instructions in C.
• for loop for (initialization ; testing ; ++ Increment
• while loop operation) Operator
{ i++; ⬄ i = i + 1;
• do while loop
do this;
SYNTAX of for loop :
do this; -- Decrement Operator
.. i--; ⬄ i = i - 1;
}
For Example, void main()
{
int i;
for(i=0 ; i<10; i++)
{
printf(“Welcome”);
}
Example
void main() void main()
{ {
int i ; int i , sum=0;
for(i=1;i<=50;i++) for(i=1;i<=100;i++)
{ {
printf(“i ”,%d); sum=sum+i;
} }
for(i=50;i>=1;i--) printf(“Sum of first 100 natural numbers is %d”, sum);
{ getch();
printf(“i ”,%d); }
}
getch();
}
Lets Understand working of loop ! for (i=1; i<=10; i++)
sum = sum + i;
void main() sum = 0 + 1; = 1
{ sum = 1 + 2; = 3
int i , sum=0; sum = 3 + 3; = 6
for(i=1;i<=10;i++)
sum = 6 + 4; = 10
{
sum=sum+i; sum = 10 + 5; = 15
} sum = 15 + 6; = 21
printf(“Sum of 1st 10 natural numbers is %d”, sum = 21 + 7; = 28
sum); sum = 28 + 8; = 36
getch(); sum = 36 + 9; = 45
} sum = 45 + 10; = 55
After this i=11, so loop will
terminate.
Factorial of a Number ! n! = n*(n-1)*(n-2)*(n-3)*(n-4)*………………….*3*2*1.
5! = 5*(5-1)*(5-2)*(5-3)*(5-4)
void main() 5! = 5*4*3*2*1
{ 5! =120
int n, i, fact=1;
Suppose n=5;
printf(“Enter Number”);
scanf(“%d”,&n); for (i=5; i>=1; i--)
for(i=n;i>=1;i--) fact = fact * i;
{ fact = 1 * 5 ; = 5
fact=fact*i; fact = 5 * 4; = 20
} fact = 20 * 3; = 60
printf(“Factorial of number is fact = 60 * 2; = 120
%d”, fact); fact = 120 * 1; = 120
getch();
After this i=0, so loop will
} terminate.
Programs
1. Write a program to find sum, division, modulus and multiplication of two
numbers.
2. WAP to find the average of three numbers.
3. WAP to check if the number entered through keyboard is even or odd.
4. WAP to find largest number among three numbers using if-else statement.
5. WAP to find largest number among three numbers using conditional operator.
6. WAP to print division obtained by a student using if else if ladder.
7. WAP to count minimum number of notes in given amount.
8. WAP to find area and perimeter of a rectangle.
9. WAP to find the area and circumference of a circle.
10. WAP to check if the given year is leap year or a common year.
11. WAP to calculate simple interest and total amount payable upon given principal
amount, rate of interest and time period.
12. WAP to print 1st 100 natural numbers and sum of 1st 100 natural numbers.
13. WAP to find the factorial of a number.
while CONTROL STATEMENT
There are three types of loop instructions in C.
while (condition)
• for loop for (initialization ; testing ;
operation) {
• while loop
{ do this;
• do while loop
do this; do this;
SYNTAX of for loop :
do this; ….
.. }
}
For Example, void main() void main()
{ {
int i=0;
int i;
while (i<10)
for(i=0 ; i<10; i++) {
{ printf(“Welcome”);
printf(“Welcome”); i++;
} }
do while CONTROL STATEMENT
do
{ void main()
do this; {
do this; i=0;
….
do
} while (condition);
{
printf(“Welcome”);
while (condition) i++;
{ } while (i<10);
do this;
do this;
….
}
NESTING OF LOOP
SYNTAX of nested for loop :

for (i=0 ; i<n ; i++) void main() void main()


{ { {
for (k=0 ; k<n ; int i; int i,k;
for(i=0 ; i<3; i++) for(i=0 ; i<3 ; i++)
k++) { {
{ printf(“Welcome”); for(k=0 ; k<3 ; k++)
do this; } {
do this; getch(); printf(“Welcome”);
.. } }
} }
} getch();
}
NESTING OF LOOP
for (i=0 ; i<3 ; i++) Output ? for (i=1 ; i<=3 ; i++) Output ?
{ 00 { 13
for (k=0 ; k<3 ; k++) 01 for (k=3 ; k<5 ; k++) 14
02 23
{ 10
{ 24
printf(“%d %d”,i,k); 11 printf(“%d %d”,i,k); 33
printf(“\n”); 12 printf(“\n”); 34
} 20 }
} 21 }
22
Factorial of a Number ! n! = n*(n-1)*(n-2)*(n-3)*(n-4)*………………….*3*2*1.
5! = 5*(5-1)*(5-2)*(5-3)*(5-4)
void main() 5! = 5*4*3*2*1
{ 5! =120
int n, i, fact=1;
Suppose n=5;
printf(“Enter Number”);
scanf(“%d”,&n); for (i=5; i>1; i--)
for(i=n;i>1;i--) fact = fact * i;
{ fact = 1 * 5 ; = 5
fact=fact*i; fact = 5 * 4; = 20
} fact = 20 * 3; = 60
printf(“Factorial of number is fact = 60 * 2; = 120
%d”, fact); fact = 120 * 1; = 120
getch();
After this i=0, so loop will
} terminate.
Factorial of a Number from 1 to n ! for(k=3;k<=4;k++)
for (i=3; i>=1; i--)
void main() Suppose n=4;
{ fact = fact * i;
int n, i, k, fact=1; for(k=1;k<=4;k++) fact = 1 * 3 ; = 3
printf(“Enter Value of n ”); for (i=1; i>=1; i--) fact = 3 * 2 ; = 6
scanf(“%d”,&n); fact = fact * i;
for(k=1; k<=n; k++)
fact = 6 * 1 ; = 6
fact = 1 * 1 ; = 1 Factorial of 3 is 6.
{
for(i=k;i>=1;i--) Factorial of 1 is 1.
{
for(k=4;k<=4;k++)
for(k=2;k<=4;k++) for (i=4; i>=1; i--)
fact=fact*i;
} for (i=2; i>=1; i--) fact = fact * i;
printf(“\nFactorial of %d is %d”, k, fact); fact = fact * i; fact = 1 * 4 ; = 4
fact=1; fact = 1 * 2 ; = 2
} fact = 4 * 3 ; = 12
getch();
fact = 2 * 1 ; = 2 fact = 12 * 2 ; = 24
} Factorial of 2 is 2.
fact = 24 * 1 ; = 24
Programs
1. WAP to find the factorial of all numbers between 1 and n, where value of n
is to be asked from the user .
2. Write a program to count number of digits in a number entered by the user.
3. WAP to find the sum of digits of a number.
4. WAP to print the reverse of a number.
5. WAP to print multiplication table of a number.
6. WAP to print multiplication table of a each number from 1 and a limit
entered by the user.
7. WAP to check whether the given number is prime or not.
8. WAP to print all prime numbers between 1 and n, where value of n is to be
entered through keyboard.
Program using for loop
Program to print multiplication table of a number.
Output ?
void main() Suppose num=8
{ 8*1=8
int num, i; 8 * 2 = 16
printf(“Enter Number ”); 8 * 3 = 24
scanf(“%d”,&num); 8 * 4 = 32
for (i=1 ; i<=10 ; i++) 8 * 5 = 40
8 * 6 = 48
{
8 * 7 = 56
printf(“%d * %d = %d”,num, i, num*i); 8 * 8 = 64
printf(“\n”); 8 * 9 = 72
} 8 * 10 = 80
}
Program to print all multiplication tables between 1 and some limit using Nesting Of Loop.

void main() Output ? Output ? Output ?


{ Suppose limit=3 2*1=2 3*1=3
int limit, i,k; 1*1=1 2*2=4 3*2=2
printf(“Enter Number “); 1*2=2 2*3=6 3*3=9
scanf(“%d”,&limit); 1*3=3 2*4=8 3 * 4 = 12
for(k=1;k<=limit; k++) 1*4=4 2 * 5 = 10 3 * 5 = 15
{ 1*5=5 2 * 6 = 12 3 * 6 = 18
for (i=1 ; i<=10 ; i++) 1*6=6 2 * 7 = 14 3 * 7 = 21
{ 1*7=7 2 * 8 = 16 3 * 8 = 24
printf(“%d * %d = %d”,k, i, k*i); 1*8=8 2 * 9 = 18 3 * 9 = 27
printf(“\n”); 1*9=9 2 * 10 = 20 3 * 10 = 30
} 1 * 10 = 10
}
}
break keyword
Program to check whether the number is prime or not.
void main() Suppose num=7; Suppose num=25;
{ for (i=2; i<7; i++) for (i=2; i<25; i++)
int num, i; if (num%i == 0) if (num%i == 0)
printf(“Enter Number ”); break; break;
scanf(“%d”,&num);
if(7%2==0) = False if(25%2==0) = False
for (i=2 ; i<num ; i++)
{ if(7%3==0) = False if(25%3==0) = False
if (num%i==0) if(7%4==0) = False if(25%4==0) = False
break; if(7%5==0) = False if(25%5==0) = True
} if(7%6==0) = False break;
if(i==num) After this i=7, so loop will so loop will terminate.
printf(“Prime Number”); terminate. and till now, value of i=5,
else if(i==num) if(i==num)
printf(“Not Prime”); if(7==7) = True, So Prime if(5==25) = False, So Not
} Number will be printed. Prime will be printed.
break keyword
Program to print all prime numbers between 1 and n.
void main()
{
int n, i, k;
printf(“Enter Number ”);
scanf(“%d”,&n);
for (k=1 ; k<=n ; i++)
{
for (i=2 ; i<k ; i++)
{
if (k%i==0)
break;
}
if(i==num)
printf(“%d is Prime Number ”);
}
}
Thank You !

You might also like