Write a program that accepts a 7-9
digit integer and echoes the number with commas
between every three digits from the right. Test it with the
numbers 1234567, 987654321, 20300045, & 10000000.
this is the program i have so far
#include
get a string, loop over from 1 - strlen(string)-1 and print each character with a comma..
Since you're outputting it with commas, it has to be a string, it can no longer be an int. Think about the problem working it as a string.
since it's seven to nine characters you can use integers too.You just have to extract their digits
well this what my teacher showed me then he said that i have to add four more lines to the middle3 and last3; i had it once but i forget it
please can someone help me with this it is already late
#include <cstdio> #include <stack> using namespace std; stack<int> S; void print(long long a) { while(a>0) { S.push(a%10); a/=10; } printf("%d",S.top()); while(!S.empty()) { printf(",%d",S.top(); s.pop(); } } int main(void) { long long a; scanf("%lld",&a); print(a); return 0; } I didn't test it, i hope it works
i am not supposed to use a while loop
why?
ah, every three digits :P i thought it would be every digit.. my code needs a small correction then.
because while we were learning this program my teacher didnt teach us any of the looping statements
fight the power :P
Join our real-time social learning platform and learn together with your friends!