C File Sample - Looping Statements
PART 4: LOOPING STATEMENTS
// Program For Demonstrating For Loop .
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("Enter Any Numbers : ");
scanf("%d",&n);
printf("\nOutput Is As Follows :
\n\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
Output :
Enter Any Numbers : 5
Output Is As Follows :
1
12
123
1234
12345
// Program For Demonstrating
While Loop.
#include<stdio.h>
#include<conio.h>
void main()
{
long n;
int
i,j,rem;
clrscr();
printf("Enter Any Numbers : ");
scanf("%ld",&n);
printf("\nReverse Of Number Is : ");
while(n>0)
{
rem=n%10;
printf("%d",rem);
n=n/10;
}
getch();
}
Output :
Enter Any Numbers : 12345
Reverse Of Number Is : 54321
// Program To Demonstrate
Do-While Loop .
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1;
long
fact=1;
clrscr();
printf("Enter Any Number : ");
scanf("%d",&n);
do
{
fact=fact*i;
i++;
}while(i<=n);
printf("\nFactorial Of %d Is %ld
!!!",n,fact);
getch();
}
Output :
Enter Any Number : 5
Factorial Of 5 Is 120 !!!
// Program To Genrate The
Following Outputs
/* 1
1 2 1
1
2 3 2 1
1 2 3 4
3 2 1
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n,sp,i,j,k,s;
clrscr();
printf("Enter Any Number :");
scanf("%d",&n);
printf("\nYour Series Is As
:\n\n");
sp=n;
for(i=1;i<=n;i++)
{
for(s=sp;s>=1;s--)
{
printf(" ");
}
for(j=1;j<i;j++)
{
printf("%d",j);
}
for(k=i;k>=1;k--)
{
printf("%d",k);
}
sp--;
printf("\n");
}
getch();
}
Output :
Enter Any Number : 5
Your Series Is As :
1
121
12321
1234321
123454321
// Program To Generate The
Following Outputs
/*
*
* * *
*
* * * *
* *
* * * * *
*/
#include<stdio.h>
#include<conio.h>
void main()
{
int
n,sp,i,j,s,c;
clrscr();
printf("Enter Any Number :");
scanf("%d",&n);
printf("\nYour Series Is As
:\n\n");
sp=n;
c=1;
for(i=1;i<=n;i++)
{
for(s=sp;s>=1;s--)
{
printf(" ");
}
for(j=1;j<=c;j++)
{
printf("*");
}
c=c+2;
sp--;
printf("\n");
}
getch();
}
Output :
Enter Any Number : 5
Your Series Is As :
*
***
*****
*******
*********
// Program By Using For Loop To
Make Star Pyramid
#include<stdio.h>
#include<conio.h>
void main()
{
int
i,j,k,l,n,s,s1,s2,m;
void space(int);
clrscr();
printf("Enter Any Number : ");
scanf("%d",&n);
if(n<=1||n>9)
{
for(int t=1;t<=10;t++)
{
printf("\n");
}
printf("Please Enter Number Which
Is\n");
printf("Greater Than 1 And Less
Than Or Equal To 9\n");
}
else
{
s=(n*2)+15;
int
spc=s;
//Top
Pyramid
for(i=1;i<=n;i++)
{
space(s);
for(j=i;j>1;j--)
{
printf("%d",j);
}
for(l=1;l<=i;l++)
{
printf("%d",l);
}
printf("\n");
s--;
}
s1=n+16;
m=1;
s2=n+(n-3);
//Left Pyramid & Right Pyramid
for(i=n;i>=1;i--)
{
space(s1);
for(j=1;j<=m;j++)
{
printf("%d",i);
}
space(s2);
for(l=1;l<=m;l++)
{
printf("%d",i);
}
s1--;
m++;
printf("\n");
if(i==1)
{
m=n-1;
s1=spc-(n+(n-3));
s2=n+(n-3);
for(int i=2;i<=n;i++)
{
space(s1);
for(int j=1;j<=m;j++)
{
printf("%d",i);
}
space(s2);
for(int l=1;l<=m;l++)
{
printf("%d",i);
}
s1++;
m--;
printf("\n");
}
}
}
//Bottm Pyramid
s=spc-(n-1);
for(int i=n;i>=1;i--)
{
space(s);
for(int j=i;j>=1;j--)
{
printf("%d",j);
}
for(int k=2;k<=i;k++)
{
printf("%d",k);
}
s++;
printf("\n");
}
}
getch();
}
void space(int sp)
{
for(int p=1;p<=sp;p++)
{
printf(" ");
}
}
Output :
Enter Any Number :
5
1
212
32123
4321234
543212345
5 5
44 44
333 333
2222 2222
11111 11111
2222 2222
333 333
44 44
5 5
543212345
4321234
32123
212
1

Comments
Post a Comment