Puzzled Student

Hi im really confused with c language programming.Im a middle school student and our instructor wanted us to make a program that will display the following:

54321
4321
321
21
1

displaying that should be relatively easy but our instructor asked us to use a do-while or a do-for statement.this is what i came up with
#include<stdio.h>
int main()
{
int x,y;
for (x=5;x>=1;x--)
{
for(y=1;y<=5;y++)
{
printf("%i",x);
x--;
}
printf("\n");
}
getch();
}

All this program displayed was 54321 please help ! any advice would do.
Hahaha I got the same question on my C++ exam only instead of numbers it was

*****
****
***
**
*

I didn't get it write then, but I got home and with my brother's help. I did it. I won't give you the answer directly, I'll give you hints. If you didn't know then I'll give you the answer.

You started it kind of correctly, when you did the 2 for loops inside each other. But you gotta change something in the second for loop to make it print 5 digits, then 4, then 3, then 2, then 1.

Your program gives

54321
54321
54321
54321
54321

If you change the 2nd for loop. HINT: Include x somewhere in the 2nd for loop.

you will get

54321
4321
321
21
1
Last edited on
I don't know if you answered it or not, but here is the code. :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;
int main()
{
     int x,y;
     for (x=5;x>=1;x--)
     {
          for(y=x;y>=1;y--)
          {
               cout<<y;
          }
     cout<<"\n";
     }
return 0;
}
Last edited on
Hi i'm back and Thank for your posts.But i kinda managed to correct my crappy code and here's what i came up with:


#include<stdio.h>
int main()
{
int x,y,z;
for(x=5;x>=1;x--)
{
z=x;
for(y=1;y<=z;y=y)
{
printf("%i",z);
z--;
}
printf("\n");
}
getch();
}

//but thanks anyway to hannad.
Last edited on
Hello my friend,
I see that you have corrected it, but instead of letting z into the program. Use x instead

I Edited Your Last Code Into This
1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
int main()
{
	int x,y;
	for(x=5;x>=1;x--)
	{
		for(y=x;y>=1;y--)
			printf("%i",y);
		printf("\n");
	}
	getch();
}
//Now it should work totally fine without the z variable. 


Yours is totally right, but when dealing with bigger programs, the lesser variables you put, the better!
Last edited on
Wow that's even better.I didn't think that i could have declared y=x then followed by a decrement.simply brilliant.How about this hannad:

54321
5432
543
54
5

this problem was the actual exam of my seatmate and he flunked it Lol.so what's the for loop program that would display this?
Here you go :D
1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
int main()
{
	int x,y;
        for(x=1;x<=5;x++)
	{
		for(y=5;y<=1;y--)
                      printf("%i",y);
		printf("\n");
	}
	getch();
}


It's just a bit manipulating the for loops. Try manipulating it in other ways and see what you get. You can get

12345
1234
123
12
1

OR

54321
5432
543
54
5

OR

55555
4444
333
22
1

Those are the ones I thought about just right now. There are more possibilities :D
Last edited on
Woah pretty nice.and it's the holidays so I got a vacation break(OH YEAH)

i might need your help anytime soon so keep track of this thread

Thanks and happy holidays.

our final exam is coming in january.And it's all about C++ T.T
ok :)
This is the first year my high school has offered a computer programming class. We're studying c++. First semester were just doing text based applications. Second semester were switching to Win32 programming.

A great place to challenge yourself is projecteuler.net, I had done up to question 15, then I showed my teacher. He loves the website and has begun to use it in school.

Personally, I use a mac and have began to learn how to use cocoa. If you have any beginner questions I would love to help.
Thank you for this website. I did 6 of the problems, and currently stuck on 2 more :D.
Topic archived. No new replies allowed.