Академический Документы
Профессиональный Документы
Культура Документы
CF-Zero Semester
Practice the following concepts in this lab
1. While Loop
2. For Loop
3. Do while loop
WHILE LOOP
A while loop statement repeatedly executes a target statement as long as a given condition is true.
Syntax
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 non-zero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following the loop.
Example-1
#include <stdio.h>
2
3
4
5
6
7
8
9
printf(%d \n,num);
num++;
}
getch();
}
TASK-1
Try to use the above program and print numbers between two given numbers A and B, the value of A and B will be
acquired from the user. To make the program easier it can be supposed that A < B
TASK-2
Modify the program developed in task-1 and print the number between two given numbers A and B such that A
can be greater or smaller than B. You might require an if statement of some similar statement to get the desired
results. Try not to use two different loops.
TASK-3
Try to print even numbers between two given numbers A and B such that A can be greater or smaller than B
TASK-4
Write a program to print the following series upto N term , the value of N should be acquired from the user
1
15
31
63 . . .
FOR LOOP
A for loop is like while loop but it provides more control over number of times the loop should execute.
Syntax
Example -2
#include <stdio.h>
#include <conio.h>
void main(){
intnum;
printf(All numbers between 1 and 10 are \n);
for(num=2;num<10;num++) {
printf(%d \n,num);
2
3
4
5
6
7
8
9
}
getch();
}
Example-3
Printing even and odd series together, by using a for loop
#include <stdio.h>
#include <conio.h>
int main(){
printf(Odd
Even \n);
for(int a=1,b=2;a<=10;a+=2,b+=2)
printf(%d \t %d \n,a,b);
getch();
return 0;
}
Even
3
5
7
9
4
6
8
10
Example-4
A program that calculates the factorial of a given number N
#include <stdio.h>
#include <conio.h>
int main(){
int n, num=1,factorial=1;
printf(Enter value of N : )
scanf(%d,&n);
for(num=1;num<=n;num++){
factorial=factorial*num;
}
printf(The factorial of %d = %d ,n,factorial);
getch();
return 0;
}
Task-05
Try to write a program that gets a number from the user and prints whether the number is even or odd, the
program should do so 5 times, i.e. the program should get 5 numbers one at a time and print their even or odd
status. The expected output of the program should be as under
Expected Output
Enter a number : 5
Status : Odd
Enter a number :10
Status :Even
Enter a number :9
Status : Odd
Enter a number :15
Status : Odd
Enter a number :2
Status : Even
Task-06
If you are able to complete the above program , try to modify your program and count the number of even
numbers and odd numbers entered by the user. The expected output of the program should be as under.
Expected Output
Enter a number : 5
Status : Odd
Enter a number :10
Status :Even
Enter a number :9
Status : Odd
Enter a number :15
Status : Odd
Enter a number :2
Status : Even
Even Numbers : 2
Odd Numbers : 3
Example-5
This program gets a number from the user if the number provided is even the program will
terminate otherwise the program will get another number. The while loop here will terminate
only when an even number is provided
#include <stdio.h>
#include <conio.h>
int main()
{
int a=1;
while(a%2!=0){
printf(\n Enter Value of a = );
scanf(%d,&a);
}
printf(\nEnd of While loop);
getch();
return 0;
}
DO WHILE LOOP
A do-while loop is like a while loop expect
Body of the loop comes first so body of this loop executes at least once
do{
statement(s);
} while(condition);
B is positive divisor of a number A if and only if B divides A completely, in other words A / B has zero
remainder. In C we can check whether a number B is positive divisor of another number A by using
simple % operator. i.e.
1. A mod(2)=0 means A is an even number,
2. A mod(B)=0 means B divides A and is a positive divisor of A
3. In C A%B= A mod (B)
Example
17 % 5 = 2
65 %13 = 0
Task-08
By using % operator and for loop perform the following activities
a) Write a program that gets a number N from user and prints all positive divisors of N
b) Write a program that gets a number P from user, the program will check whether the number
is prime or not
Task-09
Re-write the number reverse program such that the program should allow user to enter a number
consisting of 3,4 or 5 digits. The programs should print the number in reverse. You can use any of
the loop.
Comparison of Loops
Write a program that allow user to enter two numbers A and B , the program should print all the
even numbers between these two numbers, to make the program simple it is supposed that A<B
Comparison Table (scale 1 to 10 , 1 very easy, 10 extremely difficult)
Sr.No.
1
2
3
4
5
6
7
8
Element
for loop
Number of comparison
Number of variables
Number of statement
Difficulty in developing program, (scale 1-10)
Clarity of program (scale 1 to 10)
Number of lines of program
Number of times body of loop executed
Loop control (scale 1 to 10)
While loop
do while loop