MS1018 MS1018 - 4 months ago 7
C Question

Displaying an integer in a pattern using % operator

taking an integer number as input from user and displaying it in the following way

5678

678

78

8

my code is working for 4 digit numbers but for 5 digit numbers it is giving wrong output

main()
{
int i,reqpow,n,x,rem,temp;
printf("Enter a number: ");
scanf("%d",&n);

temp=n;
int digit=0;
while(temp!=0)
{
temp=temp/10;
digit++;
}
reqpow=digit-1;
printf("\n\n%d\n",n);
for(i=pow(10,reqpow);i>1;i=i/10)
{
rem=0;
rem=n%i;
printf("%d\n",rem);
}

}


Wrong output for 12345

correct ouput for 123456

Answer
#include<stdio.h>
#include<math.h>
int main() 
{
 int i,reqpow,n,x,rem,temp;
 printf("Enter a number: ");
 scanf("%d",&n);

 temp=n;
 int digit=0;
 while(temp!=0)
{
 temp=temp/10;
 digit++;
 }
reqpow=digit-1;
printf("\n\n%d\n",n);
for(i=pow(10,reqpow);i>1;i=i/10)
{
 rem=0;
 rem=n%i;
 printf("%d\n",rem);
  }
  return 0;
  }

i run your code on Dev-cpp compiler it give output as you requied.