C Program to Condense a Number
Condense a Number
In the following example, we will condense a number (8654) into a single digit (5).
i.e., 8654 = 8 + 6 + 5 + 4
8654 = 23
8654 = 2 + 3
8654 = 5 (single digit)
Example
C Compiler
#include <stdio.h>
int copyNum, result, balance;
int myCondense(int a)
{
copyNum = a;
balance = 0;
result = 0;
while(copyNum != 0)
{
balance = copyNum % 10;
result += balance;
copyNum = copyNum / 10;
}
return result;
}
int main()
{
int num = 8654;
int userValue = num;
while(num > 9)
num = myCondense(num);
printf("Single Digit of %d: %d", userValue, num);
return 0;
}
Output
Single Digit of 8654: 5
Condense any Given Number
In the following example, we will condense any given number into a single digit.
Example
C Compiler
#include <stdio.h>
int copyNum, result, balance;
int myCondense(int a)
{
copyNum = a;
balance = 0;
result = 0;
while(copyNum != 0)
{
balance = copyNum % 10;
result += balance;
copyNum = copyNum / 10;
}
return result;
}
int main()
{
int num;
printf("Enter any Multi digit (int) number: ");
scanf("%d", &num);
int userValue = num;
while(num > 9)
num = myCondense(num);
printf("Single Digit of %d: %d", userValue, num);
return 0;
}
Output
Enter any Multi digit (int) number: 147
Single Digit of 147: 3
Reminder
Hi Developers, we almost covered 98% of String functions and Interview Question on C with examples for quick and easy learning.
We are working to cover every Single Concept in C.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.