Pages

Monday, January 02, 2012

Adding 1 to each digit of a number

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:

paras said...

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.

Unknown said...

Thnx.........nyc logic.

Unknown said...

nyc logic...thnx a lot...

Koushik said...

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);
}

Koushik said...

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);
}

Unknown said...

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)

Unknown said...

PLEASE REFER FOR SIMPLE AND EASY WAY TO WRITE THE CODE

Anonymous said...

i have found a solution but the problem is that when i am coding it,it is not working.

Unknown said...

#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();
}

Unknown said...

done

Unknown said...

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

Unknown said...

#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;


}
}

Saurabh said...

Thanks, this is perfect code

Anonymous said...

Perfect code straight and simple

Sikh Revolution said...

#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;
}

manjuhakki said...

This doesn't work if we take 1239 it is getting as 2350, but we want 2340

Taneja said...

thanveer Shah your program is flawed if any one of the digits is entered as 9

Tanuj tsk said...

*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

Tanuj tsk said...

*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

Shiv_Thakur said...

#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;
}

Unknown said...

Galat hai

Anonymous said...

And you get -100/100��