Pages

Wednesday, 24 August 2011

For loop programs in c language



 1-
1
22
333
4444
55555

#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf(“%d”, i);
}
printf(“\n”);
}
getch();
}

2- 
1
12
123
1234
12345
#include <stdio.h>
#include<conio.h>
Void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf(“%d”, j);
}
printf(“\n”);
}
getch();
}

 3-
1
21
321
4321
54321
#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf(“%d”, (i+1)-j);
}
printf(“\n”);
}
getch();
}

4 - 
55555
4444
333
22
1

#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=(6-i); j++)
{
printf(“%d”, (6-i) );
}
printf(“\n”);
}
getch();
}

5 -
54321
4321
321
21
1

#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=(6-i); j>=1; j--)
{
printf(“%d”,j);
}
printf(“\n”);
}
getch();
}

6-
5
44
333
2222
11111
#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=5; i>=1; i--)
{
for (j=5; j>=i; j--)
{
printf(“%d”, i);
}
printf(“\n”);
}
getch();
}

7 -
5
54
543
5432
54321
#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j;
clrscr();

for (i=5; i>=1; i--)
{
for (j=5; j>=i; j--)
{
printf(“%d”, j);
}
printf(“\n”);
}
getch();
}

8 -

1
121
12321
1234321
123454321
#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j, k, l;
clrscr();

for (i=1; i<=5; i++)
{
for (k=1; k<=5-i; k++)
{
printf(“%d”, (“ “);
}
for (j=1; j<=i; j++)
{
printf(“%d”,  j);
}
for(i=i-1; i>=1; i--)
{
printf(“%d“, i);
}
printf(“\n”);
}
getch();
}

9 -
1
121
12321
1234321
123454321
1234321
12321
1

