C Program That in a Given Natural Number x Insert Figure c at Position p

How to Write a program that in a given natural number x insert figure c at position p . The numbers x , c and p are entered from standard input . 


For example , x = 140 , c = 2 , p = 2 , the result is the 1420th


Solution:
  1. #include<stdio.h>
  2. #include<math.h>

  3. int main() {
  4.         int x,c,p,i,y,d;
  5.         scanf("%d%d%d", &x,&c,&p);
  6.         printf("Number: %d, figure %d, required position: %d\n", x,c,p);
  7.         i=1;   
  8.         y=10;  
  9.         if(i==p){
  10.                 x=x*10;
  11.                 x=x+c;
  12.                 printf("X: %d\n", x);
  13.         }
  14.         while(i<p){
  15.         i++;
  16.         c=c*10;
  17.                 if(i==p){
  18.                 d=x%y;
  19.                 x=x-d;
  20.                 x=x*y;
  21.                 x=x+c+d;
  22.                 printf("X: %d\n", x);  
  23.                 }
  24.         y=10*y;
  25.         }
  26.                
  27.  
  28.         return 0;
  29. }


Learn More :