Replace all 0's with 1 in a given integer




Algorithm to replace all 0's with 1 in a given integer


  • Input the integer from the user.
  • Traverse the integer digit by digit.
  • If a '0' is encountered, replace it by '1'.
  • Print the integer.

replace all 0's with 1 in a given integer

#include<stdio.h>

int main(void )
{  int rem,result=0,n;
   printf("enter the number: ");
   scanf("%d",&n);

   //check digit from last if it is zero or not~

   while(n!=0)
   {
     rem=n%10;
       if(rem==0)
       {
           rem=1;
       }
         result=result*10+rem;
       n=n/10;
   }

//above gives result in reverse so

int rev=0;
while(result!=0)
  {
    int rem1=result%10;
    rev=rev*10+rem1;
    result=result/10;
  }

printf("\nafter replace 0's with 1 == %d",rev);

}


Output:










SHARE

Milan Tomic

Hi. I’m Designer of Blog Magic. I’m CEO/Founder of ThemeXpose. I’m Creative Art Director, Web Designer, UI/UX Designer, Interaction Designer, Industrial Designer, Web Developer, Business Enthusiast, StartUp Enthusiast, Speaker, Writer and Photographer. Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment