0% found this document useful (0 votes)
5 views6 pages

Lab 4

Uploaded by

mahmudereza43024
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)
5 views6 pages

Lab 4

Uploaded by

mahmudereza43024
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/ 6

“Heaven’s Light is Our Guide”

Department of Computer Science & Engineering

RAJSHAHI UNIVERSITY OF ENGINEERING & TECHNOLOGY

Lab Manual

Programming in C

Lab 4

Control structures- for and while loop

Index

Lab Objectives

Background

Some Examples

Exercises
Lab Objectives:
 Explain the basic of for loops and while loops,

 To apply the syntax of loop structure,

 Design program using loop structure,

 Solve problems using reparative loop structure.

Background:
The loop can be used to do repetitive calculations. For example, in order to compute the sum
of 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 to find the summation 1+2+3+4+…….+100
Program code:

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

2. Write a C program to add n numbers.


Program code:
#include <stdio.h>
int main() {
int n, sum = 0, c, value;
printf("Enter the number of integers you want to add\n");
scanf("%d", &n);
printf("Enter %d integers\n",n);
for (c = 1; c <= n; c++)
{
scanf("%d",&value);
sum = sum + value; }
printf("Sum of entered integers = %d\n",sum);
return 0;
}

3. Write a C program to add 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)\n");
scanf(" %c",&ch);
if ( ch == 'y' || ch == 'Y' ) continue;
else
break;
}
return 0;
}

4. Write a program that will add the digits of a given number.


Program code:

#include <stdio.h>
int main() {
int n, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0) {
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
return 0;
}

5. Write a program to find GCD and LCM.


Program code:

#include <stdio.h>
int main() {
int a, b, x, y, t, gcd, lcm;
printf("Enter two integers\n");
scanf("%d%d", &x, &y);
a = x; b = y;
while (b != 0) { t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x*y)/gcd;
printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
printf("Least common multiple of %d and %d = %d\n", x, y, lcm);
return 0;
}
6. Write a program to find factorial of a given number.
Program code:

#include<stdio.h>
int main()
{
int i,n,f;
printf("Enter the number to find the factorial\n");
scanf("%d",&n);
f=1;
for(i=1;i<=n;i++)
f=f*i;
printf("The factorial of a number %d without using recursion is %d",n,f);

Exercise:
1. Write a program that will print the prime numbers in a given range.
Sample input: Given range- 1 to 20
Sample output: 2 3 5 7 11 13 17 19

2. Write a program that will check whether a given number is palindrome or not.
Palindrome Numbers
Palindrome number in c: A palindrome number is a number such that if we reverse it, it will
not change. For example some palindrome numbers examples are 121, 212, 12321, 454. To
check whether a number is palindrome or not first we reverse it and then compare the number
obtained with the original, if both are same then number is palindrome otherwise not.
Palindrome number algorithm

3. Write a C program to print patterns of numbers or stars.


The C programs that print various different patterns of numbers and stars involve usage of
nested loops and space. A pattern of numbers, star or characters is a way of arranging these in
some logical manner or they may form a sequence. Some of these patterns are triangles,
which have special importance in mathematics. Some patterns are symmetrical while other is
not. Below are some given patterns among many different patterns.
1.
*
* *
* * *
* * * *

2.
*
* *
* * *
* * * *

3.
*
* *
* * *
* * * *

4.
*
* *
* * *
* *
*

4. C program to generate and print Armstrong numbers.

Armstrong number in c: A number is Armstrong if the sum of cubes of individual digits of


a number is equal to the number itself. For example 371 is an Armstrong number as 33 + 73 +
13 = 371. Some other Armstrong numbers are 0, 1, 153, 370, and 407.
Write a c program to check whether a given number is Armstrong or not.
5. Fibonacci series in c

Fibonacci series in c programming: Numbers of Fibonacci sequence are known as


Fibonacci numbers. First few numbers of series are 0, 1, 1, 2, 3, 5, 8 etc, Except first two
terms in sequence every other term is the sum of two previous terms, For example 8 = 3 + 5
(addition of 3, 5). This sequence has many applications in mathematics and Computer
Science.
Write a program that will print the first n number sequence of Fibonacci series.

You might also like