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

APL Experiment No2

This document describes 5 programs that use different loop control statements like for, while, and do-while loops. Program 1 prints numbers from 1 to 10 using a for loop. Program 2 calculates the factorial of a number entered by the user using a for loop. Program 3 calculates the sum of the first n natural numbers entered by the user using a for loop. Program 4 prints numbers from 1 to 5 using a while loop. Program 5 adds numbers entered by the user until 0 is entered, using a do-while loop. Each program is accompanied by sample output.
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)
22 views5 pages

APL Experiment No2

This document describes 5 programs that use different loop control statements like for, while, and do-while loops. Program 1 prints numbers from 1 to 10 using a for loop. Program 2 calculates the factorial of a number entered by the user using a for loop. Program 3 calculates the sum of the first n natural numbers entered by the user using a for loop. Program 4 prints numbers from 1 to 5 using a while loop. Program 5 adds numbers entered by the user until 0 is entered, using a do-while loop. Each program is accompanied by sample output.
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/ 5

Experiment no 2

Title: Program to study of loop control statement:


Program1
Program to Print numbers from 1 to 10
// Print numbers from 1 to 10
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i)
{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10

Program 2
Program to find Factorial of number. Using for loop
#include<stdio.h>
int main()
{
int num, count, fact = 1;
printf("Enter a number to find its Factorial\n");
scanf("%d", &num);
for(count = 1; count <= num; count++)
{
fact = fact * count;
}
printf("Factorial of %d is %d\n", num, fact);
return 0;
}
Output 1:
Enter a number to find its Factorial
5
Factorial of 5 is 120

Program 3
Program to calculate the sum of first n natural numbers
// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
#include <stdio.h>
int main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when num is less than count
for(count = 1; count <= num; ++count)
{
sum += count;
}
printf("Sum = %d", sum);
return 0;
}
Output
Enter a positive integer: 10
Sum = 55

Program 4
Program to Print numbers from 1 to 5 using while loop
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
++i;
}
return 0;
}
Output
1
2
3
4
5
Here, we have initialized i to 1.
When i = 1, the test expression i <= 5 is true. Hence, the body of the while
loop is executed. This prints 1 on the screen and the value of i is increased
to 2.
Now, i = 2, the test expression i <= 5 is again true. The body of the while
loop is executed again. This prints 2 on the screen and the value of i is
increased to 3.
This process goes on until i becomes 6. Then, the test expression i <= 5
will be false and the loop terminates.

Program 5

Program to add numbers until the user enters zero


#include <stdio.h>
int main() {
double number, sum = 0;
// the body of the loop is executed at least once
do {
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}
Output
Enter a number: 1.5
Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70

You might also like