Programs On Loops - Nested Loops - Jump Statements
Programs On Loops - Nested Loops - Jump Statements
while loop
1) WAP to display first n natural numbers using while loop(Natural numbers starts from 1)
#include<stdio.h>
int main()
{
int i=1,n;
printf("\n Enter n:");
scanf("%d",&n);
while(i<=n)
{
printf("\n%d",i);
i++;
}
return 0;
}
2) WAP to display sum of all numbers from 1 to n
#include<stdio.h>
int main()
{
int i=1,n,sum=0;
printf("\n Enter n:");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("\n Sum is:%d",sum);
return 0;
}
3) WAP to count all even numbers from m to n
#include<stdio.h>
int main()
{
int m,n,count=0;
printf("\n Enter m and n:");
scanf("%d %d",&m,&n);
while(m<=n)
{
if(m%2==0)
{
count++;
}
m++;
}
printf("\n Count of even numbers is:%d",count);
return 0;
}
4) WAP to display factorial of a number
#include<stdio.h>
int main()
{
long long int fact=1;
int n,i=1;
printf("\n Enter number:");
scanf("%d",&n);
if(n==0)
{
printf("\n Factorial is:1");
}
else
{
while(i<=n)
{
fact=fact*i;
i++;
}
printf("\n Factorial is:%d",fact);
}
return 0;
}
5) WAP to count total number of digits in a number
#include<stdio.h>
int main()
{
int n,count=0;
printf("\n Enter number:");
scanf("%d",&n);
while(n!=0)
{
count++;
n=n/10;
}
printf("\n Total number of digits are:%d",count);
return 0;
}
6) WAP to find the sum of all digits of a number
#include<stdio.h>
int main()
{
int n,digit,sum=0;
printf("\n Enter number:");
scanf("%d",&n);
while(n!=0)
{
digit=n%10;
sum=sum+digit;
n=n/10;
}
printf("\n Sum of the digits is:%d",sum);
return 0;
}
7) WAP to display the reverse of a number
#include <stdio.h>
int main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n = n/10;
}
printf("Reversed Number = %d", reversedNumber);
return 0;
}
Dry Running of the logic (Explanation):
8) WAP to check whether the entered integer is palindrome or not
//Note: Palindrome number is that one whose reverse is equivalent to the original number
#include <stdio.h>
int main()
{
int n, reversedInteger = 0, remainder, originalInteger;
printf("Enter an integer: ");
scanf("%d", &n);
originalInteger = n;
// reversed integer is stored in variable
while( n!=0 )
{
remainder = n%10;
reversedInteger = reversedInteger*10 + remainder;
n = n/10;
}
// palindrome if orignalInteger and reversedInteger are equal
if (originalInteger == reversedInteger)
printf("%d is a palindrome.", originalInteger);
else
printf("%d is not a palindrome.", originalInteger);
return 0;
}
9) WAP to check whether the entered number is Armstrong number or not
//Note: Armstrong number is that number which is equivalent to the sum of the powers of
its digit, where power of each digit is dependent upon the count of the digits, e.g: 371(Here
count of digits is: 3, so power is 3, now 3*3*3+7*7*7+1*1*1=27+343+1=371, another
example:1634 (Here count of digits is:4, so power is 4, now
1*1*1*1+6*6*6*6+3*3*3*3+4*4*4*4=1+1296+81+256=1634)
#include <stdio.h>
#include <math.h>
int main()
{
int number, originalNumber, remainder, result = 0, n = 0 ;
printf("Enter an integer: ");
scanf("%d", &number);
originalNumber = number;
while (originalNumber != 0)
{
originalNumber =originalNumber/10;
n++;
}
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber%10;
result =result+pow(remainder, n);
originalNumber=originalNumber/10;
}
if(result == number)
printf("%d is an Armstrong number.", number);
else
printf("%d is not an Armstrong number.", number);
return 0;
}
for loop
10) WAP to check whether the entered number is prime or composite
#include <stdio.h>
int main()
{
int n, i, status = 0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2; i<=n/2; i++)
{
// condition for nonprime number
if(n%i==0)
{
status=1;
break;
}
}
if (status==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number(or composite).",n);
return 0;
}
Dry running (Explanation of logic):
11) WAP to display n terms of Fibonacci series (e.g. if n=5, then fibonaaci terms are: 0 1 1 2 3
…..)
#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; i++)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
Dry running (Explanation of logic)
12) WAP to display multiplication table upto any limit for any integer: n
#include <stdio.h>
int main()
{
int n, i=1,limit=1;;
printf("Enter an integer: ");
scanf("%d",&n);
printf("\n Enter the limit:");
scanf("%d",&limit);
for(i=1;i<=limit;i++)
{
printf("%d X %d = %d \n", n, i, n*i);
}
return 0;
}
Practice questions:
Implement all the programs which are done using while loop with the help of for
loop [i.e. from Program no. 1 to 9]
do while loop
13) WAP to find the sum of all multiples of 5 from 1 to 100
#include <stdio.h>
int main()
{
int i=1,n,sum=0;
printf("\n Enter value of n:");
scanf("%d",&n);
do
{
if(i%5==0)
{
sum=sum+i;
}
i++;
}while(i<=n);
printf("\n Sum of all multiples of 5 is:%d",sum);
return 0;
}
14) WAP to count multiples of 7 from m to n
#include <stdio.h>
int main()
{
int m,n,count=0;
printf("\n Enter value of m and n:");
scanf("%d %d",&m,&n);
do
{
if(m%7==0)
{
count++;
}
m++;
}while(m<=n);
printf("\n Count of multiples of 7 is:%d",count);
return 0;
}
Practice questions:
Implement all the programs which are done using while loop and for loop with the
help of do-while loop [i.e. from Program no. 1 to 12]
***
##
d) a
ab
a b c
a b c d
● WAP to count all palindromes from 100 to 1000
}
Note: This program will check the input, if the input is greater than 100, then control is
transferred to input section again, as input should be less than or equal to 100, here y is the
label for goto
return statement: It is used to return a value from a function, Even in main() function it is
used to return a value, and normally it is placed at the end part of function body, because
after return statement, no other statement will execute, as function will return the value.
This statement can be used in user defined function, which can return the value obtained
after doing some calculation in the function body.
#include<stdio.h>
int main()
{
printf("\n Checking return statement");
return 0;
printf("\n Statement after return");
}
Output: Checking return statement
Note: Statement after return 0 will not work as main() has returned the value and after that
no statement will execute