Compare the following code you your latest version, and see my comments in the code:
Code:
#include <stdio.h>
#include <stdlib.h>
/* summing the first 1000 numbers of multiples 3 & 5 */
int main (void)
{
//Initialize all local variables
int c = 0;
int n = 0;
int x = 0;
// y & z are not used
// int y, z;
// Prompt the user what to enter
printf("Please enter the upper limit\n");
scanf("%d", &n);
// Check here that a valid value was entered.
for (c=0; c<n; c++)
{
if(c%3 ==0 || c%5==0)
{
printf("%d \n",c);
}
else
{
x+=c;
}
}
// Print a newline at the end
printf("%d\n", x);
return 0;
}
Turn on and turn up your warning level!
From my compile of your latest code:
Code:
sum.c: In function ‘main’:
sum.c:14:11: warning: zero-length gnu_printf format string [-Wformat-zero-length]
printf("");
^~
sum.c:11:11: warning: unused variable ‘z’ [-Wunused-variable]
int y, z;
^
sum.c:11:8: warning: unused variable ‘y’ [-Wunused-variable]
int y, z;
^
Please indent your code consistently, and remove unneeded blank lines. Choose one style and be consistent.
More prompts and explanation of the program purpose, and output are needed.