0

I need to give input twice to get output, where do i need to fix the code?I need to find the count and sum of given number 1 inp

#include <stdio.h> int main() { long long n; int j,count = 0,sum=0; printf("Enter Value: "); scanf("%lld %d", &n,&j); while (n != 0) { n /= 10; ++count; sum+=j%10; j=j/10; } printf("Count: %d sum: %d", count,sum); }

3rd Jul 2021, 5:40 PM
Siyam
Siyam - avatar
5 Réponses
0
Give me input example and expected output for testing.
3rd Jul 2021, 5:58 PM
Ipang
0
Ipang Sample input input: 12 output: Count:2, Sum:3 Input:2673 Output: Count:4, Sum:18
3rd Jul 2021, 6:03 PM
Siyam
Siyam - avatar
0
Why input only one number? I see the code reads value for <n> and <j>.
3rd Jul 2021, 6:13 PM
Ipang
0
Do you want to write code to take single input and work on it for same result? If yes, then You can take single input n and copy to j. scanf("%lld",&n); long long j=n; But you can do same with single variable. No need of j, Think about it.. hope it helps...
3rd Jul 2021, 7:58 PM
Jayakrishna 🇮🇳
0
I get it now, you want to have such output with only one input number. Before I didn't understand cause your thread title wasn't complete. It is possible ... #include <stdio.h> int main() { long long n, sum = 0, count = 0; printf( "Enter Value:\n" ); scanf( "%lld", &n ); while ( n ) { ++count; sum += n % 10; n /= 10; } printf( "Count: %lld sum: %lld", count, sum ); } You can also take input as string, in which you can escape data type range limits, and can count and sum freely.
4th Jul 2021, 12:08 AM
Ipang