Looping
Looping
Looping
// Program to calculate the sum of first n natural numbers using for loop
// Positive integers 1,2,3...n are known as natural numbers
#include <stdio.h>
int main()
{
int num, count, sum = 0;
return 0;
}
while loop:
while loop allows a part of the code to be executed multiple times depending
upon a given Boolean condition. It can be viewed as a repeating if statement. The
while loop is mostly used in the case where the number of iterations is not
known in advance. It is also called a pre-tested loop.
Syntax :
initialisation;
while(testExpression)
{
// statements inside the body of the loop
Increment/decrement;
}
Flow Chart:
//program to find the sum of digits of a given number using while loop
#include <stdio.h>
int main ()
{
int n, t, sum = 0, r;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
r= t % 10;
sum = sum + r;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);
return 0;
}
Output:
Enter an integer 12345
Sum of digits of 12345 = 15
Exit Controlled loop
do while loop:
Unlike for and while loops, which test the loop condition at the top of the loop, the
do...while loop in C programming language checks its condition at the bottom of the
loop. A do...while loop is similar to a while loop, except that a do...while loop is
guaranteed to execute at least one time.
Syntax :
do
{
Statement1;
Statement 2;
} while ( condition );
Flow Chart:
In this loop the test condition or In this loop the test condition or
criteria is checked and evaluated first criteria is checked after the loop is
before loop starts executing. executed.
while loop is entry-controlled loop. do-while loop is exit- controlled loop.
Loop conditions are not terminated Loop conditions are terminated with
with semicolon. semicolon.
Body of loop never executed if the Body of loop is executed for at least
condition is false. It means body of one time even after the condition is
the loop executed zero time, if the false.
condition is false.
If there is single statement in body of Brackets are always required.
loop, brackets are not required.
Variable in condition is initialized variable may be initialized before or
before the execution of loop. within the loop.
Syntax: Syntax:
while (condition) do
{ {
Block of statements; Block of statements;
} } while(condition);
//Write a program in C to read 10 numbers from the keyboard and find their
sum and average.
#include <stdio.h>
int main() {
int i, n, sum = 0;
float avg;
printf("Input the 10 numbers : \n");
for (i = 1; i <= 10; i++)
{
printf("Number-%d :", i);
scanf("%d", &n);
sum += n;
}
avg = sum / 10.0;
printf("The sum of 10 no is : %d\nThe Average is : %f\n", sum, avg);
return 0;
}
Output:
Input the 10 numbers :
Number-1 :10
Number-2 :10
Number-3 :10
Number-4 :10
Number-5 :10
Number-6 :20
Number-7 :20
Number-8 :20
Number-9 :20
Number-10 :10
The sum of 10 no is : 140
The Average is : 14.000000
10 X 1 = 10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100
//Write a C program to display the n terms of odd natural numbers and their
sum.
#include <stdio.h>
int main() {
int i, n, sum = 0,odd;
printf("Input number of terms : ");
scanf("%d", &n);
printf("\nThe odd numbers are :");
for (i = 1; i <= n; i++)
{
odd= 2*i-1;
printf("%d ",odd);
sum += odd;
}
printf("\nThe Sum of odd Natural Number upto %d terms : %d \n", n, sum);
return 0;
}
Output:
Input number of terms : 5
The odd numbers are :1 3 5 7 9
The Sum of odd Natural Number upto 5 terms : 25
//Write a C program to display the n terms of even natural numbers and their
sum.
#include <stdio.h>
int main() {
int i, n, sum = 0,even;
printf("Input number of terms : ");
scanf("%d", &n);
printf("\nThe even numbers are :");
for (i = 1; i <= n; i++)
{
even= 2*i;
printf("%d ",even);
sum += even;
}
printf("\nThe Sum of even Natural Number upto %d terms : %d \n", n, sum);
return 0;
}
Output:
Input number of terms : 5
The even numbers are :2 4 6 8 10
The Sum of even Natural Number upto 5 terms : 30
//Write a C program that displays the n terms of square natural numbers and
their sum.
#include <stdio.h>
int main() {
int i, n, sum = 0, sq;
printf("Input number of terms : ");
scanf("%d", &n);
printf("\nThe square numbers are :");
for (i = 1; i <= n; i++)
{
sq= i*i;
printf("%d ",sq);
sum += sq;
}
printf("\nThe Sum of square Natural Number upto %d terms : %d \n", n, sum);
return 0;
}
Output:
Input number of terms : 5
The square numbers are :1 4 9 16 25
The Sum of square Natural Number upto 5 terms : 55
//Write a program in C to find the sum of the series 1 +11 + 111 + 1111 + .. n
terms.
#include <stdio.h>
int main() {
int n, i;
long sum = 0;
long int t = 1;
printf("Input the number of terms : ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
sum = sum + t;
t = (t * 10) + 1;
}
printf("\nThe Sum is : %ld\n", sum);
return 0;
}
Output:
Input the number of terms : 5
The Sum is : 12345
OR
// Write a program to print check a number is prime number or not
#include <stdio.h>
int main()
{
int n, i, count= 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// Write a C program to add first seven terms of the following series using for
loop.
1/1! +2/2! +3/3! +--------
#include<stdio.h>
int main()
{
int i;
float fact=1.0, res, n_res=0;
for (i=1;i<8;i++)
{
//find factorial for 1 to 7
fact = fact * i;
//find the i/factof(i)
res = i/fact;
//add all the results
n_res = n_res + res;
}
printf("%f", n_res);
}
Output:
2.718056
int main(){
int i, f = 1, num;
printf("Input the number : ");
scanf("%d", &num);
return 0;
}
Output:
Input the number: 12
number is not perfect.
Or
Input the number: 6
number is perfect.
2018-19(KCS-201)
1. Write a program in C to print following pattern with appropriate comments:
10
98
765
4321 10
2. What is the use of break statement in loops? Write a program in C using
while loop to elaborate the use of break statement. 10
3. Differentiate while and do while loop. 2
2019-20(KCS-101)
1. Take the three digit number from the user then write a program to check
entered number is palindrome or not. 10
2. Write a program in C to generate the Fibonacci series up to the last Fibonacci
number less than 100. Also finds the sum of all Fibonacci numbers and total
count of all Fibonacci numbers. 10
3. Write a program in C to print the following pattern: 10
234567
34567
4567
567
67
7
4. Differentiate while and do while loop. 2
2020-21(KCS-101T)
1. Write a program to find the entered number is palindrome or not. 10
2. Write a program in C to print the following pattern: 10
****
***
**
*
3. Differentiate while and do while loop. 2
2021-22(KCS-101T)
1. Differentiate between type conversion and type casting. Write a program to
input a floating-point number and find leftmost digit of integral part of a
number. 10
2. Show the usage of break statement. 2
2021-22(KCS-201T)
1. Write a program to print the pattern 10
1
12
123
1234
123
12
1
2. Write a Program for pattern 10
*****
****
***
**
*
**
***
****
*****
2022-23(BCS-101)
1. Write a program to print the pattern 7
*
***
*****
*******
*********
2. Find the output of the following code: 2
#include<stdio.h>
main()
{
int i=1;
for(;;)
{ printf("%d",i);
if(i=5)
break;
}
}
2022-23(BCS-201)
1 Write a program in C to reverse a given number N having any number of
digits. 7
2 Differentiate between while and do-while loop. Write a program in C to print
the following pattern: 7
12345
1234
123
12
1
2023-24(BCS-101)
1 Write a program to print the pattern 7
*
**
***
****
***
**
*
2 Write a program to check whether the entered number is prime or not. 7
3 Find the output of the code 2
#include<stdio.h>
main()
{
int a,b;
for(a=6,b=4;a<=24;a=a+6)
{
if(a%b==0)
break;
}
printf("%d",a);
}