I'm trying to finish my program but it kinda doesn't show what it should.
This is the main
code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
int happynum(int num);
#define MAXCOLUM 4
int main()
{
int num, liminf, limsup, cont=0, cont2=0, col, hnum, contc=0, ch;
do{
//DIGITANDO EL RANGO
do{
system("cls");
printf("Enter lower limit: ",161);
scanf("%d",&liminf);
if ( liminf <= 0 )
{
printf("value must be up to 0.\n");
system("pause");
}
}while ( liminf <= 0 );
do{
system("cls");
printf("Enter upper limit: ");
scanf("%5d",&limsup);
if ( limsup <= 0 )
{
printf("value must be up to 0.\n");
system("pause");
}
}while ( limsup <= 0 );
// VALIDANDO EL RANGO
if ( liminf >= limsup )
{
printf("Incorrect range.\n",130);
getch();
}
}while ( liminf >= limsup);
do{
system("cls");
printf("Enter # of columns to show the happy numbers (max 4): \n");
scanf("%d", &col);
if (col > 4 || col < 0)
{
printf("Enter again the # of columns \n");
}
}while (col > 4 || col < 0);
system("cls");
printf("Happy numbers between the range [%d,%d]: \n",163,liminf,limsup);
for (num = liminf; num<=limsup; num++)
{
hnum = happynum(num);
if (hnum== 1)
{
printf("%.d F\n", num);
cont++;
}
else {
printf("%.d \n", num);
cont++;
}
if (contc < col)
{
printf("\t");
contc++;
} else
{
printf("\n");
cont2++;
contc=1;
}
if (cont2>=20)
{
cont2=0;
do{
printf("Press enter to continue");
ch=getch();
system("cls");
}while(ch!=13);
/*printf("Press Enter to Continue");
while( getchar() != '\n' );*/
}
}
return 0;
}
This is the function:
int happynum(int num)
{
int i,dig,sum=0;
while(num!=89 && num!=1)
{
sum=0;
while(num>0)
{
dig=num%10;
num=num/10;
sum=sum+(dig*dig);
}
num=sum;
}
if (num == 1)
{
return 1;
} else
return 0;
}
The problem is that the output should be something like this:
happy numbers between range [1,50]:
1 F 21
2 22
3 23 F
4 24
5 25
6 26
7 F 27
8 28 F
9 29
10 F 30
11 31 F
12 32 F
13 F 33
14 34
15 35
16 36
17 37
18 38
19 F 39
20 40
Press enter to keep showing numbers...
happy numbers between range [1,50]:
41
42
43
44 F
45
46
47
48
49 F
50
But the output that shows it's this:
1 F
2
3
4
5
6
7 F
8
9
10 F
11
12
13 F
14
15
16
17
18
19 F
20
21
22
23 F
24
25
26
27
28 F
29
30
31 F
32 F
33
34
35
36
37
38
39
40
41
42
43
44 F
45
46
47
48
49 F
50
Please check out the code and let me know If there's something I'm missing or something its wrong, thanks in advance.
Solved.
printf("%.d F\n", num); //you should remove the \n here
printf("%.d \n", num); //you should remove the \n here
You should remove the \n from these printf above, because you are intending to print all linebreaks \n in this scope below:
if (contc < col)
{
printf("\t");
contc++;
} else
{
printf("\n");
cont2++;
contc=1; //you should change it to contc =0
}
And, contc=1 in this scope, is wrong. It should be contc =0 because the contc starts from 0 at the beginning, not 1.
After these changes, I have already debugged it in my compiler and the result is just what you want.
THIS IS THE FULL FIX:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <conio.h>
int happynum(int num);
#define MAXCOLUM 4
int main()
{
int num, liminf, limsup, cont = 0, cont2 = 0, col, hnum, contc = 0, ch,c;
do
{
//DIGITANDO EL RANGO
do
{
system("cls");
printf("Enter lower limit: ");
scanf("%d", &liminf);
if (liminf <= 0)
{
printf("value must be up to 0.\n");
system("pause");
}
}
while (liminf <= 0);
do
{
system("cls");
printf("Enter upper limit: ");
scanf("%5d", &limsup);
if (limsup <= 0)
{
printf("value must be up to 0.\n");
system("pause");
}
}
while (limsup <= 0);
// VALIDANDO EL RANGO
if (liminf >= limsup)
{
printf("Incorrect range.\n");
getchar();
}
}
while (liminf >= limsup);
do
{
system("cls");
printf("Enter # of columns to show the happy numbers (max 4): \n");
scanf("%d", &col);
if (col > 4 || col < 0)
{
printf("Enter again the # of columns \n");
}
}
while (col > 4 || col < 0);
system("cls");
printf("Happy numbers between the range [%d,%d]: \n", liminf, limsup);
for (num = liminf; num <= limsup; num++)
{
hnum = happynum(num);
if (hnum == 1)
{
printf("%.d F", num);
cont++;
}
else
{
printf("%.d ", num);
cont++;
}
if (contc < col-1)
{
printf("\t");
contc++;
}
else
{
printf("\n");
cont2++;
contc = 0;
}
if (cont2 >= 20)
{
cont2 = 0;
while ((c = getchar()) != EOF && c != '\n');
do
{
printf("Press enter to continue");
ch = getchar();
system("cls");
}
while (ch != '\r'&&ch!='\n');
}
}
return 0;
}
int happynum(int num)
{
int i, dig, sum = 0;
while (num != 89 && num != 1)
{
sum = 0;
while (num > 0)
{
dig = num % 10;
num = num / 10;
sum = sum + (dig * dig);
}
num = sum;
}
if (num == 1)
{
return 1;
}
return 0;
}
I used while ((c = getchar());= EOF && c != '\n');
to flush the input buffer so that while (ch;= '\r'&&ch!='\n');
can detect input properly.
(your original code while (ch;=13);
does not work proper for me
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.