#include <stdio.h>
#include<conio.h>
Void main()
{
int  i, j, k, l;
clrscr();
for (i=1; i<=5; i++)
{
for (k=1; k<=5-i; k++)
{
printf(“%d”, (“ “);
}
for (j=1; j<=i; j++)
{
printf(“%d”, j);
}
for(i=i-1; i>=1; i--)
{
printf(“%d“, i);
}
printf(“\n”);
}
for (i=1; i<=4; i++)
{
for (j=1; j<=i; j++)
{
Printf(“%d”, (“ “);
}
for (j=1; j<=5-1; j++)
{
printf(“%d“, j);
}
j=j-2;
for ( ; j>=1; j++)
{
printf(“%d“, j);
}
printf(“\n”);
getch();
}     

10 -           
1234321
12321
121
1
121
12321
1234321


#include <stdio.h>
#include<conio.h>
void main()
{
int  i, j, k, l;
clrscr();

for (i=1; i<=4; i++)
{
for (j=1; j<i; j++)
{
printf(“%d”, (“ “);
}
for (j=1; j<=5-i; j++)
{
printf(“%d”, j);
}
J=j-2;
for ( ; j<=1; j++)
{
printf(“%d“, j);
}
printf(“\n”);
}
for (i=1; i<=4; i++)
{
for (k=1; k<=4-i; k++)
{
printf(“%d”, (“ “);
}
for (j=1; j<=i; j++)
{
Printf(“%d“, j);
}
for (i=i-1 ; i>=1; i--)
{
printf(“%d“, i);
}
printf(“\n”);
getch();
}

11 -
*
**
***
****
*****

#include <stdio.h>
#include<conio.h>
Void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=i; j++)
{
printf(“*”);
}
}
getch();
}

12 -
*****
****
***
**
*

#include <stdio.h> 
#include<conio.h>
Void main()
{
int  i, j;
clrscr();

for (i=1; i<=5; i++)
{
for (j=1; j<=(6-i); j++)
{
printf(“*”);
}
printf(“\n”);
}
getch();
}

Monday, 22 August 2011

Some programs related to For Loop in c prgramming


1- Wap to print all the number from 1 to 10 ?


#include <stdio.h>
#include<conio.h>
void main()

{
      int i;

   clrscr();

for(i=1; i<=10; i++)

 { 
     printf(“n%d” &i);
}

getch();

}

2- Wap to print all the number from 1 to 20 ?

#include <stdio.h>
#include<conio.h>
void main()
{
      Int i;

    clrscr();

for(i=1; i<=20; i++)
 { 
     printf(“n%d” &i);
 }
getch();

}

3- Wap to print all the number from 5 to 15 ?

#include <stdio.h>
#include<conio.h>
void main()
{
      int i;
    clrscr();

 for(i=5; i<=15; i++)
 { 
     printf(“n%d” &i);
}
getch();

}

4- Wap to accept the number from user that is  n  then print all number from 1 to n?

#include <stdio.h>
#include<conio.h>
void main()
{
     int i;
   clrscr();

printf(“enter a number”);
scan(“%d”, &n);

for(i=1; i<=n; i++)
 { 
     printf(“n%d” &i);
}
getch();

}

5- Wap to print the sum  of all the number from 1 to 5 ?

#include <stdio.h>
#include<conio.h>
void main()
{
      int I, sum=0;
      clrscr();

for(i=1; i<=5; i++)
 { 
     sum=sum+i;
}
printf(“sum is %d”, sum);

getch();

}

6- Wap to accept a number from user then calculate the sum of all the number from 1 to n and print a number.

#include <stdio.h>
#include<conio.h>
void main()
{
      int i, sum=0;
      clrscr();

printf(“enter a number”);
scan(“%d”, &n);

for(i=1; i<=n; i++)
 { 
    sum=sum+i;
}

printf(“sum is %d”, sum);

getch();
}

7- Wap to accept a number from user then print all the even number from 1 to n.

#include <stdio.h>
#include<conio.h>
void main()
{
      int  n, i;
     clrscr();

printf(“enter a number”);
scan(“%d”, &n);

for(i=2; i<=n; i++)
 { 
printf(“\n%d”, i);

i=i+1;
}

getch();
}

8- Wap to accept a number from user then print sum of all the even number between 1 to n?
#include <stdio.h>
#include<conio.h>
void main()
{
      int  n, i, sum=0;
     clrscr();

printf(“enter a number”);
scan(“%d”, &n);

for(i=1; i<=n; i++)

   if (i%2==0)
 { 
  sum=sum+i;
 }

printf(sum  is %d”, sum);

getch();
}

9-Wap to accept a number from user then print the sum of all the number from 1 to n and average of number from 1 to n?
  
#include <stdio.h>
#include<conio.h>
void main()
{
      int  n, i, sum=0,avg;
     clrscr();

printf(“enter a number”);
scan(“%d”, &n);

for(i=1; i<=n; i++)

if (i%2==0)
 { 
   sum=sum+i;
}

if=sum/n;
printf(“sum  of number is %d”, sum);
printf(“average  is %d”, avg);

getch();
}

10- Wap to print all the number from 1 to 10?

#include <stdio.h>
#include<conio.h>
void main()
{
     int i;
    clrscr();

for (i=10; i>=i; i--)
{
printf(“%d”, i);
}
getch();

}

11- Wap to accept a number from user that is n then print all the number from n to 1?

#include <stdio.h>
#include<conio.h>
void main()
{
int  n,i;
clrscr();

printf(“enter a number”);
scan(“%d”, &n);

for (i=n; i>=1; i--)

{
printf(“%d”, i);
}
getch();
}




12- Wap to accept a number from user that is n then print all the  odd number from n to 1?


#include <stdio.h>
#include<conio.h>
void main()
{
int  n,i;
clrscr();

printf(“enter a number”);
scan(“%d”, &n);

for (i=n; i>=1; i--)
{
printf(“%d”, i);
i=i-2;
}
getch();
}
  
c programmming online,basic of c language,structure of c programming,c language statements,Compiling a C Program,Pseudo Codes in c,basis data type in c ,variables in C Program,constants in C Program,c operators in C Program,special operators in C ,Decision Making,Branching & Looping in C Program,if statement in C Program,for loop in C Program