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.
#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:
0 comments:
Post a Comment