Hi Friends,
I am a beginner in C++ I have to make a programms using loops to get the out put as
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
also I have to write a program having output as
1
121
12321
1234321
123454321
The pyramid mentioned above should look like a pyramid and not like a right triangle.
please help me as I am not very well aware about the concepts of loops.
Last edited by siddharth_1202; February 6th, 2009 at 05:54 AM.
You've shown no code, so what "tips" can we give you? Maybe "work harder".
So far, you've shown no code that you came up with. You need to show us what you've done.
There is another program for which I have written a code but there is problem with it as the display is showing 51 infinite times.
//WAP to display prime numbers between 1 to 50.
#include<iostream>
class prime
{
int a,b,c;
public:
void display()
{
for (a=2;a<=50;a++)
// cout<<"\nEnter a number:";cin>>a;
b=2,c=0;
while (b<=a/2)
{
if (a%b==0)
{
b++;
}
else
{cout<<a;
cout<<", ";
b++;}
}
// if (c==1)
// {cout<<"\nNumber is not a prime number";}
// else
// {cout<<"\nNumber is a prime number";}
}
};
void main()
{
prime num;
num.display();
return 0;
}
#include<iostream>
class hill
{
int row,space,col,a;
public:
void display()
{
for (row=1;row<=5;row++)
{
for (space=18;space>=row*2-1;space--)
{
cout<<" ";
}
for (col=1;col<=row;col++)
cout<<col<<" ";
// cout<<"\n";
for (a=col-2;a>=col*-1+4;a--)
// cout<<"\n";
cout<<a<<" ";
cout<<"\n";
}
}
};
void main()
{
hill formation;
formation.display();
return 0;
}
It's int main(), not void main().
Code:
int main()
Second, look at what the problem is in your output. You are looping too many times in the second loop. The easiest way to fix this is to print only if a is greater or equal to 1.
Thanks a lot Paul, it works. I am very happy, thanks a lot for the help.
Can you please also suggest me what mistake am I making in the below mentioned program?
Code:
//WAP to display prime numbers between 1 to 50.
#include<iostream>
class prime
{
int a,b,c;
public:
void display()
{
for (a=2;a<=50;)
b=2,c=0;
while (b<=a/2)
{
if (a%b==0)
{
b++;
}
else
{
cout<<a;
cout<<", ";
b++;
}
}
a++;
}
};
int main()
{
prime num;
num.display();
return 0;
}
Bookmarks