0% found this document useful (0 votes)
29 views5 pages

Practical 4 C

The document discusses different types of loops used in programming including while, do-while, and for loops. It provides syntax examples and sample code for each loop type to print numbers, calculate sums, and check conditions. A variety of programming exercises using loops are also listed.

Uploaded by

hasini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

Practical 4 C

The document discusses different types of loops used in programming including while, do-while, and for loops. It provides syntax examples and sample code for each loop type to print numbers, calculate sums, and check conditions. A variety of programming exercises using loops are also listed.

Uploaded by

hasini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Worksheet 04 - Loops

Loops are used when the same procedure/ sequence is repeated:

There are 3 kinds of loops:


1) While loop
2) Do while loop
3) For loop

1. While loop:

Syntax:

while(condition)
{
// Code block: this executes until condition is true.
//Increment or decrement
}

 Ex:
a) A program to print odd numbers from 1 to n:

#include<stdio.h>
int main()
{
int i=1,no;

printf("Enter a Number:");
scanf("%d",&no);

printf("Odd numbers from 1 to %d are \n",no);

while(i<=no)
{
if(i%2!=0)
{
printf(" %d \t ",i);
}
i++;
}
return 0;
}
b) Write a program to print all the positive numbers below 10.
c) By using while loop write a program to compute the sum of 1+2+3+
…..........................+n, for an input integer n:
d) The factorial of an integer n is the product of consecutive integer from 1 to n.
n!= n*(n-1)*…*1. Write a c program that compute and print a table of
factorial of any given number n.

2. Do while loop:

Syntax:
do
{
// Code block: executes until condition is true.
//Increment/decrement
} while (condition)

 Ex:
a) A program to print even numbers from 1 to n:

#include<stdio.h>
int main()
{
int i=1,no;

printf("Enter a Number:");
scanf("%d",&no);
printf("even numbers from 1 to %d are \n",no);

do
{
if(i%2==0)
{
printf(" %d \t ",i);
} i+
+;

} while(i<=no);
return 0;
}
b) Write a program to print the square of all the numbers up to input number by
the user, using a do-while loop.

 What is the difference between While & Do while loop???

int i=1;
while(i<0)
{
printf(“%d”,i);
i++;
}
int i=1;
do
{
printf(“%d”,i);
} while(i<0);
3. For loop:

Syntax:
for(initialize variable; condition ; increment/decrement)
{
// Code block: executes until condition is true.
}

Ex:
a) A program to print numbers from 1 to n
#include<stdio.h>

int main()
{
int i,no;

printf("Enter a Number:");
scanf("%d",&no);
printf("Numbers from 1 to %d are",no);

for(i=1;i<=no;i++)
{
printf("\t %d",i);
}
return 0;
}

b) A program to print the followings:


This is X=10
This is X=9
This is X=8
This is X=7
This is X=6
This is X=5
This is X=4
This is X=3
This is X=2
This is X=1
This is X=0
c) Obtain three test scores from a student. Calculate their average test score
and output this value. If their average score is 75% or more output a
message indicating that they may proceed to the next class.

d) A program that will obtain 10 exam scores from the user and determine
whether the score is passing (a score of 60 or above) or failing. Your
program should count the number of passing and failing scores. Output
the average of the scores, the number of failing scores, and the number of
passing scores. List the necessary variables for this program.
e) Program for printing even numbers between 9 and 100
f) A program to print all numbers between two given numbers that are
divisible by another given number.
g) A program to print the SUM of numbers between two given numbers.
(Test your algorithm with LOW = 3 and HIGH = 9.)
h) Program to print the square of all numbers from a given number to
another given number. (Test your algorithm with LOW = 1 and HIGH =
10).
i) Program for computing factorial N (N!)
j) Program to find the largest of three numbers x, y and z.
k) Program to find the sum of first 100 natural numbers.
l) Program to calculate the average from 25 exam scores.
m) Program for printing odd numbers less than a given number. It should
also calculate their sum and count.
n) Obtain three test scores from a student. Calculate their average test score
and output this value. If their average score is 75% or more output a
message indicating that they may proceed to the next class

You might also like