Problem: C program for "If a five digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits. For example, if the number that is input is 12391, then the output should be displayed as 23502."
Logic used :
- Check if the number is not equal to zero
- Take remainder out using modulus with 10 and store it in a variable
- Increase remainder variable by 1
- Use formulae : output = ( output x 10 ) + remainder
- Take quotient out by dividing it by 10 and store it in the num variable itself
- Repeat above steps until num value becomes zero
- Set num = 0
- check if output is not equal to zero
- Take remainder out from output using modulus operator with 10
- Use formulae : num = ( num x 10 ) + remainder
- Take the quotient out from output by dividing it by 10 and store in output variable itself
- Repeat above steps after num = 0, until output becomes zero
- Print the num
Demo :
while(num!=0){
remainder = num%10;
remainder++;
output=(output*10)+remainder;
num = num/10;
}
num = 0;
while(output!=0){
remainder = output%10;
num=(num*10)+remainder;
output = output/10;
}
C program :
// Program by Shyam Kanth#include<stdio.h>int main(){int num, output=0, remainder;printf("Enter the five digit number : ");scanf("%d", &num);while(num!=0){remainder = num%10;remainder++;output=(output*10)+remainder;num = num/10;}num = 0;while(output!=0){remainder = output%10;num=(num*10)+remainder;output = output/10;}printf("Increased number is %d.",num);return 0;}
- #include < stdio.h > : Header file for input-output
- int : Integer data type
- num, output, remainder : Variable used
- printf : Function used for displaying message
- scanf : Function used for taking input
- while : Loop
- != : Not equal to
- %d : Format specifier for integer type data
Do comments, if any doubt
Know about me : Shyam Kanth
No comments:
Post a Comment