Write a program that displays the following output using loop:
A
A B
A B C
A B C D
A B C D E
Solution:
#include<iostream.h>
#include<conio.h>
void main()
{
int i,j;
char ch='A'
for ( i= 1; i<=5; i++)
{
for(j=1; j<=i; j++)
{ if (j==1) ch= 'A';
cout<<ch++; }
cout<<endl;
}
getch();
}
Saturday, 28 May 2016
Friday, 27 May 2016
Write a program to generate the following pyramid of digits using nested loop:
Solution:
#include<iostream.h>
#include<conio.h>
void main()
{
int mid;
clrscr();
for(int i=1; i<=10; i++)
{
cout<<" ";
mid =(2*i)-1;
for(int j=1; j<=(10-i); j++)
cout<<" ";
for(j=i; j<=mid; j++)
cout<<(j%10);
for(j =(mid-1); j>=i; j--)
cout<<(j%10);
cout<<endl;
}
getch();
}
Write a program that displays a diamond of asterisks using loop:
Solution:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i, j;
for(i=0; i<=4; i++)
{
for(j=0; j<=5-i; j++)
cout<<" ";
for(j=5-i; j<=5+i; j++)
cout<<"*";
cout<<endl;
}
for(i=2; i<= 5; i++)
{
for(j=0; j<=i; j++)
cout<<" ";
for(j=i; j<=(10-i);j++)
cout<<"*";
cout<<endl;
}
getch();
}
Write a program that inputs the height of a triangle and display it using loop. For example if the user enters height as 5, the program should display the following.
&&&&&&&&&
&&&&&&&
&&&&&
&&&
&
Solution:
#include <iostream.h>
#include<conio.h>
void main ()
{
int i , n , j;
clrscr();
cout<<"Please Give the Value of N as height";
cin>>n;
for(i = n; i >0; i++)
{
for(j=n-i; j>0; j--)
cout<<" ";
for(j = 2*i-1; j>0; j--)
cout<<"&";
cout<<"\n";
}
getch();
}
&&&&&&&&&
&&&&&&&
&&&&&
&&&
&
Solution:
#include <iostream.h>
#include<conio.h>
void main ()
{
int i , n , j;
clrscr();
cout<<"Please Give the Value of N as height";
cin>>n;
for(i = n; i >0; i++)
{
for(j=n-i; j>0; j--)
cout<<" ";
for(j = 2*i-1; j>0; j--)
cout<<"&";
cout<<"\n";
}
getch();
}
Write a program to print the following output using loop:
BBBBBBBBB
. BBBBBBB
. . BBBBB
. . . BBB
B
Solution:
#include<iostream.h>
#include <conio.h>
void main()
{
int i , j , k;
for(i=1; i <=5; i++)
{
for(k = 1; k <=5-i; k++)
cout<<" .";
for(j=1; j <= 2*i-1; j++)
cout<<"B";
cout<<endl;
}
getch();
}
BBBBBBBBB
. BBBBBBB
. . BBBBB
. . . BBB
B
Solution:
#include<iostream.h>
#include <conio.h>
void main()
{
int i , j , k;
for(i=1; i <=5; i++)
{
for(k = 1; k <=5-i; k++)
cout<<" .";
for(j=1; j <= 2*i-1; j++)
cout<<"B";
cout<<endl;
}
getch();
}
Wednesday, 25 May 2016
Write a program to print the following output using loop:
Solution:
#include<iostream.h>
#include<conio.h>
void main()
{
int row, pound, star;
int nrows = 6;
for(row = 1; row <= nrows; row++)
{
for(pound = 1; pound <= nrow-row; pound++)
cout<<"#";
for(star =1; star <= 2*row-1; star++){
if(star == 1 || star == 2*row-1)
cout<<"*";
else
cout<<"#";
for(pound = 1; pound <= nrow-row; pound++)
cout<<"#";
cout<<endl;
}
}
getch();
}
Write a program that uses nested for loops to display the multiplication table shown below.
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Solution:
#include<iostream.h>
#include<conio.h>
void main()
{
int m, n;
clrscr();
m = 1;
while(m <=5)
{
n = 1;
while(n <= 5)
{
cout<<"\t"<<m*n;
n++
}
cout<<"\n";
m++;
}
getch();
}
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Solution:
#include<iostream.h>
#include<conio.h>
void main()
{
int m, n;
clrscr();
m = 1;
while(m <=5)
{
n = 1;
while(n <= 5)
{
cout<<"\t"<<m*n;
n++
}
cout<<"\n";
m++;
}
getch();
}
Subscribe to:
Posts (Atom)