-
February 6th, 2009, 06:50 AM
#1
Pyramids in C++
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 06:54 AM.
-
February 6th, 2009, 06:55 AM
#2
Re: Pyramids in C++
What have you tried?
You could use [code][/code] tags to help format your expected output.
-
February 6th, 2009, 10:01 AM
#3
-
February 6th, 2009, 05:34 PM
#4
Re: Pyramids in C++
Originally Posted by cilu
Hello cilu!
How are you?
Haha that looks so cool!
-
February 7th, 2009, 03:31 AM
#5
Re: Pyramids in C++
Originally Posted by cilu
Thanks for your reply, it helped me, but I am still not able to get
1
23
456
78910
can you please give me some more tips.
I also made another program for printing
....1
...121
..12321
. 1234321
1 2 3 4 5 4 3 2 1
Last edited by siddharth_1202; February 7th, 2009 at 08:51 AM.
-
February 7th, 2009, 09:53 AM
#6
Re: Pyramids in C++
Originally Posted by siddharth_1202
Thanks for your reply, it helped me, but I am still not able to get
1
23
456
78910
can you please give me some more tips.
You've shown no code, so what "tips" can we give you? Maybe "work harder".
I also made another program for printing
....1
...121
..12321
. 1234321
1 2 3 4 5 4 3 2 1
So far, you've shown no code that you came up with. You need to show us what you've done.
Regards,
Paul McKenzie
-
February 7th, 2009, 01:41 PM
#7
Re: Pyramids in C++
Originally Posted by Paul McKenzie
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.
Regards,
Paul McKenzie
//WAP to display
// 1
// 1 2 1
// 1 2 3 2 1
// 1 2 3 4 3 2 1
// 1 2 3 4 5 4 3 2 1
// achived:
// 1
// 1 2 1
// 1 2 3 2 1 0
// 1 2 3 4 3 2 1 0 -1
// 1 2 3 4 5 4 3 2 1 0 -1 -2
#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;
}
-
February 7th, 2009, 01:44 PM
#8
Re: Pyramids in C++
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;
}
-
February 7th, 2009, 02:05 PM
#9
Re: Pyramids in C++
Originally Posted by siddharth_1202
//WAP to display
// 1
// 1 2 1
// 1 2 3 2 1
// 1 2 3 4 3 2 1
// 1 2 3 4 5 4 3 2 1
// achived:
// 1
// 1 2 1
// 1 2 3 2 1 0
// 1 2 3 4 3 2 1 0 -1
// 1 2 3 4 5 4 3 2 1 0 -1 -2
Please use code tags when posting code.
Code:
#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().
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.
Code:
for (a=col-2; a>=1;a--)
Regards,
Paul McKenzie
-
February 8th, 2009, 05:32 AM
#10
Re: Pyramids in C++
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;
}
-
February 9th, 2009, 04:47 AM
#11
Re: Pyramids in C++
Dear Friends,
Can you please help me with the following program?
Code:
//WAP to print 1 to 15 in different lines in incrementing manner.
// 1
// 2 3
// 4 5 6
// 7 8 9 10
// 11 12 13 14 15
#include<iostream>
class one
{
int x,a;
public:
void display()
{
for (x=1;x<6;x++)
{
for (a=1;a<=x;a++)
cout<<a;
cout<<"\n";
}
}
};
void main()
{
one z;
z.display();
return 0;
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|