this is my code:
#include%26lt;stdio.h%26gt;
main()
{
int cas,cnt,ctr,x=1,y=2;
clrscr();
printf("Enter case number[1-4]:");
scanf("%d",%26amp;cas);
switch(cas)
{
case 1:
for(cnt=5;cnt%26gt;0;cnt--,x++,y++)
{
for(ctr=cnt;ctr%26gt;0;ctr--)
gotoxy(x,y);
printf("%d",ctr);
printf("\n");
}
break;
case 2:
{
for(cnt=1;cnt%26lt;=5;cnt++)
{
for(ctr=1;ctr%26lt;=6-cnt;ctr++)
printf("%d",ctr);
printf("\n");
}break;
}
case 3:
{
for(cnt=1;cnt%26lt;=5;cnt++)
{
printf("%d",cnt);
if (cnt==1)
for(ctr=2;ctr%26lt;=5;ctr++)
printf("%d",ctr);
printf("\n");
}break;
}
case 4:
{
for(cnt=1;cnt%26lt;=10;cnt++)
{
for(ctr=1;ctr%26lt;=10;ctr++)
printf("%d\t",ctr*cnt);
printf("\n");
}
}
break;
}
getch();
}
and my problem is in the case 1:
which must produce:
54321
4321
321
21
1
but it produces:
54321
4321
321
21
1
thank you in advance.
Can anyone help me to correct this turbo C program? thankyou in advance.?
Well, you could do it with a case structure as you have, but what happens if you need to change it to 10 digits? The program grows twice as large.
I'm not sure why the case structure is being used. You can do this with a single for loop
cas has the value that they entered. So you need one outer loop to control the number of lines being printed, one inner loop to print spaces, and another inner loop to print the numbers as follows:
for (y=cas; y%26gt;0; y--) {
for (spaces=cas-y; spaces%26gt;0; spaces--) {
printf(" ");
}
for (x=y; x%26gt;0; x--) {
printf("%d",x);
}
printf("\n");
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment