C Program to Increment by 1 to all the Digits of a Given Integer

This is a C program to Increase 1 to all of the given Integer Digit.

Problem Description

This program increases 1 to all of the given integer digit and print the sum of all digits.

Problem Solution

1. Take a integer as input.
2. Obtain its remainder and quotient.
3. Increment the remainder by 1 and add to the another variable.
4. Repeat the steps 2 & 3 with the quotient obtained until the quotient becomes zero.

Program/Source Code

Here is source code of the C Program to Increase 1 to all of the given Integer Digit and print the sum of all digits. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1.  
  2. /*
  3.  * C program to Increase 1 to all of the given Integer Digit
  4.  */
  5. #include <stdio.h>
  6.  
  7. int main()
  8. {
  9.     int number, sum = 0, remainder, count;
  10.  
  11.     printf("Enter a number: ");
  12.     scanf("%d", &number);
  13.     while (number)
  14.     {
  15.         remainder = number % 10;
  16.         sum  = sum + (remainder + 1);
  17.         number /= 10;
  18.     }
  19.     printf("increasing 1 to all digits:  %d", sum);
  20.     return 0;
  21. }
Program Explanation

1. Initialize variable sum to zero.
2. Take a number as input and store it in the variable number.
3. Obtain the remainder and quotient of the variable number. Store the remainder in the variable remainder and override the variable number by quotient.
4. Increment the variable remainder by 1 and add it to the variable sum.
5. Repeat the step 3 & 4 with the quotient obtained until it becomes zero.
6. When it becomes zero, print the variable sum as output and exit.

advertisement
advertisement
Runtime Test Cases
Enter a number: 3456
increasing 1 to all digits:  22

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.