Ticker

6/recent/ticker-posts

How do I place the sum of the digits of a number to the left of the original number (ex: n=521 ==> sum =8 ==> n=8521) using loops in C language?

 How do I place the sum of the digits of a number to the left of the original number (ex: n=521 ==> sum =8 ==> n=8521) using loops in C language?


#include <stdio.h>

int main() {

int num,sum=0,i,r=0,num2=0;

printf("enter a number\n");

scanf("%d",&num);

for(i=0;num;num=num/10){

r=num%10;

sum=sum+r;

num2=num2*10+r;

}

num2=num2*10+sum;

for(i=0;num2;num2=num2/10){

r=num2%10;

num=num*10+r;

}

printf("%d",num);

}#include <stdio.h>

int main() {

int num,sum=0,i,r=0,num2=0;

printf("enter a number\n");

scanf("%d",&num);

for(i=0;num;num=num/10){

r=num%10;

sum=sum+r;

num2=num2*10+r;

}

num2=num2*10+sum;

for(i=0;num2;num2=num2/10){

r=num2%10;

num=num*10+r;

}

printf("%d",num);

}

output:-

enter a number

521

8521

కామెంట్‌ను పోస్ట్ చేయండి

0 కామెంట్‌లు