5.4.c Prog
5.4.c Prog
main()
INPUT
{ A,B
int A,B,Sum;
Sum=A+B
scanf("%d%d",&A,&B);
Sum=A+B;
PRINT
printf("%d",Sum); Sum
}
Stop
#include<stdio.h> Header file/
Link section
Main function
declare
main()
start {
int A,B,Sum; Variable Declaration
Input scanf(“%d%d”,&A,&B);
Sum=A+B; Calculate sum
printf(“%d”,Sum);
Show result
End }
Stem: JB 2016
Isthique Ahmed tried to write a C program for
adding 3 numbers. After failing several times
he took help from his elder brother who is a
software engineer. His brother describe the
necessary code to Ishtique and he managed
to solve the problem.
C) write a program to solve the problem in
the stem.
D) how can you show the way to represent
the procedure easily.
#include<stdio.h>
int main()
{
int a,b,c,S;
scanf(“%d%d%d",&a,&b,&c);
S=a+b+c;
printf("%d",S);
return 0;
}
Stem: Draw FC
#include<stdio.h>
int main()
{ int N1,N2,N3, S;
printf(" Give 3 numbers");
scanf("%d %d %d" , &N1, &N2, &N3);
S=N1+N2+N3;
printf("The sum is =%d",S);
return 0;
}
Flowchart for adding 3 numbers
Start
INPUT
N1, N2, N3
Sum=N1+N2+N3
PRINT
Sum
Stop
CB 2019: c)Draw FC for fig 1
d)which is better between 1 and 2
#include<stdio.h> #include<stdio.h>
main() main()
{ {
int a=10,b=15,c; int a,b,c;
scanf("%d%d ", &a,&b);
c=a+b;
c=a+b;
printf("%d",c);
printf("%d",c);
}
}
Figure-1 Figure-2
calculating the average of three
numbers
#include<stdio.h>
main( ) {
int A,B,C,S;
float Avg;
scanf ("%d %d %d", &A, &B, &C);
S= A + B + C;
Avg=S/3;
printf ("%f ", Avg);
}
A single program that calculate summation,
subtraction, multiplication and division
#include<stdio.h>
main()
{
int a,b;
scanf("%d%d ", &a,&b);
printf("%d \n ",a+b);
printf("%d \n ",a-b);
printf("%d \n ",a*b);
printf("%d \n ",a/b);
}
Write a C Program for determining Area of a Rectangle
#include<stdio.h> Start
main()
{ Input w,h
int w,h,area;
area=w*h
scanf("%d%d", &w, &h);
area=w * h ; Print area
printf(" %d ", area);
} Stop
Write a C Program for determining Area of a square
#include<stdio.h>
#include<math.h>
main()
{
int A, Area;
scanf("%d", &A);
Area=pow(A,2);
printf(" %d ", Area);
}
Write a C Program for determining Area of a Triangle
#include<stdio.h>
main() Start
{
READ B,H
int b, h;
float area;
Area= ½*B*H
scanf ("%d %d", &b, &h);
area = 1/2 * b* h ;
PRINT Area
printf (" %f ", area);
}
Stop
Area of a triangle (depends on the 3 sides of traingle)
A+B+C
S=
2
Area = S(S-A)(S-B)(S-C)
Write a C Program for determining Area of a triangle(3
sides of the triangle should be taken from the keyboard)
#include<stdio.h>
#include<math.h> // for ‘sqrt’ function
main()
{
int a, b, c;
float s, Area;
scanf("%d %d %d", &a, &b, &c);
s=(a+b+c)/2;
Area = sqrt(s*(s-a)*(s-b)*(s-c));
printf("%f",Area);
}
Area of a Circle
Start
#include<stdio.h>
main()
READ R
{
int r;
Area=3.1416*R*R
float area;
scanf (" %d ", &r);
area = 3.1416 * r * r ; PRINT Area
printf (" %f ", area);
} Stop
Write a C Program for determining Area of a
circle using ( #define preprocessor )
#include<stdio.h>
Preprocessor
#define pi 3.1416
main()
{
int r;
float area;
scanf (" %d ", &r);
area = pi * r * r ;
printf (" %f ", area);
}
Write a C Program for determining Area of a
circle(using const keyword )
#include<stdio.h>
main()
{
const float pi=3.1416;
int r;
float area;
scanf (" %d ", &r);
area = pi * r * r ;
printf (" %f ", area);
}
Mother's age is 3 times higher than son. Father's age is more
than 5 years of Mother’s age. Age of the son is X
#include<stdio.h>
main()
{
int x,m,f;
scanf("%d",&x);
m=3*x;
f=3*x+5;
printf("%d %d",m,f);
}
#include<stdio.h>
main()
{
int F;
float C;
scanf ("%d” ,&F);
C=(F-32)/1.8;
printf("%f",C);
}
Brother of Arif is suffering from fever. So Arif took a
clinical thermometer and checked the temperature.
The thermometer had the facility to measure both
Fahrenheit and Celsius. In Fahrenheit the
temperature shows 1000F.
Write a C program to convert his temperature in
Celsius.
main() {
int F=100;
float C;
C=(F-32)/1.8;
printf("%f",C); }
Convert from C to F
#include<stdio.h>
main()
{
float F,C;
scanf("%f",&C);
F=1.8* C+32;
printf("%f",F);
}
There are 20 apples In a basket. By
measuring I got weight of 3 apples
respectively 150g, 175g and 210g.
if statement
if-else statement
if-else if-.....else statement
switch-case statement
1) if statement
used to apply only one condition
Performs task if the condition is true
Syntax/structure:
if (conditional)
{
statement(s); Condition True
}
#include<stdio.h>
50
main() Pass
{
int m; 20
scanf("%d",&m);
if(m>=33)
Condition
{
printf("Pass"); Statement
}
}
#include<stdio.h>
main() 50
{ Pass
int m;
scanf("%d",&m);
if(m>=33) 20
{
printf("pass");
Fail
}
if(m<33)
{
printf("Fail");
}
}
Flowchart for determining bigger
between two numbers
Start
INPUT
A,B
No Yes
Is A>B?
Print B Print A
Stop
#include<stdio.h>
main()
{
int A,B; 5
scanf(“%d%d”, &A, &B); 7
if(A>B) 7
{
printf(“%d”,A); 59
} 17
if(B>A) 59
{
printf(“%d”,B);
}
}
Larger between 2 integer by using if.
Also consider if they are equel
if(a>b)
{
printf(“%d”,a);
}
if(b>a)
{
printf(“%d”,b);
}
if(a==b)
{
printf(“A and B are equal”);
}
Determine ifa number is positive, negative or
Zero.
Start
INPUT
X
No Yes
X=0?
No Yes
X>0?
Stop
main(){
int x;
scanf("%d",&x);
if(x==0)
{
printf("Zero");
}
if(x>0)
{
printf("positive");
}
if(x<0)
{
printf("Negative");
}
}
Largest among 3 numbers
#include<stdio.h>
main()
{
int a,b,c;
scanf(“%d%d%d”, &a,&b,&c);
if((a>b)&& (a>c))
{
printf(“%d”,a);
}
if((b>a) && (b>c))
{
printf(“%d”,b);
}
if((c>a) && (c>b))
{
printf(“%d”,c);
}
}
Stem:
You will enter any number from 0 to
7(integer)
Computer will show the spelling
If take 0 output will be Zero
If take 1 output will be One
………………………………….
If take 7 output will be Seven
main()
{
int x;
scanf("%d",&x);
if (x==0)
{
printf("Zero");
}
if (x==1)
{
printf("One");
}
if (x==2)
{
printf(“Two");
}
if (x==3)
{
printf(“Three");
}
if (x==6)
{
printf(“Six");
}
if (x==7)
{
printf(“Seven");
}
if (x==4)
{
printf(“Four");
}
if (x==5)
{
printf(“Five");
}
if ((x>7)||(x<0))
{
printf(“Wrong Number");
}
Compare 2 numbers for determining whether
they are equal
#include<stdio.h>
int main()
{
int n1=5,n2=5;
if (n1==n2)
printf("the numbers are equal");}
return 0;
}
Compare 2 numbers for determining whether
they are equal
#include<stdio.h>
int main()
{
int n1=5,n2=7;
if (n1==n2)
printf("the numbers are equal");}
return 0;
}
#include<stdio.h>
main()
{
int n1,n2;
scanf(“%d%d”,&n1,&n2);
if (n1==n2)
printf("the numbers are equal");
if (n1!=n2)
printf("the numbers are NOT equal");
}
Syntax of if-else
if (condition)
{ Condition True
Statement1(s);
}
else
{
Statement2(s);
}
Condition False
Using if-else statement
#include<stdio.h>
main() { int n1=5,n2=5;
if (n1==n2)
{
printf("the numbers are equal");
}
else
{
printf("the numbers are NOT equal");
}
}
#include<stdio.h>
main() {
int n1,n2;
scanf(“%d%d”,&n1,&n2);
if (n1==n2)
{
printf("equal");
}
else
{
printf("NOT equal");
}
}
Start
INPUT
A,B
False True
Is A>B?
Print B Print A
Stop
Find the larger between 2 numbers
#include<stdio.h>
main() Condition
{
int A,B;
scanf("%d%d",&A,&B);
if (A>B)
{ statement1
printf("%d",A);
}
else
{
printf("%d",B);
} statement2
}
Write a program to input user age and check if S/he is eligible to vote in
Bangladesh.
(A person in Bangladesh is eligible to vote if S/he is 18+).
#include<stdio.h>
main()
{
int Age;
scanf("%d",&Age);
if(Age>=18)
{
printf("Voter");
}
else
{
printf("NON Voter");
}
}
#include<stdio.h>
main()
{
int Age;
scanf(“%d”,&Age);
if(Age>=18)
{
printf(“%d Voter”, Age);
}
else
{
printf(“%d Not Voter”, Age);
}
}
Find smaller between 2 integer number
#include<stdio.h>
main()
{
int a,b;
scanf("%d %d",&a,&b);
if (a<b)
{
printf("%d",a);
}
else
{
printf("%d",b);
}
}
Determine pass or fail
main()
{
Condition
int m;
scanf("%d",&m);
if (m>=33)
{ Statement1
printf("Pass");
}
else
{
printf("Fail"); Statement2
}
}
Positive or Negative?
#include<stdio.h>
main()
{
int x;
scanf("%d",&x);
if (x>=0)
{
printf(“Positive");
}
else
{
printf(“Negative");
}
}
#include<stdio.h>
main()
Start
{
int X,R; INPUT X
scanf("%d",&X);
R=X MOD 2
R=X%2;
if(R==0)
No Yes
{ R=0?
printf("Even”);
} Print Odd Print Even
else
{
printf("Odd "); Stop
}
}
#include<stdio.h>
main()
{
int X;
scanf("%d",&X);
if(X%2==0)
{
printf("Even”);
}
else
{
printf("Odd ");
}
}
Stem: which one is better ? explain
with example
if (condition)
{
if (condition) Operation1;
{ }
Operation1; else
} {
Operation2;
}
Say, eligible for applying for a job is
limited to 18 to 35.
#include<stdio.h>
main()
{
int Age;
scanf("%d",&Age);
if ((Age>=18) && (Age<=35))
{
printf ("Eligible");
}
else
{
printf("Not Eligible");
}
}
A super shop offers a special package
before new year. If any customer purchase
goods of tk 5000 or less, he will get 20%
discount. But if he buys products of more
than tk 5000, he will get 25% discount. Draw
a flowchart for determining discount.
Purchased Discount Discount
Amount percentage amount
5000 or less 20% D=M*20/100
Input M
N Y
M ≤ 5000
D=M*0.25 D=M*0.20
Print D
STop
main()
{
float M,D;
scanf("%f",&M);
if (M <= 5000)
{
D=M*0.20;
}
else
{
D=M*0.25;
}
printf("%f",D);
Determine whether a year is Leap
Year or Not Leap Year
if(y%400 =0)
OR
Read
Read Y
Y
No Y% 400=0 Yes
or
(Y %4=0) AND (Y%100≠0)
Print Print
Not Leap Year Leap Year
Stop
main()
{
int y;
scanf("%d",&y);
if((y%400==0)||((y%4==0)&&(y%100!=0)))
{
printf("Leap year");
}
else
{
printf( "Not Leap year");
}
}
Sushmita’s teacher asked her about her date
of birth so she replayed 29/02/2000. the
teacher told her that she will not able to
celebrate her birthday in every year and also
make her understand by using formula which
years she can celebrate her birthday.
INPUT
A,B,C
No Yes
A>B?
Yes No No Yes
B>C? A>C?
Stop
main()
{
int A,B,C;
scanf(“%d%d%d”,&A,&B,&C);
if (A>B)
{
if(A>C)
printf(“%d ”,A);
else
printf(“%d ”,C);
}
else
{
if (B>C)
printf(“%d ”,B);
else
printf(“%d ”,C);
}
}
3. if -else if -………else statement
if (condition-1)
{
Statment1(s);
}
else if(condition-2)
{
Statment2(s);
}
else if(condition-3)
{
Statment3(s);
}
.......................
........................
else
{
Another Statement;
}
Start
n1=?
n2=?
n1=n2 Yes
They are equal
No
Yes
n1>n2 n1 is greater
No
n2 is greater
Stop
#include<stdio.h>
main()
{
int n1,n2;
scanf("%d%d",&n1,&n2);
if (n1==n2)
{
printf("equal");
}
else if (n1>n2)
{
printf("n1 is greater" );
}
else
{
printf("n2 is greater");
}
}
Write a C program to input day number
of week and print week day
Day Day name
Number
Input : 1
1 Saturday Output: Saturday
2 Sunday
Input : 6
3 Monday Output: Thursday
4 Tuesday
5 Wednesday
6 Thursday
7 Friday
#include<stdio.h>
main()
{
int x;
scanf(“%d”,&x);
if (x==1)
printf(“Saturday”);
else if (x==2)
printf(“Sunday”);
………
else if (x==7)
printf (“Friday”);
else
printf(“Invalid number”);
}
determine if a number is positive, negative or
Zero.
Start
INPUT
X
No Yes
X=0?
No Yes
X>0?
Stop
main() {
int x;
scanf("%d",&x);
if(x==0)
{
printf("Zero");
}
else if(x>0)
{
printf("positive");
}
else
{
printf("Negative");
}
}
Stem: which one is better ? explain
with example
if (condition1)
{
if (condition) Operation1;
{ }
Action1; else if (condition2)
} {
else Operation2;
{ }
...................
Action2;
else {
} another Operation;
}
Fig-1
Fig-2
Flowchart to determine the largest
among three numbers
Start
INPUT
A,B,C
No Yes
A>B & A>C?
Print C Print B
Stop
Flowchart for determine the largest
among three numbers
Start
INPUT
A,B,C
No Yes
A>B?
Yes No No Yes
B>C? A>C?
Stop
#include<stdio.h>
main()
{
int A,B,C;
scanf("%d%d%d",&A,&B,&C);
if ((A>B) && (A>C))
{
printf("%d",A);
}
else if ((B>A) && (B>C))
{
printf("%d",B);
}
else
{
printf("%d",C);
}
}
#include<stdio.h>
main()
{
float A,B,C;
scanf("%f%f%f",&A,&B,&C);
if ((A>B) && (A>C))
{
printf("%f",A);
}
else if ((B>A) && (B>C))
{
printf("%f",B);
}
else
{
printf("%f",C);
}
}
Stem: write C Program for the
flowchart
Start
INPUT
A,B,C
No Yes
A<B?
Yes No No Yes
B<C? A<C?
Print X
Stop
main()
{ int A,B,C,X;
scanf("%d%d%d",&A,&B,&C);
printf(“%f”,c);
}
Stem
80 to 100
A+
70 to 79
A
60 to 69
A-
50 to 59
B
40 to 49
C
33 to 39
D
Otherwise
F
Find the letter grade for a student
i.e. 90 and above A+ , from 80 to 79 A etc
#include<stdio.h>
#include<conio.h>
main()
{
int m;
scanf(“%d”,&m );
if ((m>=80) &&(m<=100))
{
printf("A+");
}
else if ((m>=70)&&(m<=79))
{
printf("A");
}
else if ((m>=60) &&(m<=69))
{
printf("A-");
}
else if ((m>=50)&& (m<=59))
{
printf("B");
}
else if ((m>=40)&&(m<=49))
{
printf("C");
}
else if (m>=33)&&(m<=39))
{
printf("D");
}
else
{
printf("F");
}
getch();
}
Takes any integer number from 0 to 7 and show
the result in text.
Example: if input is 4 then it will show Four
Give a number:4
Four
Give a number: 1
One
Takes any integer number from 0 to 7 and show the
result in text/spelling .
Example: if input is 4 then it will show Four
#include<stdio.h>
main()
{
int x;
printf(“Give a Number”);
scanf("%d",&x);
if (x==0)
{
printf("Zero");
}
else if (x==1)
{
printf("One");
}
else if (x==2)
{
printf("Two");
}
else if (x==3)
{
printf("Three");
}
else if (x==4)
{
printf("Four");
}
else if (x==5)
{
printf("Five");
}
else if (x==6)
{
printf("Six");
}
else if (x==7)
{
printf("Seven");
}
else
{
printf("Wrong Number");
}
getch();
}
In annual sports students of eleven class
are divided in three groups A, B and C.
Students having roll no 1 to 30 will be in
group "A", 31 to 60 will be in group "B" and
61 to 100 will be in group "C“
Write down the algorithm to make the
group as mentioned in stem.
Write down a program in "C" language
using conditional statement to make the
group.
main(){
int R;
scanf(“%d”,&R);
if((R>=1)&&(R<=30))
printf(“Group A”);
else if((R>=31)&&(R<=60))
printf(“Group B”);
else
printf(“Group C”);
}
Age Status
More than 16 Senior
13-16 Intermediate
Below 13 Junior
#include<stdio.h>
main()
{ int Age;
scanf(“%d”,&Age);
if(Age>16)
printf(“Senior”);
if((Age>=13)&&(Age<=16))
printf(“Intermidiate”);
if(Age<13)
printf(“Junior”);
}
Looping/Iteration
for loop
while loop
do-while loop (do loop)
go to loop
Draw a Flowchart to display the word
“Dhaka” For 10 times
#include<stdio.h> Start
main() i=1
{
int i; No
for(i=1; i<=10; i=i+1) i≤10?
{
Yes
printf("\n Dhaka");
Print
} Dhaka
Stop
i=i+1
}
stem
Today programming class of class XII is
completed. ICT teacher gave the students an
assignment that print the word “Bangladesh”
5 times. Some students submitted the
assignment by using printf function 5 times
but ICT teacher did not like it told them that
the program is unnecessarily large and it can
be done in a short form.
How the students wrote the C program
How the ICT teacher make it shorter ?
include<stdio.h>
main()
{
printf(“Bangladesh");
printf(“Bangladesh");
printf(“Bangladesh");
printf(“Bangladesh");
printf(“Bangladesh");
}
#include<stdio.h>
main()
{
int i;
for(i=1; i<=5; i=i+1)
{
printf(“Bangladesh");
}
}
Draw a Flowchart to display the word
“Dhaka” For Nth times
#include<stdio.h>
main()
{
int i,N;
scanf(“%d”,&N);
for(i=1; i<=N; i=i+1)
{
printf("\n Dhaka");
}
}
Output=? Write C program for this FC
Ans:
Start
2
i=2
5
8
No
i≤23? 11
Yes 14
Print
i 17
Stop
i=i+3 20
23
#include<stdio.h> Ans:
2
main() 5
{
8
int i;
11
for(i=2;i<=23;i=i+3)
{ 14
printf(“\n %d”,i); 17
}
20
}
23
Stem
Identify and explain the program written
below:
Initialization Counter Update
}
Structure/Syntax of for loop
main()
{
int i;
for(i=1; i<=10; i=i+1)
{
printf("Dhaka");
}
}
RB-2016
See the series given below and answer
the question:
#include<stdio.h>
main()
{
int i;
for(i=10; i<=100; i=i+10)
{
printf("%d", i);
}
}
Mr. X joined in a company in contractual
basis on 1/1/2018. Since his job was
contractual, he need to attend office only 1
day in every four day.
}
Jb19: show structure of the loop
show and explain output
#include<stdio.h>
main() OUTPUT:
20
{ 25
Initialization Counter Update
int i; 30
35
for(i=20;i<=50;i+5)
Condition
40
{ 45
printf(“%d”,i); 50
Body of the loop
}
}
What is the Sum of the series:
2+3+4+5
#include<stdio.h>
Start
main()
{ S=0
i=2
int i, S;
S=0; No
i≤5?
for(i=2; i<=5; i=i+1)
{ Y
S=S + i
S=S+i;
} PRINT S
i= i+1
printf(“%d”, S); Stop
}
3+5+7+. . .. . . . . .. . . . .. .+99
#include<stdio.h>
main() {
int i, S; Counter Update
Initialization
S=0;
for(i=3; i<=99; i=i+2)
{ Condition
S=S+i;
Body of the loop
}
printf(“%d”, S);
}
10+13+16+………………..+115=?
#include<stdio.h>
#include<conio.h>
main()
{
int i,S=0;
S=S+i;
Body of the loop
}
Stem:what kind of loop is used? Explain.
#include<stdio.h>
main()
{
Counter Update
int i,S; Initialization
S=0;
for (i=1; i<=100; i=i+1)
{ Condition
S=S+i; Statement
}
printf(“%d”,S);
}
10+15+20+. . .. . . . . .. . . . .. .+N
#include<stdio.h>
main()
{
int i,s,N;
scanf("%d",&N);
s=0;
for(i=10; i<=N; i=i+5)
{
s=s+i;
}
printf("%d", s);
}
10+15+20+. . .. . . . . .. . . . .. .+N
#include<stdio.h>
main()
{
int i,s,N;
printf("Give value of last element or N");
scanf("%d",&N);
s=0;
for(i=10; i<=N; i=i+5)
{
s=s+i;
}
printf("The result is =%d", s);
}
Rewrite the C program that user
can give input of the last element
#include<stdio.h>
main()
{
int i,s=0;
for(i=1; i<=20; i=i+2)
{
s=s+i;
}
printf(“Summation is =%d”, s);
#include<stdio.h>
main()
{
int i,s=0,N;
scanf(“%d”,&N);
for(i=1; i<=N; i=i+2)
{
s=s+i;
}
printf(“Summation is %d”,S);
}
Stem: Write C program for the Pseudo code
given below.
Step1: INPUT N
Step2: S=0, i=5
Step3: if i>N goto Step 5 else goto step 4
Step4: S=S+i, increment value of i by 3 and
repeat step3
Step5: PRINT S
#include<stdio.h>
main()
{
int i,s,N;
scanf(“%d”,&N);
s=0;
for(i=5; i<=N; i=i+3)
{
s=s+i;
}
printf(“%d”, s);
}
22 + 3 2 + 4 2 +. . . . . . . . . . . + 202
#include<stdio.h>
#include<math.h>
main()
{
int i, s;
s=0;
for(i=2; i<=20; i=i+1)
{
s=s+pow(i,2);
}
printf(“%d”, s);
}
14 +34 +54 +- - -- -- ---- --- -- ---+N4
#include<stdio.h>
#include<math.h>
main()
{
int i, s,N;
scanf(“%d”,&N);
s=0;
for(i=1; i<=N; i+=2)
{
s=s+pow(i,4);
}
printf(“%d”, s);
}
1 +33 +55 +- - -- -- ---- --- -- ---+9999
#include<stdio.h>
#include<math.h>
main()
{
int i, s;
s=0;
for(i=1; i<=99; i+=2)
{
s=s+pow(i,i);
}
printf(“%d”, s);
}
1 +33 +55 +- - -- -- ---- --- -- ---+9999
#include<stdio.h>
#include<math.h>
main()
{
int i, s;
s=0;
for(i=1; i<=99; i+=2)
{
s=s+i^i;
}
printf(“%d”, s);
}
12 + 3 4 + 5 6 + . . . . . . . . . . . + NN+1
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
clrscr();
int i, S,N;
S=0;
scanf(“%d”,&N);
for(i=1; i<=N; i=i+2)
{
S=S+pow(i,i+1);
}
printf(“%d”, S);
getch();
}
200+199+198+. . .. .. . +10
#include<stdio.h>
main()
{
int i, S;
S=0;
for(i=200; i>=10; i=i- -)
{
S=S+i;
}
printf(“%d”, S);
}
502+482+462+……………….+N2
#include<stdio.h>
#include<math.h>
main()
{
int N;
scanf(“%d”,&N);
int S=0;
for(int i=50; i>=N; i=i-2)
{
S=S+pow(i,2);
}
printf(“%d”, S);
}
1*2*3*……..*10
#include<stdio.h>
main()
{
int i, M;
M=1;
printf(“%d”, S);
}
On the date of 12/08/2016 the ICT teacher was
discussing in the class on the Program the
solution to the general problems to make the
computer know the programming language of a
generation with the help of computer and told
that in the next class he would teach how to find
out the result of a number of digit in a series with
the help of C programming. Next day after
writing the series 221 + 223 + 225 + ------- + N on
the black board.
(c) Make the program of finding out the result of a
series mentioned in the Stimuli with the help of C
language.
(d) Which of the programs mentioned on the date
in Stimuli by the ICT teacher? Do you think is
more useful? Illustrate your answer.
221+223+225+---------------+N
main()
{
int i,S,N;
scanf(“%d”,&N);
S=0;
for(i=221; i<=N; i=i+2)
{
S=S+i;
}
printf(“%d”, S);
}
#include<stdio.h> Draw FC and Algorithm for the
C program
#include<conio.h>
main()
{
int a,s=0,n;
printf(“Value of n:”);
scanf(“%d”,&n);
for(a=1;a<=n;a++)
{
s=s+a*a;
}
printf(“Sum:=%d”,s);
}
Factorial any positive Integer
Number = N!
N*(N-1)(N-2)(N-3)*…..*1
Start
INPUT N
F=1
i=N
No
i≥1?
Yes
F=F*i
PRINT F
i= i-1
Stop
Factorial
N!=N*(N-1)*(N-2)*…………….*1
#include<stdio.h>
main()
{
int N,F,i;
F=1;
scanf("%d",&N);
for(i=N;i>=1;i=i--)
{
F=F*i;
}
printf("%d", F);
}
Stem: c) write the FC for the any given value of n
d)if N=4 the result will be 24. show this step by step
main()
Enter the Number:
{ 4
int f=1,N;
Value of the given number 24
printf(“\n Enter the Number:”);
scanf(“%d”,&N);
for(int i=1;i<=N;i++)
{
f=f*i;
}
printf(“\n value of the given number =%d”,f);
}
Stem: C) Draw Flowchart
D) How can we get 13, 19, 25, 31, 37, 43, 49
by changing the C program next
void main()
{
int c,s=0;
for(c=0;c<=100;c++)
s=s+c;
printf(“the Total is %d”,s);
}
Start
S=0
C=0
No
c≤100?
Yes
S=S+C
PRINT S
C= C+1
Stop
#include<stdio.h>
main()
{
int s,a,b,c;
scanf(“%d%d%d”,&a,&b,&c);
s=a+b+c;
printf(“%d”,s);
}
Use loop and summation of 10 numbers
Start
i=1
S=0
N
i≤10
input X Print S
Stop
S=S+X
i=i+1
#include<stdio.h>
main(){
int i,s=0,x;
for (i=1;i<=5;i=i+1)
{
scanf("%d",&x); Body of the loop
s=s+x;
}
printf("%d",s);
}
Try your self
1) Write a C program to calculate the sum of the all even
numbers from 1 to 100
2) Write a C program to calculate the sum of the all Odd
numbers from 1 to N
3) Write a C program to calculate the value of the following
series:
i)13+23+33 . ………....………….+1333
ii) 33+44+55+. . ………....…..….+103103
iii) 23+45+67+ ………..………….+100101
iv) 1+3+9+27+81+………………+N
v) 20+21+22+23+24+……………………+28
vi) 100+98+96+…………………..+N
1 1 1 1
vii) 1 .......... ...
2 3 4 N
S=S+i; Statement
i=i+2;
}
printf("%d", S);
Counter update
}
10,20,30 ,…………….50
#include<stdio.h>
main()
{
int i;
i=10;
while(i<=50)
{
printf(“\n %d", i);
i=i+10;
}
}
2*5*8*…………………………*N (use while)
#include<stdio.h>
main()
{
int i,M,N;
scanf(“%d”,&N);
Initialization
M=1;
i=2;
while(i<=N) Condition
{
M=M*i; Statement
#include<stdio.h>
main()
{
int i,S;
S=0;
i=100;
while(i>=10)
{
S=S+i;
i=i-1;
}
printf("%d", S);
}
Show the word “Bangladesh” for 20 times
using while
#include<stdio.h>
main()
{
int i;
i=1;
while(i<=20)
{
printf(“Bangladesh”);
i=i+1;
}
}
Stem: c) how to calculate sum of all even numbers
up to n by following the syntax of the stem?
d) show the procedure to explain the stem simply
#include<stdio.h>
void main()
{
int i,S=0, i=1,n;
printf(“Enter the value of Total Number:”);
scanf(“%d”,&n);
while(i<=n)
{
S=S+i;
i=i++;
}
printf(“the sum of Total number is%d\n", S);
getch();
}
Is it possible to get same result by while loop?
#include<stdio.h>
main()
{
int i,S,N;
scanf(“%d”,&N);
S=0;
for(i=1;i<=N;i+=2)
{
S=S+i;
}
printf("%d", S);
}
main()
{
int i,S,N;
scanf(“%d”,&N);
S=0;
i=1;
while(i<=N)
{
S=S+i;
i+=2;
}
printf("%d", S);
}
do while loop
Initialization;
do
{
Statement(s); Body of the
Counter Update; Loop
} while(Condition);
2+5+8+.......................+N
main()
{
int i,S,N;
scanf(“%d”,&N);
S=0; Initialization
i=2;
do
{
Statement
S=S+i;
i=i+3; Condition
}while(i<=N);
printf("%d", S); Counter update
}
3+5+7+.......................+99
main()
{
int i,S;
S=0;
i=3;
do
{
S=S+i;
i=i+2;
}while(i<=99);
printf("%d", S);
}
2+4+6+8 . . . . . . . . . . . .. . . . . . .. +200 by while
#include<stdio.h>
#include<conio.h>
main()
{
int i,S;
S=0;
i=2;
while(i<=200)
{
S=S+i;
i=i+2;
}
printf("%d", S);
getch();
}
Stem: Draw FC
can you apply do loop producing same result?
analyze
#include<stdio.h>
main()
{
int i, S;
S=0;
for(i=1; i<=100; i=i++)
{
S=S+i;
}
printf(“Total %d”, S);
}
#include<stdio.h>
main()
{
int i, S;
S=0;
i=1;
do
{
S=S+i;
i=i++;
} while(i<=100);
printf(“Total %d”, S);
}
Stem: Draw FC, what change do you need to
make for using while loop ?
#include<stdio.h>
main()
{
int SUM,N,i;
printf(“Enter Last Number”);
scanf(“%d”,&N);
SUM=0;
for(i=1; i<=N; i=i+3)
{
SUM=SUM+i;
}
printf(“Result: %d”, SUM);
}
100+99+98+……………..+10
#include<stdio.h>
main()
{
int i,S;
S=0;
i=100;
do
{
S=S+i;
i=i-1;
}
while(i>=10);
printf("%d", S);
}
Example: 2+3+4+.. . . .. . .+10
#include<stdio.h> #include<stdio.h>
main() main()
{ {
int i,S; int i,S;
S=0; S=0;
i=2; i=2;
while(i<=10) do
{ {
S=S+i; S=S+i;
i=i+1; i=i+1;
} } while(i<=10);
printf("%d",S); printf("%d",S);
} }
What about this?
#include<stdio.h> #include<stdio.h>
main() main()
{ {
int i,S; int i,S;
S=0; S=0;
i=2; i=2;
while(i<=1) do
{ {
S=S+i; S=S+i;
i=i+1; i=i+1;
} } while(i<=1);
printf("%d",S); printf("%d",S);
} }
Stem: modify the program to print Computer Science
at lest once even after condition is false
#include<stdio.h>
main()
{
int i;
for (i=1; i<=100; i=i+1)
{
printf(“Computer Science”);
}
}
#include<stdio.h>
main()
{
int i=1;
do
{
printf(“\n Computer Science”);
i=i+1;
} while(i<=100);
}
Stem: FC & algorithm
void main()
{
int i,S=0, i=1,n;
printf(“Enter the value of Total Number:”);
scanf(“%d”,&n);
while(i<=n)
{
S=S+i;
i=i++;
}
printf(“the sum of Total number is%d\n", S);
}
If C=1 and C=C+1
Is replaced by C=2 and
Input N C=C+2 than write a C
program
Let, S=0, C=1
Let, S=S+C
C=C+1
Yes
Is C<=N?
No
Print S
main()
{
int S=0, c=2,n;
scanf(“%d”,&n);
do
{
S=S+c;
c=c+2;
} while(c<=n);
printf(“%d", S);
}
#include<stdio.h> Use do while loop
#include<conio.h>
main()
{
int a,s=0,n;
printf(“Value of n:”);
scanf(“%d”,&n);
for(a=1;a<=n;a++)
{
s=s+a*a;
}
printf(“Sum:%d”,s);
}
1+3+5+………….+99
#include<stdio.h>
main()
{
int i,S;
S=0;
i=1;
do
{
s=s+i;
i=i+2;
} while(i<=99);
printf("%d", s);
}
Stem:c) FC
D) implement by do loop
#include<stdio.h>
void main()
{
int i,S,n;
printf("Enter Last Term");
scanf("%d",&n);
S=0;
for(i=1;i<=n; i=i+3)
{
S=S+i;
}
printf(“ Summation %d ", S);
}
2+4+6+---------------------+100
main()
{
int i,S=0;
Initialization
i=2;
MRC: S=S+i; Statement
i=i+2;
Counter update
if(i<=100) goto MRC;
printf(“%d”,S); Condition
}
Draw FC
implement by GOTO
#include<stdio.h>
main()
{
int s,c;
for(c=100;c>=40;c=c--)
{
s=s+c;
}
Printf(“%d”,s)
}
Stem: RB 2017
See the series given below and answer the
question:
{
int i=10;
tanvir:
printf(“%d”,i);
i=i+10;
if(i<=100) goto tanvir;
}
Ctg 19 : use if and goto
(90)2+ (80)2 + (70)2 -------------------------+ (20)2
main()
{
int S=0;i=90;
X:
S=S+pow(i,2);
i=i-10;
if(i>=20) goto X;
printf(“%d”,S);
}
After discussing details about C programming
ICT teacher ask the student to calculate the
GCD of 6 and 12.
C) write C program for the stem mention
above .
LCM Start
Input A,B
No Yes
A>B
X=B X=A
X%A=0 Yes
&
X%B=0?
Print LCM=X
No
X=X+1
Stop
main(){
int a,b,x;
scanf("%d%d",&a,&b);
if (a>b)
x=a;
else
x=b;
loop1: if ((x%a==0)&&(x%b==0))
printf("%d",x);
else {
x=x+1;
goto loop1;
}
}