//program to display following numeric pattern.
// 1 2 3 4 5
// 2 4 6 8 10
// 3 6 9 12 15
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=5;j++)
{
printf(" %d",i*j);
}
printf("\n");
}
getch();
}
-----------------------------------
logic in mind:-
------------------------------------
1)we have to display 1 2 3 4 5 then different numbers in second row..
2)we can see there are three rows so we use first loop(outer) with three execution times. And there are 5 columns so we use one another loop for columns(inner).
3) since the pattern is like
1*1 1*2 1*3 1*4 1*5
2*1 2*2 2*3 2*4 2*5
and so on.
4) For this, We have used nested loop as shown above.
5)For each value of outer loop(value of 'i'), the inner loop executes 5 times. And it goes to display numbers.
while displaying, it multiplies both numbers and displays on screen.
------------------
program's screenshot with output:-
----------------------------------------------------
output's screen
// 1 2 3 4 5
// 2 4 6 8 10
// 3 6 9 12 15
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int i,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=5;j++)
{
printf(" %d",i*j);
}
printf("\n");
}
getch();
}
-----------------------------------
logic in mind:-
------------------------------------
1)we have to display 1 2 3 4 5 then different numbers in second row..
2)we can see there are three rows so we use first loop(outer) with three execution times. And there are 5 columns so we use one another loop for columns(inner).
3) since the pattern is like
1*1 1*2 1*3 1*4 1*5
2*1 2*2 2*3 2*4 2*5
and so on.
4) For this, We have used nested loop as shown above.
5)For each value of outer loop(value of 'i'), the inner loop executes 5 times. And it goes to display numbers.
while displaying, it multiplies both numbers and displays on screen.
------------------
program's screenshot with output:-
----------------------------------------------------
program's screen |
No comments:
Post a Comment