Chapter 6:
Looping Structures
Programming Exercise
Problem # 48:
Write a program to display the following output using while loop:
1
3 5
7 9 11
13 15 17 19
21 23 25 27 29
31 33 35 37 39 41
43 45 47 49 51 53 55
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int j, k=1, i=1;
while(i<=7)
{
j=1;
while(j<=i)
{
cout<<k<<" ";
j++;
k+=2;
}
cout<<"\n";
i++;
}
getch();
}
#include<conio.h>
using namespace std;
int main()
{
int j, k=1, i=1;
while(i<=7)
{
j=1;
while(j<=i)
{
cout<<k<<" ";
j++;
k+=2;
}
cout<<"\n";
i++;
}
getch();
}
Output:
Let me know in the comment section if you have any question.
Previous Post:
47. Program that inputs the height of triangle and displays a triangle of alphabets.
Comments
Post a Comment