Computer Science, asked by kgayathri10306, 1 year ago

in c program if the input is 123456 the output must be 214365. how to program it?

Answers

Answered by sswaraj04
0

Answer:

#include <stdio.h>

#include<math.h>

int main()

{

   int n;

   printf("Enter the number");

   scanf("%d",&n);

   int new=0,p=0;

   while(n>10)

   {

       int prev=n%10;  

       n/=10;

       new+=(n%10)*pow(10,p);

       p++;

       new+=prev*pow(10,p);

       p++;

       n/=10;

   }

   new+=n*pow(10,p);

   printf("New number is %d",new);

   return 0;

}

Explanation:

it will give result as

Input->123456

output->214365

Input->12345

Output->13254

Now logic of the program,

in each iteration we will exchange last two digit of original number and add to the new number

and we have to add to original number to left not to right

so,for 123456

we will store 6 to a variable and now take 5 and add to new no.

and then add 6 to left to it

and for that we will use power of 10

like 5+6*10^1=56

now in next iteration we will first have 3 to add to new no.

and again power of 10 like 56+3*10^2=356

so power should be increased by 1 each time... p++;

and we will continue till all are done

Try to dry run for better understanding

Hope it helps :-)

Similar questions