0% found this document useful (0 votes)
5 views

Lab Manual # 3. Control Statements

This lab manual focuses on control statements in C programming, specifically the while loop, for loop, and do-while loop. It includes objectives, background information, example programs, and exercises for students to practice their skills. The manual provides code snippets for various tasks such as summation, factorial calculation, and generating patterns.

Uploaded by

ohijannatul572
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab Manual # 3. Control Statements

This lab manual focuses on control statements in C programming, specifically the while loop, for loop, and do-while loop. It includes objectives, background information, example programs, and exercises for students to practice their skills. The manual provides code snippets for various tasks such as summation, factorial calculation, and generating patterns.

Uploaded by

ohijannatul572
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

“Heaven’s Light is Our Guide”

Department of Computer Science & Engineering

RAJSHAHI UNIVERSITY OF ENGINEERING & TECHNOLOGY

Programming in C

Lab Manual

Lab 3
Control Statements - while loop, for loop and do-while loop

INDEX

Lab Objectives

Background

Some Examples

Exercises
Lab Objectives:
• Explain the basic of for loop and while loop,
• To apply the syntax of loop structure,
• Design program using loop structure.

Background:
The loop can be used to do repetitive calculations. For example, in order to compute the sum of
1 to 100 (i.e., 1 + 2 + 3 + ... + 100), we can do easily using the following code segment:

int sum = 0;
for (int j = 1; j <= 100; j++)
sum += j;

There are two control structures used often: the ‘for’ loop and the ‘while’ loop (and ‘do while’
as a different way of while). The syntaxes of these loop structures are as follows:

for( initialize loop variables ; loop terminator ; loop variable update )


{
statements in the block or one statement
}

while(condition){
statements in the block or one statement
}

do{
statements in the block or one statement
}while(condition);

Some Examples:

1. Write a program that adds two numbers repeatedly.


Program code:
#include<stdio.h>
int main()
{
int a, b, c; char ch;
while(1) {
printf("Enter values of a and b\n");
scanf("%d%d",&a,&b);
c = a + b;
printf("a + b = %d\n", c);
printf("Do you wish to add more numbers(y/n): ");
scanf(" %c",&ch);
if ( ch == 'y' || ch == 'Y' ) continue;
else
break;
}
return 0;
}

2. Write a program to find the summation 1+2+3+4+……. +100.


Program code:
#include<stdio.h>
int main()
{
int sum = 0, number;
for(number= 1; number <= 100 ; number++)
sum += number;
printf("1+2+3+......+100= %d",sum);
return 0;
}

3. Write a program to add n numbers.


Program code:
#include <stdio.h>
int main() {
int noOfIntegers, sum, count, value;
printf("Enter the number of integers you want to add: ");
scanf("%d", &noOfIntegers);
printf("Enter %d integers: \n",noOfIntegers);

sum = 0;
for(count = 1; count <= noOfIntegers; count++){
scanf("%d",&value);
sum = sum + value;
}
printf("Sum of the entered integers = %d\n",sum);
return 0; }
4. Write a program that accepts the number of rows from the user and generates the
following output:
Sample Input: Sample Input:
Enter the number of rows: 5 Enter the number of rows: 3
Sample Output: Sample Output:
* *
* * * *
* * * * * *
* * * *
* * * * *
Program code:
#include<stdio.h>
int main(){
int n, line, star;
printf("\n Enter the number of rows: ");
scanf("%d",&n);
for(line=1; line<=n; line++){
for(star=1; star<= line; star++){
printf(" * ");
}
printf("\n");
}
return 0;
}

Exercise:
1. Write a program to find the factorial of a given number.
[CLUE: 5! = 1*2*3*4*5; 8! = 1*2*3*4*5*6*7*8]
2. Write a program to find the minimum of N numbers.
3. Write a program that will take a lower limit (x1) and an upper limit (x2) of a range
from the user and find the summation of all the integer numbers which are
between x1 & x2 and divisible by 3.
4. Write three different programs that will accept the number of rows from the user
and generate the following outputs:
(a) Sample Input: (b) Sample Input: (c) Sample Input:
Enter the number of rows: 5 Enter the number of rows: 5 Enter the number of rows: 5
Sample Output: Sample Output: Sample Output:
* * * * * 1 12345
* * * * 2 2 1234
* * * 3 3 3 123
* * 4 4 4 4 12
* 5 5 5 5 5 1

You might also like