Example Of Code
int number = 12345;
int sum = 15;
How do I print the below sentence taking into account that the number can be dynamically chosen by the user using a scanf function and I already have a dynamic function that calculates the sum of each digit.
"1 2 3 4 5 = 15"
Edit: Thank you guys so much for your help!
CodePudding user response:
If the input is coming in as a string, the easiest thing to do is to leave it as a string. Don't ever convert it to an int
. (eg, if you are reading the data with scanf
using %d
, just use %s
instead). If the data is already an integer, the easiest thing to do is (probably) to convert it to a string:
#include <stdio.h>
#include <stdint.h>
int
main(void)
{
int number = 12345;
char buf[128];
int sum = 0;
snprintf(buf, sizeof buf, "%d", number);
for( char *s = buf; *s; s ){
if( s != buf ){
fputs(" ", stdout);
}
putchar(*s);
sum = *s - '0';
}
printf(" = %d\n", sum);
}
CodePudding user response:
Easiest by far (and probably fastest too actually) is to stick with strings. Here's an example, error handling yet to be implemented:
#include <stdio.h>
int main (void)
{
char str[128];
scanf("%s", str);
int sum = 0;
for(int i=0; str[i]!='\0'; i )
{
sum = str[i] - '0'; // convert to integer digit, then add to sum
printf("%c ", str[i]);
if( str[i 1] == '\0' )
printf("= ");
else
printf(" ");
}
printf("%d", sum);
}
CodePudding user response:
You can try something like this.
#include <stdio.h>
#define MAX 100
void printIndiviualDigits(int number)
{
int arr[MAX];
int i = 0;
int j, r,sum=0;
// Till number becomes 0
while (number != 0) {
// Extract the last digit of number
r = number % 10;
// Put the digit in arr[]
arr[i] = r;
i ;
// Update number to number/10 to extract
// next last digit
number = number/ 10;
}
// Print the digit of number by traversing
// arr[] reverse
for (j = i - 1; j > -1; j--) {
printf("%d", arr[j]);
sum = arr[j];
if(j > 0){
printf(" ");
}
}
printf("=%d",sum);
}
// main function
int main()
{
int number;
printf("Enter any number:\n");
scanf("%d",&number);
printIndiviualDigits(number);
return 0;
}
CodePudding user response:
This is not the most efficient but it's a charmingly recursive solution.
The challenge isn't totally digits. The challenge is outputting them in decreasing order of significance unless you're always provided with the number as a character string!
Expected Output:
15 -> 1 5 = 6
12345 -> 1 2 3 4 5 = 15
123456 -> 1 2 3 4 5 6 = 21
49 -> 4 9 = 13
-78 -> 7 8 = 15
9 -> 9 = 9
-9 -> 9 = 9
10 -> 1 0 = 1
-10 -> 1 0 = 1
#include <iostream>
int sum(int n){
if(n==0){
return 0;
}
int r=sum(n/10);
int d=n;
if(d<0){
d=-d;
}
r =d;
if(n>=10 || n<=-10){
printf(" ");
}
printf("%d",d);
return r;
}
void show(int n){
printf("%d -> ",n);
int r=sum(n);
printf(" = %d\n",r);
}
int main() {
show(15);
show(12345);
show(123456);
show(49);
show(-78);
show(9);
show(-9);
show(10);
show(-10);
return 0;
}
int main() {
show(15);
show(123456);
show(49);
show(-78);
show(9);
show(-9);
show(10);
show(-10);
return 0;
}
CodePudding user response:
Simply recursion can be used to print the first digits when the value is 10 or more, along with the " "
.
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
// Print each digit with a trailing " "
static int sum_helper(int x) {
int sum = 0;
if (x >= 10) {
sum = sum_helper(x / 10);
x %= 10;
}
printf("%d ", x);
return sum x;
}
void sum_line(int x) {
printf("d: ", x);
int last_digit = abs(x % 10);
int sum = last_digit;
int first_digits = abs(x / 10);
if (first_digits) {
sum = sum_helper(first_digits);
}
printf("%d = %d\n", last_digit, sum);
}
Test code
int main(int argc, char **argv) {
sum_line(123);
sum_line(12345);
sum_line(0);
sum_line(1);
sum_line(-12345);
sum_line(INT_MAX);
sum_line(INT_MIN);
return 0;
}
Output
123: 1 2 3 = 6
12345: 1 2 3 4 5 = 15
0: 0 = 0
1: 1 = 1
-12345: 1 2 3 4 5 = 15
2147483647: 2 1 4 7 4 8 3 6 4 7 = 46
-2147483648: 2 1 4 7 4 8 3 6 4 8 = 47