//program to display following numeric pattern.
// 1 2 3 4 5
// 6 7 8 9 10
// 11 12 13 14 15
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int i,j,k=0;
for(i=1;i<=3;i++)
{
for(j=1;j<=5;j++)
{
printf(" %d",j+k);
}
printf("\n");
k=k+5;}
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(five times execution) for columns(inner).
3) since the pattern is like
1+0 2+0 3+0 4+0 5+0
1+5 2+5 3+5 4+5 5+5
and so on.
4) For this, We have used nested loop as shown above and one variable named k with initial value '0'.
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 adds both numbers (j and k)and displays on screen.
6)In second display the value of k is increased by 5 as shown above.
-------------------------------------------------------------------------------------------------------
program's screenshot
---------------------------
---------------------------
output is:-
----------------
// 1 2 3 4 5
// 6 7 8 9 10
// 11 12 13 14 15
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int i,j,k=0;
for(i=1;i<=3;i++)
{
for(j=1;j<=5;j++)
{
printf(" %d",j+k);
}
printf("\n");
k=k+5;}
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(five times execution) for columns(inner).
3) since the pattern is like
1+0 2+0 3+0 4+0 5+0
1+5 2+5 3+5 4+5 5+5
and so on.
4) For this, We have used nested loop as shown above and one variable named k with initial value '0'.
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 adds both numbers (j and k)and displays on screen.
6)In second display the value of k is increased by 5 as shown above.
-------------------------------------------------------------------------------------------------------
program's screenshot
---------------------------
---------------------------
output is:-
----------------
No comments:
Post a Comment