0
I have to do a program to reverse the digits of a number and print it between brackets []; To begin , the reading loop has to stop when the number typed is equal to 0, however if the value is a sequence of 0s it has to continue (ex:000 or 00); And I don’t know if my logic is right, can you help me? follows the reading code:
do
{
scanf("%d", &n[i]);
k=n[i];
i++;
}while(k!=0);
and the entire code:
#include <stdio.h>
#include <math.h>
int main ()
{
int n[100],v[100],a[100],b[100];
int i=0,cont,j,k,x,y,t,p;
do
{
scanf("%d", &n[i]);
k=n[i];
i++;
}while(k!=0);
for(j=0;j<i;j++)
{
if(n[j]>=1000)
{
p=n[j]/1000;
v[j]=n[j]%1000;
if(v[j]>=100)
{
y=v[j]/100;
a[j]=v[j]%100;
if(a[j]>=10)
{
t=a[j]/10;
b[j]=a[j]%10;
printf("[%d][%d][%d][%d]\n", a[j]%10,t,y,p);
}
}
}
else if(n[j]>=100 && n[j]<1000)
{
y=n[j]/100;
a[j]=n[j]%100;
if(a[j]>=10)
{
t=a[j]/10;
b[j]=a[j]%10;
printf("[%d][%d][%d]\n", a[j]%10,t,y);
}
}
}
return 0;
}
Mathematically zeroes on the left are totally expendable, there is no difference between the number 0 and 000. Maybe you don’t want to consider it as a number but as a string and then "'0" is different from "000".
– anonimo
000
is not a number, and so the problem starts right there. Read everything as text and you won’t even have a problem– Isac
how char? Because in the statement of the problem says that if the number typed is 000 , it should print only 0, but if it is typed 000 the rest does not run
– Noct
read with char,this should solve the problem
– IanMoone