Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

c program to add 1 to each digit of a number

#include <stdio.h>

int main(void)
{
    int num, sum, remainder, check; // check used as a boolean expression
    sum = check = 0;

    printf("Enter the sequence: ");
    scanf("%d", &num);

    while (num > 0)
    {
        remainder = num % 10; // each time num is reduced

        if (remainder != 9)
        {
            if (check == 0)
                sum = (10 * sum) + (remainder + 1);
            else
            {
                sum = (10 * sum) + (remainder + 2);
                check = 0;
            }
        }
        else
        {
            sum = (10 * sum) + 0;
            check = 1;
        }
        num /= 10; // will divide and execute in each iteration until it's true
    }

    num = sum; // final number will be equal to the sum
    sum = 0;

    // Summing up the results
    while (num > 0)
    {
        remainder = num % 10;
        sum = (10 * sum) + remainder;
        num /= 10;
    }

    printf("Result: %d
", sum);

    return 0;
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #program #add #digit #number
ADD COMMENT
Topic
Name
2+5 =