mcveigh
Diamond Member
I have taken a simple C (not C++) class before. now for a new class I have a really simple first program, almost heloo world simple.
I have to take a 5 digit input number then add it to gether that's it.
EX: 12345 is the input the output would be 1+2+3+4+5=15
I originally wrote a program that did that but I thought the input was 5 separate integers, pressing enter after each one.
how can I read a string as separate integers? I'm thinking using getchar() is that right or am I way off?
here's what i wrote so far
#include <stdio.h>
int main()
{
int a,b,c, sum = 0;
printf("\nPlease enter three integers to be added together.\n");
scanf ("%d",&a);
scanf ("%d",&b);
scanf ("%d",&c);
sum = a + b + c;
printf("\n\n\nThe sum of your integers is %d\n", sum);
return 0;
}
I have to take a 5 digit input number then add it to gether that's it.
EX: 12345 is the input the output would be 1+2+3+4+5=15
I originally wrote a program that did that but I thought the input was 5 separate integers, pressing enter after each one.
how can I read a string as separate integers? I'm thinking using getchar() is that right or am I way off?
here's what i wrote so far
#include <stdio.h>
int main()
{
int a,b,c, sum = 0;
printf("\nPlease enter three integers to be added together.\n");
scanf ("%d",&a);
scanf ("%d",&b);
scanf ("%d",&c);
sum = a + b + c;
printf("\n\n\nThe sum of your integers is %d\n", sum);
return 0;
}