+ 1

program : Add a digit to all digits of n input : 2875 4 output; 612119

correct the code

15th Jun 2019, 4:53 AM
Nikash
8 Respostas
15th Jun 2019, 11:24 AM
nicolas turek
nicolas turek - avatar
+ 2
i tried this #include<stdio.h> int main() { int n,k,s=0,rem; scanf("%d%d",&n,&k); while(n!=0) { rem=n%10; s=k+rem; n=n/10; printf("%d",s); } return 0; }
15th Jun 2019, 4:55 AM
Nikash
+ 1
but i get the output 911126.
15th Jun 2019, 4:56 AM
Nikash
+ 1
you can do it using string or array
15th Jun 2019, 5:19 AM
nicolas turek
nicolas turek - avatar
+ 1
#include<stdio.h> int main() { int n,k,s=0,p; scanf("%d%d",&n,&k); while(n!=0) { p = n; while(p>9)p/=10; s=k+p; int x = 1; while(x<n)x*=10; x/=10; n%=x; printf("%d",s); } return 0; }
15th Jun 2019, 5:30 AM
nicolas turek
nicolas turek - avatar
+ 1
#include<stdio.h> int main() { int n,k,s=0,p=0; scanf("%d%d",&n,&k); while(n!=0) { p = n; while(p>9)p/=10; s=k+p; int x = 1; while(x<n)x*=10; x/=10; n%=x; printf("%d",s); } return 0; } the output is only coming for the non-zero values. for ex: input 90100 9 output 1891099 but for this above program we have got the output as 1810...can anyone correct the logic...and explain it
15th Jun 2019, 6:04 AM
Nikash
+ 1
ok, i can make u it using array
15th Jun 2019, 10:36 AM
nicolas turek
nicolas turek - avatar
+ 1
anything needed explain?
15th Jun 2019, 11:26 AM
nicolas turek
nicolas turek - avatar