This program adds 1 to each digit of the number.
If a five digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits.For example if the number that is input is 12391 then the output should be displayed as 23402
Here first we have added one to each digit. If any digit becomes 10 then replacing it with 0
#include<stdio.h>
void main(){
long r,num,temp,sum=0,i,temp_sum=0;
clrscr();
printf("Enter 5 digit number : ");
scanf("%ld",&num);
printf("\nThe number after adding 1 to each digit : ");
while(num!=0){
r=num%10;
temp=r+1;
if(temp==10)
temp=0;
num=num/10;
sum=(sum*10)+temp;
}
while(sum!=0){
i=sum%10;
sum=sum/10;
temp_sum=(temp_sum*10)+i;
}
printf("%ld",temp_sum);
getch();
}
output:
Enter 5 digit number : 12345
The number after adding 1 to each digit : 23456
Enter 5 digit number : 12935
The number after adding 1 to each digit : 23046
22 comments:
Yes i really appreciate your code and its working fine with exception of 99999 but still its likable..but iam unable to understand logic behind this means but how you come to reach code.
Any words would be really appreciatable.
Thnx.........nyc logic.
nyc logic...thnx a lot...
for the 999 exception to work modify the loop to the following
for(;a!=0;a/=10)
{
int b=a%10;
temp=b+1;
if(temp==10)
{
if(sum==0)
sum=10*10;
else
sum=sum*10;
}
else
sum=(sum*10)+temp;
}
if(sum%10==0) printf("Sum after adding 1 to each digit is=%d",(sum/10));
else
{
for(;sum!=0;sum/=10)
{
int c=sum%10;
temp_sum=(temp_sum*10)+c;
}
printf("Sum after adding 1 to each digit is=%d",temp_sum);
}
for the 999 exception to work modify the loop to the following
for(;a!=0;a/=10)
{
int b=a%10;
temp=b+1;
if(temp==10)
{
if(sum==0)
sum=10*10;
else
sum=sum*10;
}
else
sum=(sum*10)+temp;
}
if(sum%10==0) printf("Sum after adding 1 to each digit is=%d",(sum/10));
else
{
for(;sum!=0;sum/=10)
{
int c=sum%10;
temp_sum=(temp_sum*10)+c;
}
printf("Sum after adding 1 to each digit is=%d",temp_sum);
}
IT IS SIMPLE AND EASY TO UNDERSTAND
#include
main()
{
int num=0,i=0,rem=0,n;
long int a[10];
printf("ENTER THE NUMBER OF DIGITS IN A NUMBER\n");
scanf("%d",&n);
printf("ENTER THE NUMBER TO WHICH YOU ADD 1 TO EACH DIGIT\n");
scanf("%d",&num);
while(num!=0)
{
rem=num%10;
rem++;
a[i++]=rem;
num=num/10;
}
for(i=n-1;i>=0;i--)
{
printf("%ld",a[i]);
}
}
OUTPUT:
ENTER THE NUMBER OF DIGITS IN A NUMBER
4
ENTER THE NUMBER TO WHICH YOU ADD 1 TO EACH DIGIT
1239
23410
(1+1,2+1,3+1,9+1)
PLEASE REFER FOR SIMPLE AND EASY WAY TO WRITE THE CODE
i have found a solution but the problem is that when i am coding it,it is not working.
#include
int main()
{
int a,b=0,c=0,d=0,e=0;
printf("Enter te no: ");
scanf("%d",&a);
b=a;
while(b>0)
{
b=b/10;
++c;
}
printf("No. of digits %d ",c);
while(c!=0)
{
d=d*10+1;
c--;
}
e=a+d;
printf(" %d",e);
getch();
}
done
EASIEST WAY
#include
int main()
{
int a,b,c,d,e;
printf("Enter 5numbers=");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
a++;
b++;
c++;
d++;
e++;
printf("%d%d%d%d%d",a,b,c,d,e);
return 0;
}
JUST LEAVE SPACE AFTER TYPING EACH DIGIT IN THE OUTPUT
#include
int main(){
int num,y,x,rem;
printf(" enter the number");
scanf("%d",num);
while(num>0){
rem=num%10;
x=1+rem;
y=x%10;
printf("%d",y);
x=x/10;
num=num+x;
num/10;
}
}
Thanks, this is perfect code
Perfect code straight and simple
#include
/* Include other headers as needed */
int main()
{int N;
scanf("%d",&N);
for(int i=0;i0){
a++;
n=n/10;
}
int no=0;
for(int j=0;j<a;j++){
no=no*10+1;
}
printf("%d\n",realn+no);
}
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
This doesn't work if we take 1239 it is getting as 2350, but we want 2340
thanveer Shah your program is flawed if any one of the digits is entered as 9
*This is The MOST EAsiest code YOu all Will Ever SEE in your LIfE World Class ANswer is here NOw-
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{Scanner k =new Scanner(System.in);
int a,b,c,d=0,p=1;
a=k.nextInt();
while(a>0)
{
b=a%10;
c=b+1;
if(c==10)
c=0;
d=d+c*p;
a=a/10;
p=p*10;
}
System.out.println(d);
}
}
Thanks
*This is The MOST EAsiest code YOu all Will Ever SEE in your LIfE World Class ANswer is here NOw-
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{Scanner k =new Scanner(System.in);
int a,b,c,d=0,p=1;
a=k.nextInt();
while(a>0)
{
b=a%10;
c=b+1;
if(c==10)
c=0;
d=d+c*p;
a=a/10;
p=p*10;
}
System.out.println(d);
}
}
Thanks
#include
/* Include other headers as needed */
int main()
{
int a,b,c,d,e;
scanf("%d",&a);
e=(a%10+1)%10;
a/=10
d=(a%10+1)%10;
a/=10;
c=(a%10+1)%10;;
a/=10;
b=(a%10+1)%10;
a=(a/10+1)%10;
a=a*10000+b*1000+c*100+d*10+e;
printf("%d",a);
return 0;
}
Galat hai
And you get -100/100��
Post a Comment