You are on page 1of 4

Example: Write a program to factorial of an integer using for loop.

#include<stdio.h>
#include<conio.h>
int main()
{
int n,i;
long int fact=1;
// crlscr();
printf("Entet the value of n=");
scanf("%d",&n);
for(i=1; i<=n; i++)
fact=fact*i;
printf("fact is=%ld", fact);
getch();
return 0; }
Example: Write a program to display the following format
*
* *
* * *
* * * *
* * * * *
#include<stdio.h>
#include<conio.h>
int main()
{ int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf("* \t");
}
printf("\n");
}
getch();
return 0;
}

EXCERSICE
Example: Write a program to display the following format
* * * * *
* * * *
* * *
* *
*
Example: Write a program to display the following format
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
2. while loop
- It is often the case in programming that you want to do something a fixed number of times.
- The statements within the while loop would keep on getting executed till the condition being
tested remains true. When the condition becomes false, the control passes to the first
statement that follows the body of the while loop
- In place of the condition in the while loop there can be any valid expression, that is, condition
being tested may use relational or logical operators. Example: while ( i >= 10 && j <= 15 ) .
- The statement within while loop may be single statement or block of statements.
- Loop counter may be integer or float.
Syntax
int i=1; <----- initialization part
while(condition) <--- condition part
{
----
i++/i--; <---- increment/decrement part
}

Example: Write a program to display table of a number using while loop


#include<stdio.h>
#include<conio.h>
int main() {
int n,i=1,tab;
clrscr();
printf("Enter the value of n=");
scanf("%d",&n);
while(i<=10)
{ tab=n*i;
printf("%d X %d = %d",n,i,tab);
i++; }
getch();
return 0; }

Example: Write a program to display sum of the following given series


1! + 2! + 3! +................ n!

#include<stdio.h>
#include<conio.h>
main()
{
int i, j, n;
long int fact =1, sum=0;
//clrscr();
printf("Enter limit of the series=");
scanf("%d",&n);
for(j=1;j<=n;j++)
{
fact=fact*j;
sum=sum+fact;
}
printf("sum is=%ld",sum);
getch();
return 0;
}

Example: Write a program to display sum of the following given series using while loop
1! + 2! + 3! +................ n!
>>>>Do this ~!

Example: Write a program to display the sum of the following series using while loop
1/1! + 2/2! + 3/3! + .................... n/n!
>>>>Do this ~!
Example: Write a program to display the sum of digits of a number using while loop
(like that input n=12345 & OUTPUT: sum=1+2+3+4+5=15)
#include<stdio.h>
#include<conio.h>
main()
{
int n,r,sum=0;
printf("Enter the value of n");
scanf("%d", &n);
while(n>0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("Sum of digits is =%d", sum);
getch();
return 0;
}

Example: Write a program to find whether a given number is Armstrong or not.


NB: A number which is equal to the sum of cube of its digits is called Armstrong.
#include<stdio.h>
#include<conio.h>
main()
{
int m, n,r,sum=0;
printf("Enter the value of n");
scanf("%d", &n);
m=n;
while(n>0)
{ r=n%10;
sum=sum+r*r*r;
n=n/10; }
if(sum==m)
printf("this is an armstrong number");
else
printf("this is not an armstrong number");
getch();
return 0;
}

You might also like