SlideShare a Scribd company logo
1 of 26
Download to read offline
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 1
Handout#5
Assignment/Program Statement:
Write C programs using loop statements such as for, while, do-while and nested
loop.
Learning Objectives:
Students will be able to
- explain decision control statements in C
- draw the flowchart for loop statement solution
- write the algorithm loop statements
- write C code using ‘for’ statement in C
- write C code using ‘while’ statement in C
- write C code using ‘do-while’ ladder in C
- write C code using nested loop in C
Theory:
Programming languages provide various control structures that allow for more
complicated execution paths. A loop statement allows us to execute a statement or
group of statements multiple times. C programming language provides the
following types of loops to handle looping requirements.
- For loop
- While loop
- Do-while loop
- Nested loop
5-a. Write a C program using ‘for’ loop - to display character from A to Z and their
ASCII values
5-b. Write a C program using ‘while’ loop - to print factorial of given number
5-c. Write a C program using ‘do-while’ loop - to find the Fibonacci Series
5-d. Write a C program using nested loop - find the prime numbers from 2 to 100
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 2
Assignment#5a
Assignment/Program Statement:
Write a C program using ‘for’ loop - to display character from A to Z and their
ASCII values
Learning Objectives:
Students will be able to
- write the syntax of ‘for’ loop
- draw flowchart for loop statement solution
- write the algorithm for loop statement
- write the program using the ‘for’ loop
Theory:
 A for loop is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times.
 The syntax of a ‘for’ loop in C programming language is –
