Practical Assignment 06
Assignment on use of while loops
SET A
1. WAP to add first 10 numbers.
#include<stdio.h>
void main()
{
//Declaring Variable
int i=1, sum = 0;
//Calculating Sum
while(i<=10)
{
sum = sum + i;
i++;
}
printf("\n Sum of first 10 Natural Numbers is : %d", sum);
}
Output🡪
Sum of first 10 Natural Numbers is : 55
2. WAP to add first n numbers.
#include <stdio.h>
int main()
{
int n, sum = 0;
printf("Enter the number :--> ");
scanf("%d", &n);
int i = 1;
printf("The sum of first %d number is :--> ", n);
while(i <= n)
{
sum = sum + i;
i++;
}
printf("%d",sum);
return 0;
}
Output🡪
Enter the number :--> 5
The sum of first 5 number is :--> 15
3. WAP to add any 10 numbers.
#include <stdio.h>
int main()
{
int num, i, sum = 0;
float avg;
printf("Please Enter the 10 Numbers\n");
i = 1;
while(i <= 10)
{
printf("Number %d = ", i);
scanf("%d", &num);
sum = sum + num;
i++;
}
avg = (float)sum / 10.0;
printf("\nThe Sum of 10 Numbers = %d", sum);
printf("\nThe Average of 10 Numbers = %.2f\n", avg);
}
Output🡪
Please Enter the 10 Numbers
Number 1 = 3
Number 2 = 6
Number 3 = 8
Number 4 = 6
Number 5 = 7
Number 6 = 9
Number 7 = 3
Number 8 = 1
Number 9 = 10
Number 10 = 55
The Sum of 10 Numbers = 108
The Average of 10 Numbers = 10.80
4. WAP to print reverse of any number.
#include <stdio.h>
int main()
{
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", reverse);
return 0;
}
Output🡪
Enter an integer: 4532
Reversed number = 2354
5. WAP to find factorial of a given number
#include <stdio.h>
int main()
{
int n,i,f;
f=i=1;
printf("Enter a Number to Find Factorial: ");
scanf("%d",&n);
while(i<=n)
{
f*=i;
i++;
}
printf("The Factorial of %d is : %d",n,f);
return 0;
}
Output🡪
Enter a Number to Find Factorial: 6
The Factorial of 6 is : 720
SET B
1. WAP to print following pattern.
a) for example if n=5 then output should be
*
**
***
****
*****
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}
Output🡪
Enter the number of rows: 5
*
**
***
****
*****
2. a) WAP to print the square of number(s) repeatedly till 1 is entered by user.
Using do-while loop.
#include < stdio.h >
int main()
{
int N, count = 1, sum = 0;
printf("Enter the limit\n");
scanf("%d", &N);
while(count <= N)
{
sum = sum + (count * count);
count++;
}
printf("Sum of squares of numbers from 1 to %d is %d.\n", N, sum);
return 0;
}
Output🡪
Enter the limit
5
Sum of squares of numbers from 1 to 5 is 55.
b) WAP to print following series
a) Fibonacci series of n numbers
#include <stdio.h>
int main() {
int i, n;
// initialize first and second terms
int t1 = 0, t2 = 1;
// initialize the next term (3rd term)
int nextTerm = t1 + t2;
// get no. of terms from user
printf("Enter the number of terms: ");
scanf("%d", &n);
// print the first two terms t1 and t2
printf("Fibonacci Series: %d, %d, ", t1, t2);
// print 3rd to nth terms
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
Output🡪
Enter the number of terms: 7
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8,
b) 1+3+5+7+-------- +n
#include<stdio.h>
int main()
{
int n,sum=0,i=1;
printf("Enter the range of number:");
scanf("%d",&n);
while(i<=n)
{
sum+=i;
i+=2;
}
printf("The sum of the series = %d",sum);
}
Output🡪
Enter the range of number:9
The sum of the series = 25
SET C
1. WAP to print following pattern
a) for example if n=5 then output should be
*
* *
* * *
* * * *
* * * * *
#include <stdio.h>
int main()
{
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space)
{
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
Output🡪
Enter the number of rows: 5
*
***
*****
*******
*********
2. WAP to print table of any given number
#include<stdio.h>
int main()
{
int i=1,n,m,a;
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the table's number:");
scanf("%d",&a);
while(i<=n)
{
printf("\n%d*%d=%d",i,a,i*a);
i++;
} return 0;
}
Output🡪
Enter the limit:10
Enter the table's number:5
1*5=5
2*5=10
3*5=15
4*5=20
5*5=25
6*5=30
7*5=35
8*5=40
9*5=45
10*5=50