19. Some program using for loop

Here i give some program by using for loop.
Q1. Useing for loop printf 10 star.

/* Program 1 */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=0;i<10;i++)
printf("* ");
getch();
}

Explanation:
When the program is execute initialization scction i=0, then it execute the condition (i<10). First time when i=0 then the condition is true so it execute the printf function and pint a star. After execute the printf function it will go back in the increment section and increment the value of i, then i=1. Then again execute the condition and work again and again. When i=10 the condition is false and it can not exicute the printf function. So that the output is :

* * * * * * * * * *

/* Program 2 */

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n;
printf("Enter the value : ");
scanf("%d",&n);
for(i=1;i<=n;i+=4)
{
for(i=1;i<=n;i+=4)
printf(" %d",i);
}
getch();
}
if n=10
Output:
1 5 9

/* Program 3 */

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n;
printf("Enter the value : ");
scanf("%d",&n);
for(i=0;n>=i;n-=4)
{
printf(" %d",n);
}
getch();
}
if n=20
Output is:
20 16 12 8 4 0
Post by Arnob



No comments:

Post a Comment

free counters