for (init; condition; increment)
{
Statement(s);
}
Here is the flow of control in a 'for' loop −
o The init step is executed first, and only once. This step declares and
initializes any loop control variables.
o Next, the condition is evaluated. If it is true, the body of the loop is
executed. If it is false, the body of the loop does not execute and the flow
of control jumps to the next statement just after the 'for' loop.
o After the body of the 'for' loop executes, the flow of control jumps back
up to the increment statement. This statement updates any loop control
variables. This statement can be left blank, as long as a semicolon
appears after the condition.
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 3
o The condition is now evaluated again. If it is true, the loop executes and
the process repeats itself. After the condition becomes false, the 'for' loop
terminates.
Example: C code to print first 10 numbers
for( a = 1; a <= 10; a = a + 1 )
{
printf("value of a: %dn", a);
}
[Reference: http://www.tutorialspoint.com/cprogramming/c_for_loop.htm ]
Flow Diagramfor‘for’loop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 4
Various examples of infinite ‘for’ loop
Loop structure Description
for ( i=1 ; i<=10 ; )
printf("Hello,
World!n");
Here the updating part of the counter variable i is
missing. Hence the value of i will remain 1 forever
and the loop runs infinite times printing Hello,
World!
for ( i=1 ; ; i++ )
printf("Hello,
World!n");
Here the condition part of the loop is missing. Due to
which the loop is forced to run repeatedly infinite
times printing Hello, World! As there isn’t any
condition to terminate.
for ( i=1 ; ; )
printf("Hello,
World!n");
Here the condition as well as updating part is
missing from the loop. Hence it will also iterate
infinite times printing Hello, World!
for ( ; ; )
printf("Hello,
World!n");
Here all the three parts initialization, condition as
well as updation part is missing from the loop.
Hence it will also run infinite times printing Hello,
World!
ASCII Value:
ASCII stands for American Standard Code for Information Interchange.
Computers can only understand numbers, so an ASCII code is the numerical
representation of a character such as 'a' or '@' or an action of some sort.
Below is the ASCII character table for alphabet.
Symbol Decimal Binary
A 65 01000001
B 66 01000010
C 67 01000011
D 68 01000100
Symbol Decimal Binary
a 97 01100001
b 98 01100010
c 99 01100011
d 100 01100100
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 5
E 69 01000101
F 70 01000110
G 71 01000111
H 72 01001000
I 73 01001001
J 74 01001010
K 75 01001011
L 76 01001100
M 77 01001101
N 78 01001110
O 79 01001111
P 80 01010000
Q 81 01010001
R 82 01010010
S 83 01010011
T 84 01010100
U 85 01010101
V 86 01010110
W 87 01010111
X 88 01011000
Y 89 01011001
Z 90 01011010
e 101 01100101
f 102 01100110
g 103 01100111
h 104 01101000
i 105 01101001
j 106 01101010
k 107 01101011
l 108 01101100
m 109 01101101
n 110 01101110
o 111 01101111
p 112 01110000
q 113 01110001
r 114 01110010
s 115 01110011
t 116 01110100
u 117 01110101
v 118 01110110
w 119 01110111
x 120 01111000
y 121 01111001
z 122 01111010
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 6
Flowchart for Problem Statement:
Algorithm:
1. Start
2. i=0
3. If i<26,
print the alphabet and ASCII value
i=i+1
go to step 3
else
go to step 4
4. Stop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 7
Program:
#include <stdio.h>
#include <conio.h>
int main() {
int i;
for(i = 0; i < 26; i++)
{
printf("%c = %d | %c = %d n",'A'+i,'A'+i,'a'+i,'a'+i);
}
getch();
return 0;
}
Input:
No Input
Output:
A = 65 | a = 97
B = 66 | b = 98
C = 67 | c = 99
D = 68 | d = 100
E = 69 | e = 101
F = 70 | f = 102
G = 71 | g = 103
H = 72 | h = 104
I = 73 | i = 105
J = 74 | j = 106
K = 75 | k = 107
L = 76 | l = 108
M = 77 | m = 109
N = 78 | n = 110
O = 79 | o = 111
P = 80 | p = 112
Q = 81 | q = 113
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 8
R = 82 | r = 114
S = 83 | s = 115
T = 84 | t = 116
U = 85 | u = 117
V = 86 | v = 118
W = 87 | w = 119
X = 88 | x = 120
Y = 89 | y = 121
Z = 90 | z = 122
Practice Problem Statements:
1. Write a C program to calculate the sum of first n natural numbers
2. Write a C program to find factorial of a number
3. Write a C program to print natural numbers
4. Write a C program to print natural numbers in reverse
5. Write a C program to print alphabets
Conclusion:
Thus a C program using ‘for’ loop - to display character from A to Z and their
ASCII values is implemented.
Learning Outcome:
At end of this assignment, students are able to
- write the syntax of ‘for’ loop
- draw flowchart for loop statement solution
- write the algorithm for loop statement
- write the program using the ‘for’ loop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 9
Assignment#5b
Assignment/Program Statement:
Write a C program using ‘while’ loop - to print factorial of given number
Learning Objectives:
Students will be able to
- write the syntax of ‘while’ loop
- draw flowchart for loop statement solution
- write the algorithm for loop statement
- write the program using the ‘while’ loop
Theory:
 A while loop in C programming repeatedly executes a target statement as
long as a given condition is true.
 The syntax of a while loop in C programming language is −
while(condition)
{
statement(s);
}
 Here, statement(s) may be a single statement or a block of statements. The
condition may be any expression, and true is any nonzero value. The loop
iterates while the condition is true.
 When the condition becomes false, the program control passes to the line
immediately following the loop.
[Reference: http://www.tutorialspoint.com/cprogramming/c_while_loop.htm ]
Example: C code to print first 10 numbers
a=1;
while ( a <= 20 )
{
printf("value of a: %dn", a);
a++;
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 10
}
Flow Diagram for ‘while’ loop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 11
Flowchart for Problem Statement:
Algorithm:
1. Start
2. Enter the number whose factorial need to be found
3. f=1
4. i=1
5. If i<=num
f=f*I
i=i+1
go to step 5
else
go to step 6
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 12
6. Display the factorial of the given number
7. Stop
Program:
#include<stdio.h>
int main()
{
int num,f,i;
printf("Enter a number: ");
scanf("%d",&num);
f=1;
i=1;
while(i<=num)
{
f = f * i;
i++;
}
printf("Factorial of %d is: %d",num,f);
return 0;
}
Input:
Enter a number:4
Output:
Factorial of 4 is: 24
Practice Problem Statement:
1. Write a program to print sum of all even numbers between 1 to n.
2. Write a program to print sum of all odd numbers between 1 to n.
3. Write a program to print table of any number.
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 13
4. Write a program to enter any number and calculate sum of all natural
numbers between 1 to n.
5. Write a program to enter any number and calculate sum of its digits.
Conclusion:
Thus a C program using ‘while’ loop - to print factorial of given number is
implemented.
Learning Outcomes:
At end of this assignment, students are able to
- write the syntax of ‘while’ loop
- draw flowchart for loop statement solution
- write the algorithm for loop statement
- write the program using the ‘while’ loop
At the end of this assignment, students are able to
- write the syntax of ‘while’ loop
- draw flowchart for loop statement solution
- write the algorithm for loop statement
- write the program using the ‘while’ loop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 14
Assignment#5c
Assignment/Program Statement:
Write a C program using ‘do-while’ loop - to find the Fibonacci Series
Learning Objectives:
Students will be able to
- write the syntax of ‘do-while’ loop
- draw flowchart for loop statement solution
- write the algorithm for loop statement
- write the program using the ‘do-while’ loop
Theory:
 Unlike for and while loops, which test the loop condition at the top of the
loop, the do...while loop in C programming checks its condition at the
bottom of the loop.
 A do...while loop is similar to a while loop, except the fact that it is
guaranteed to execute at least one time.
 The syntax of a do...while loop in C programming language is –
do {
statement(s);
} while( condition );
 The conditional expression appears at the end of the loop, so the
statement(s) in the loop executes once before the condition is tested.
 If the condition is true, the flow of control jumps back up to do, and the
statement(s) in the loop executes again. This process repeats until the given
condition becomes false.
Example: C code to print first 10 numbers
a=1;
do
{
printf("value of a: %dn", a);
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 15
a++;
} while ( a <=10 )
[Reference: http://www.tutorialspoint.com/cprogramming/c_do_while_loop.htm ]
Flow Diagram for ‘do-while’ loop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 16
Flowchart for Problem Statement:
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 17
Algorithm:
1. Start
2. First=0, second=1
3. Enter the number of terms in Fibonacci series
4. Display first and second term of Fibonacci series
5. If i<=n
c=first second
Display c
first = second
second = c
i=i+1
go to step 5
else
go to step 6
6. Stop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 18
Program:
#include<stdio.h>
void main()
{
int first, second, c, n, i;
clrscr();
first=0;
second=1;
i=3;
printf("Enter the number of terms: ");
scanf("%d",&n);
printf("%dn%d",first,second);
do
{
c=first+second;
printf("n%d",c);
first = second;
second = c;
i=i+1;
}while (i<=n);
getch();
}
Input:
Enter number of terms: 4
Output:
0 1 2 3
Practice Problem Statement:
1. Write a C program to find power of any number using for loop.
2. Write a C program to enter any number and print all factors of the number.
3. Write a program to display character from A to Z and their ASCII values
4. Write a C program to enter any number and calculate product of its digits.
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 19
5. Write a C program to enter any number and print its reverse.
Conclusion:
Thus a C program using ‘do-while’ loop - to find the Fibonacci Series is
implemented.
Learning Objectives:
At the end of this assignment, students are able to
- write the syntax of ‘do-while’ loop
- draw flowchart for loop statement solution
- write the algorithm for loop statement
- write the program using the ‘do-while’ loop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 20
Assignment#5d
Assignment/Program Statement:
Write a C program using nested loop – to find the prime numbers of n-terms
Learning Objectives:
Students will be able to
- write the syntax of nested loop
- draw flowchart for loop statement solution
- write the algorithm for loop statement
- write the program using the nested loop
Theory:
 C programming allows you to use one loop inside another loop. The
following section shows a few examples to illustrate the Theory.
 The syntax for a nested for loop statement in C is as follows −
for ( init; condition; increment ) {
for ( init; condition; increment ) {
statement(s);
}
statement(s);
}
Flow Diagram for nested for loop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 21
 The syntax for a nested while loop statement in C programming language is
as follows −
while(condition) {
while(condition) {
statement(s);
}
statement(s);
}
Flow Diagram for nested while loop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 22
 The syntax for a nested do...while loop statement in C programming
language is as follows −
do {
statement(s);
do {
statement(s);
}while( condition );
}while( condition );
Flow Diagram for nested do-while loop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 23
 A final note on loop nesting is that you can put any type of loop inside any
other type of loop. For example, a 'for' loop can be inside a 'while' loop or
vice versa.
[Reference: http://www.tutorialspoint.com/cprogramming/c_nested_loops.htm ]
Flowchart for Problem Statement:
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 24
Algorithm:
1. Start
2. c=0, i=1
3. Read the number n
4. If i <= n
if (n % i == 0)
c++;
end if
i=i+1
go to step 4
else
go to step 5
5. if c = 2
printf("%d is a Prime number",n);
go to step 6
else
printf("%d is not a Prime number",n);
go to step 6
6. Stop
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 25
Program:
#include <stdio.h>
void main() {
int n, i, c = 0;
clrscr();
printf("Enter any number n:");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
if (n % i == 0)
{
c++;
}
}
if (c == 2) {
printf("%d is a Prime number",n);
}
else {
printf("%d is not a Prime number",n);
}
getch();
}
Input:
Enter any number n: 5
Output:
2 is prime
3 is prime
5 is prime
Practice Problem Statement:
1. Write a program to print all even numbers between 1 to 100.
2. Write a program to print all even numbers between 1 to 100 and sum of it.
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 26
3. Write a program to print all odd number between 1 to 100.
4. Write a program to print all odd numbers between 1 to 100 and sum of it.
5. Write a program to find the prime numbers from 2 to 100 and sum of it.
Conclusion:
Thus a C program using nested loop – to find the prime numbers of n-terms is
implemented.
Learning Outcomes:
At the end of this assignment, students are able to
- write the syntax of nested loop
- draw flowchart for loop statement solution
- write the algorithm for loop statement
- write the program using the nested loop

More Related Content

What's hot

Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C ProgrammingAnil Pokhrel
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1ReKruiTIn.com
 
Java loops for, while and do...while
Java loops   for, while and do...whileJava loops   for, while and do...while
Java loops for, while and do...whileJayfee Ramos
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaSanjeev Tripathi
 
While loop
While loopWhile loop
While loopFeras_83
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++imran khan
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In JavaManish Sahu
 
Control structures in java
Control structures in javaControl structures in java
Control structures in javaVINOTH R
 
Control Structure in C
Control Structure in CControl Structure in C
Control Structure in CNeel Shah
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Javaparag
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Digvijaysinh Gohil
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPTShubham Mondal
 

What's hot (20)

Java Basics
Java BasicsJava Basics
Java Basics
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
 
C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1C, C++ Interview Questions Part - 1
C, C++ Interview Questions Part - 1
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
Loops c++
Loops c++Loops c++
Loops c++
 
Java loops for, while and do...while
Java loops   for, while and do...whileJava loops   for, while and do...while
Java loops for, while and do...while
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Decision Making and Looping
Decision Making and LoopingDecision Making and Looping
Decision Making and Looping
 
While loop
While loopWhile loop
While loop
 
C language
C languageC language
C language
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
 
Control Structure in C
Control Structure in CControl Structure in C
Control Structure in C
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
 
Constructor and Destructor PPT
Constructor and Destructor PPTConstructor and Destructor PPT
Constructor and Destructor PPT
 

Viewers also liked

Handout #4 slo Procedural Cheat Sheet-May 2014-Final
Handout #4 slo Procedural Cheat Sheet-May 2014-FinalHandout #4 slo Procedural Cheat Sheet-May 2014-Final
Handout #4 slo Procedural Cheat Sheet-May 2014-FinalResearch in Action, Inc.
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismEduardo Bergavera
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)akmalfahmi
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)Jyoti Bhardwaj
 
Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments rajni kaushal
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Abdullah khawar
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and OutputEduardo Bergavera
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramDeepak Singh
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmigAppili Vamsi Krishna
 

Viewers also liked (20)

Handout #4 slo Procedural Cheat Sheet-May 2014-Final
Handout #4 slo Procedural Cheat Sheet-May 2014-FinalHandout #4 slo Procedural Cheat Sheet-May 2014-Final
Handout #4 slo Procedural Cheat Sheet-May 2014-Final
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
CP Handout#10
CP Handout#10CP Handout#10
CP Handout#10
 
CP Handout#9
CP Handout#9CP Handout#9
CP Handout#9
 
Chapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and PolymorphismChapter 13 - Inheritance and Polymorphism
Chapter 13 - Inheritance and Polymorphism
 
CP Handout#2
CP Handout#2CP Handout#2
CP Handout#2
 
8.3 program structure (1 hour)
8.3 program structure (1 hour)8.3 program structure (1 hour)
8.3 program structure (1 hour)
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
 
Java programming lab assignments
Java programming lab assignments Java programming lab assignments
Java programming lab assignments
 
Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]Pf cs102 programming-9 [pointers]
Pf cs102 programming-9 [pointers]
 
Apclass
ApclassApclass
Apclass
 
Apclass (2)
Apclass (2)Apclass (2)
Apclass (2)
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Chapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ ProgramChapter 2 - Structure of C++ Program
Chapter 2 - Structure of C++ Program
 
C++ lecture 04
C++ lecture 04C++ lecture 04
C++ lecture 04
 
Loop c++
Loop c++Loop c++
Loop c++
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
User defined functions in C programmig
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
 
Array in c++
Array in c++Array in c++
Array in c++
 
C++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLESC++ ARRAY WITH EXAMPLES
C++ ARRAY WITH EXAMPLES
 

Similar to C-Programming Loops

Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopPriyom Majumder
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing TutorialMahira Banu
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notesSrikanth
 
Programming basics
Programming basicsProgramming basics
Programming basics246paa
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxssuser10ed71
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of Ceducationfront
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Shipra Swati
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in Csana shaikh
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 

Similar to C-Programming Loops (20)

Looping statements
Looping statementsLooping statements
Looping statements
 
What is c
What is cWhat is c
What is c
 
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested LoopLoops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
Loops in C Programming | for Loop | do-while Loop | while Loop | Nested Loop
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
Datastructure notes
Datastructure notesDatastructure notes
Datastructure notes
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Chp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptxChp4_C++_Control Structures-Part2_Iteration.pptx
Chp4_C++_Control Structures-Part2_Iteration.pptx
 
Discussing Fundamentals of C
Discussing Fundamentals of CDiscussing Fundamentals of C
Discussing Fundamentals of C
 
Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8Fundamental of Information Technology - UNIT 8
Fundamental of Information Technology - UNIT 8
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
C operators
C operatorsC operators
C operators
 
C Basics
C BasicsC Basics
C Basics
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
CP Handout#8
CP Handout#8CP Handout#8
CP Handout#8
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 

More from trupti1976

MobileAppDev Handout#3
MobileAppDev Handout#3MobileAppDev Handout#3
MobileAppDev Handout#3trupti1976
 
MobileAppDev Handout#10
MobileAppDev Handout#10MobileAppDev Handout#10
MobileAppDev Handout#10trupti1976
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9trupti1976
 
MobileAppDev Handout#8
MobileAppDev Handout#8MobileAppDev Handout#8
MobileAppDev Handout#8trupti1976
 
MobileAppDev Handout#7
MobileAppDev Handout#7MobileAppDev Handout#7
MobileAppDev Handout#7trupti1976
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6trupti1976
 
MobileAppDev Handout#5
MobileAppDev Handout#5MobileAppDev Handout#5
MobileAppDev Handout#5trupti1976
 
MobileAppDev Handout#4
MobileAppDev Handout#4MobileAppDev Handout#4
MobileAppDev Handout#4trupti1976
 
MobileAppDev Handout#2
MobileAppDev Handout#2MobileAppDev Handout#2
MobileAppDev Handout#2trupti1976
 
MobileAppDev Handout#1
MobileAppDev Handout#1MobileAppDev Handout#1
MobileAppDev Handout#1trupti1976
 

More from trupti1976 (13)

MobileAppDev Handout#3
MobileAppDev Handout#3MobileAppDev Handout#3
MobileAppDev Handout#3
 
MobileAppDev Handout#10
MobileAppDev Handout#10MobileAppDev Handout#10
MobileAppDev Handout#10
 
MobileAppDev Handout#9
MobileAppDev Handout#9MobileAppDev Handout#9
MobileAppDev Handout#9
 
MobileAppDev Handout#8
MobileAppDev Handout#8MobileAppDev Handout#8
MobileAppDev Handout#8
 
MobileAppDev Handout#7
MobileAppDev Handout#7MobileAppDev Handout#7
MobileAppDev Handout#7
 
MobileAppDev Handout#6
MobileAppDev Handout#6MobileAppDev Handout#6
MobileAppDev Handout#6
 
MobileAppDev Handout#5
MobileAppDev Handout#5MobileAppDev Handout#5
MobileAppDev Handout#5
 
MobileAppDev Handout#4
MobileAppDev Handout#4MobileAppDev Handout#4
MobileAppDev Handout#4
 
MobileAppDev Handout#2
MobileAppDev Handout#2MobileAppDev Handout#2
MobileAppDev Handout#2
 
MobileAppDev Handout#1
MobileAppDev Handout#1MobileAppDev Handout#1
MobileAppDev Handout#1
 
CP Handout#6
CP Handout#6CP Handout#6
CP Handout#6
 
CP Handout#3
CP Handout#3CP Handout#3
CP Handout#3
 
CP Handout#1
CP Handout#1CP Handout#1
CP Handout#1
 

Recently uploaded

HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)dollysharma2066
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 

Recently uploaded (20)

HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
Call Us ≽ 8377877756 ≼ Call Girls In Shastri Nagar (Delhi)
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 

C-Programming Loops

  • 1. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 1 Handout#5 Assignment/Program Statement: Write C programs using loop statements such as for, while, do-while and nested loop. Learning Objectives: Students will be able to - explain decision control statements in C - draw the flowchart for loop statement solution - write the algorithm loop statements - write C code using ‘for’ statement in C - write C code using ‘while’ statement in C - write C code using ‘do-while’ ladder in C - write C code using nested loop in C Theory: Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times. C programming language provides the following types of loops to handle looping requirements. - For loop - While loop - Do-while loop - Nested loop 5-a. Write a C program using ‘for’ loop - to display character from A to Z and their ASCII values 5-b. Write a C program using ‘while’ loop - to print factorial of given number 5-c. Write a C program using ‘do-while’ loop - to find the Fibonacci Series 5-d. Write a C program using nested loop - find the prime numbers from 2 to 100
  • 2. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 2 Assignment#5a Assignment/Program Statement: Write a C program using ‘for’ loop - to display character from A to Z and their ASCII values Learning Objectives: Students will be able to - write the syntax of ‘for’ loop - draw flowchart for loop statement solution - write the algorithm for loop statement - write the program using the ‘for’ loop Theory:  A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.  The syntax of a ‘for’ loop in C programming language is – for (init; condition; increment) { Statement(s); } Here is the flow of control in a 'for' loop − o The init step is executed first, and only once. This step declares and initializes any loop control variables. o Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and the flow of control jumps to the next statement just after the 'for' loop. o After the body of the 'for' loop executes, the flow of control jumps back up to the increment statement. This statement updates any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
  • 3. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 3 o The condition is now evaluated again. If it is true, the loop executes and the process repeats itself. After the condition becomes false, the 'for' loop terminates. Example: C code to print first 10 numbers for( a = 1; a <= 10; a = a + 1 ) { printf("value of a: %dn", a); } [Reference: http://www.tutorialspoint.com/cprogramming/c_for_loop.htm ] Flow Diagramfor‘for’loop
  • 4. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 4 Various examples of infinite ‘for’ loop Loop structure Description for ( i=1 ; i<=10 ; ) printf("Hello, World!n"); Here the updating part of the counter variable i is missing. Hence the value of i will remain 1 forever and the loop runs infinite times printing Hello, World! for ( i=1 ; ; i++ ) printf("Hello, World!n"); Here the condition part of the loop is missing. Due to which the loop is forced to run repeatedly infinite times printing Hello, World! As there isn’t any condition to terminate. for ( i=1 ; ; ) printf("Hello, World!n"); Here the condition as well as updating part is missing from the loop. Hence it will also iterate infinite times printing Hello, World! for ( ; ; ) printf("Hello, World!n"); Here all the three parts initialization, condition as well as updation part is missing from the loop. Hence it will also run infinite times printing Hello, World! ASCII Value: ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as 'a' or '@' or an action of some sort. Below is the ASCII character table for alphabet. Symbol Decimal Binary A 65 01000001 B 66 01000010 C 67 01000011 D 68 01000100 Symbol Decimal Binary a 97 01100001 b 98 01100010 c 99 01100011 d 100 01100100
  • 5. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 5 E 69 01000101 F 70 01000110 G 71 01000111 H 72 01001000 I 73 01001001 J 74 01001010 K 75 01001011 L 76 01001100 M 77 01001101 N 78 01001110 O 79 01001111 P 80 01010000 Q 81 01010001 R 82 01010010 S 83 01010011 T 84 01010100 U 85 01010101 V 86 01010110 W 87 01010111 X 88 01011000 Y 89 01011001 Z 90 01011010 e 101 01100101 f 102 01100110 g 103 01100111 h 104 01101000 i 105 01101001 j 106 01101010 k 107 01101011 l 108 01101100 m 109 01101101 n 110 01101110 o 111 01101111 p 112 01110000 q 113 01110001 r 114 01110010 s 115 01110011 t 116 01110100 u 117 01110101 v 118 01110110 w 119 01110111 x 120 01111000 y 121 01111001 z 122 01111010
  • 6. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 6 Flowchart for Problem Statement: Algorithm: 1. Start 2. i=0 3. If i<26, print the alphabet and ASCII value i=i+1 go to step 3 else go to step 4 4. Stop
  • 7. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 7 Program: #include <stdio.h> #include <conio.h> int main() { int i; for(i = 0; i < 26; i++) { printf("%c = %d | %c = %d n",'A'+i,'A'+i,'a'+i,'a'+i); } getch(); return 0; } Input: No Input Output: A = 65 | a = 97 B = 66 | b = 98 C = 67 | c = 99 D = 68 | d = 100 E = 69 | e = 101 F = 70 | f = 102 G = 71 | g = 103 H = 72 | h = 104 I = 73 | i = 105 J = 74 | j = 106 K = 75 | k = 107 L = 76 | l = 108 M = 77 | m = 109 N = 78 | n = 110 O = 79 | o = 111 P = 80 | p = 112 Q = 81 | q = 113
  • 8. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 8 R = 82 | r = 114 S = 83 | s = 115 T = 84 | t = 116 U = 85 | u = 117 V = 86 | v = 118 W = 87 | w = 119 X = 88 | x = 120 Y = 89 | y = 121 Z = 90 | z = 122 Practice Problem Statements: 1. Write a C program to calculate the sum of first n natural numbers 2. Write a C program to find factorial of a number 3. Write a C program to print natural numbers 4. Write a C program to print natural numbers in reverse 5. Write a C program to print alphabets Conclusion: Thus a C program using ‘for’ loop - to display character from A to Z and their ASCII values is implemented. Learning Outcome: At end of this assignment, students are able to - write the syntax of ‘for’ loop - draw flowchart for loop statement solution - write the algorithm for loop statement - write the program using the ‘for’ loop
  • 9. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 9 Assignment#5b Assignment/Program Statement: Write a C program using ‘while’ loop - to print factorial of given number Learning Objectives: Students will be able to - write the syntax of ‘while’ loop - draw flowchart for loop statement solution - write the algorithm for loop statement - write the program using the ‘while’ loop Theory:  A while loop in C programming repeatedly executes a target statement as long as a given condition is true.  The syntax of a while loop in C programming language is − while(condition) { statement(s); }  Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true.  When the condition becomes false, the program control passes to the line immediately following the loop. [Reference: http://www.tutorialspoint.com/cprogramming/c_while_loop.htm ] Example: C code to print first 10 numbers a=1; while ( a <= 20 ) { printf("value of a: %dn", a); a++;
  • 10. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 10 } Flow Diagram for ‘while’ loop
  • 11. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 11 Flowchart for Problem Statement: Algorithm: 1. Start 2. Enter the number whose factorial need to be found 3. f=1 4. i=1 5. If i<=num f=f*I i=i+1 go to step 5 else go to step 6
  • 12. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 12 6. Display the factorial of the given number 7. Stop Program: #include<stdio.h> int main() { int num,f,i; printf("Enter a number: "); scanf("%d",&num); f=1; i=1; while(i<=num) { f = f * i; i++; } printf("Factorial of %d is: %d",num,f); return 0; } Input: Enter a number:4 Output: Factorial of 4 is: 24 Practice Problem Statement: 1. Write a program to print sum of all even numbers between 1 to n. 2. Write a program to print sum of all odd numbers between 1 to n. 3. Write a program to print table of any number.
  • 13. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 13 4. Write a program to enter any number and calculate sum of all natural numbers between 1 to n. 5. Write a program to enter any number and calculate sum of its digits. Conclusion: Thus a C program using ‘while’ loop - to print factorial of given number is implemented. Learning Outcomes: At end of this assignment, students are able to - write the syntax of ‘while’ loop - draw flowchart for loop statement solution - write the algorithm for loop statement - write the program using the ‘while’ loop At the end of this assignment, students are able to - write the syntax of ‘while’ loop - draw flowchart for loop statement solution - write the algorithm for loop statement - write the program using the ‘while’ loop
  • 14. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 14 Assignment#5c Assignment/Program Statement: Write a C program using ‘do-while’ loop - to find the Fibonacci Series Learning Objectives: Students will be able to - write the syntax of ‘do-while’ loop - draw flowchart for loop statement solution - write the algorithm for loop statement - write the program using the ‘do-while’ loop Theory:  Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop in C programming checks its condition at the bottom of the loop.  A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time.  The syntax of a do...while loop in C programming language is – do { statement(s); } while( condition );  The conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested.  If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again. This process repeats until the given condition becomes false. Example: C code to print first 10 numbers a=1; do { printf("value of a: %dn", a);
  • 15. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 15 a++; } while ( a <=10 ) [Reference: http://www.tutorialspoint.com/cprogramming/c_do_while_loop.htm ] Flow Diagram for ‘do-while’ loop
  • 16. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 16 Flowchart for Problem Statement:
  • 17. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 17 Algorithm: 1. Start 2. First=0, second=1 3. Enter the number of terms in Fibonacci series 4. Display first and second term of Fibonacci series 5. If i<=n c=first second Display c first = second second = c i=i+1 go to step 5 else go to step 6 6. Stop
  • 18. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 18 Program: #include<stdio.h> void main() { int first, second, c, n, i; clrscr(); first=0; second=1; i=3; printf("Enter the number of terms: "); scanf("%d",&n); printf("%dn%d",first,second); do { c=first+second; printf("n%d",c); first = second; second = c; i=i+1; }while (i<=n); getch(); } Input: Enter number of terms: 4 Output: 0 1 2 3 Practice Problem Statement: 1. Write a C program to find power of any number using for loop. 2. Write a C program to enter any number and print all factors of the number. 3. Write a program to display character from A to Z and their ASCII values 4. Write a C program to enter any number and calculate product of its digits.
  • 19. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 19 5. Write a C program to enter any number and print its reverse. Conclusion: Thus a C program using ‘do-while’ loop - to find the Fibonacci Series is implemented. Learning Objectives: At the end of this assignment, students are able to - write the syntax of ‘do-while’ loop - draw flowchart for loop statement solution - write the algorithm for loop statement - write the program using the ‘do-while’ loop
  • 20. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 20 Assignment#5d Assignment/Program Statement: Write a C program using nested loop – to find the prime numbers of n-terms Learning Objectives: Students will be able to - write the syntax of nested loop - draw flowchart for loop statement solution - write the algorithm for loop statement - write the program using the nested loop Theory:  C programming allows you to use one loop inside another loop. The following section shows a few examples to illustrate the Theory.  The syntax for a nested for loop statement in C is as follows − for ( init; condition; increment ) { for ( init; condition; increment ) { statement(s); } statement(s); } Flow Diagram for nested for loop
  • 21. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 21  The syntax for a nested while loop statement in C programming language is as follows − while(condition) { while(condition) { statement(s); } statement(s); } Flow Diagram for nested while loop
  • 22. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 22  The syntax for a nested do...while loop statement in C programming language is as follows − do { statement(s); do { statement(s); }while( condition ); }while( condition ); Flow Diagram for nested do-while loop
  • 23. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 23  A final note on loop nesting is that you can put any type of loop inside any other type of loop. For example, a 'for' loop can be inside a 'while' loop or vice versa. [Reference: http://www.tutorialspoint.com/cprogramming/c_nested_loops.htm ] Flowchart for Problem Statement:
  • 24. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 24 Algorithm: 1. Start 2. c=0, i=1 3. Read the number n 4. If i <= n if (n % i == 0) c++; end if i=i+1 go to step 4 else go to step 5 5. if c = 2 printf("%d is a Prime number",n); go to step 6 else printf("%d is not a Prime number",n); go to step 6 6. Stop
  • 25. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 25 Program: #include <stdio.h> void main() { int n, i, c = 0; clrscr(); printf("Enter any number n:"); scanf("%d", &n); for (i = 1; i <= n; i++) { if (n % i == 0) { c++; } } if (c == 2) { printf("%d is a Prime number",n); } else { printf("%d is not a Prime number",n); } getch(); } Input: Enter any number n: 5 Output: 2 is prime 3 is prime 5 is prime Practice Problem Statement: 1. Write a program to print all even numbers between 1 to 100. 2. Write a program to print all even numbers between 1 to 100 and sum of it.
  • 26. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 26 3. Write a program to print all odd number between 1 to 100. 4. Write a program to print all odd numbers between 1 to 100 and sum of it. 5. Write a program to find the prime numbers from 2 to 100 and sum of it. Conclusion: Thus a C program using nested loop – to find the prime numbers of n-terms is implemented. Learning Outcomes: At the end of this assignment, students are able to - write the syntax of nested loop - draw flowchart for loop statement solution - write the algorithm for loop statement - write the program using the nested loop