I'm writing a program that uses if loops and if logical operators to evaluate whether a number is odd or even, but the compiler keeps bringing up the error in the title - the if statement is there I can see it lol!
/* Program to decide whether the numbers 1 .. n are odd/even */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
double n, r;
int t;
char i [256];
/* User enters a value for n from stdin, and converted and stored as double r */
printf("This is a program will decide whether number 1 .. n are odd or even.\n Enter the value of n :");
gets( i );
n = atof ( i );
/* Iteration to evaluate values between 1 .. n, and print whether they are odd or even*/if ( t = 1, t <= n, t++)
{
printf("NUMBER ODD/EVEN\n");
r = fmod (t,2.0);
/* If the modulus of n / 2 == 0, then it must be even, if the test t==0 evaluates falsly it must be odd*/if ( r == 0.0); {
printf(" %i EVEN\n", n);
} else {
printf(" %i ODD\n", n);
}
}
printf("The program is complete!\n");
}
/* Program to decide whether the numbers 1 .. n are odd/even */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
double n, r;
int t;
char i [256];
/* User enters a value for n from stdin, and converted and stored as double r */
printf("This is a program will decide whether number 1 .. n are odd or even.\n Enter the value of n :");
gets( i );
n = atof ( i );
/* Iteration to evaluate values between 1 .. n, and print whether they are odd or even*/
printf("NUMBER ODD/EVEN\n");
for ( t = 1; t <= n; t++)
{
r = fmod (t,2.0);
/* If the modulus of n / 2 == 0, then it must be even, if the test t==0 evaluates falsly it must be odd*/
if ( r == 0) {
printf(" %i EVEN\n",t);
} else {
printf(" %i ODD\n",t);
}
}
printf("The program is complete!\n");
}
For a heads up, the math class isn't needed. Using modulo by doing something similar to n%2 will return a 1 or 0, 1 meaning it's odd and 0 meaning it's even.
I tried % , I removed it early on in the debugging stage because I thought the problem had something to do with that operation, but it wasn't - so I'll use % in the future.
Firedraco could you explain your post please, I'm very new to C .
This forum is excellent, so many knowledgeable people - I'll be glad when I can start answering questions! Thanks again!