I'm trying to make a small program, where user enters a list of numbers and at the end it shows how much in % of the numbers are odd or even.
If the user enters no value (just presses enter) it stops request a number and shows the calculation, but if the user enters exit
it exits. Additionally I want to check if the entered number is a whole number.
Here what I got so far:
#include <stdio.h>
int main(void)
{
int input, all [100], even[100], odd[100], even_counter, even_print, odd_counter, odd_print, total_counter, total_print, even_p, odd_p;
char cinput;
for (;;)
{
printf("Enter a number: ");
if (scanf("%d", &cinput) != 1) break;
input=(int)(cinput-0);
all[total_counter]=input; //Insert number in all numbers array
if (input % 2 == 0)
{
even[even_counter]=input; //Insert number in even numbers array
even_counter++;
total_counter++;
}
if (input % 2 != 0)
{
odd[odd_counter]=input; //Insert number in odd numbers array
odd_counter++;
total_counter++;
}
}
printf("\n You entered the following numbers: ");
for (total_print=0; total_print<total_counter; total_print++)
{
printf("%d ", all[total_print]); //Show all entered numbers
}
printf("\n Even numbers are: ");
for (even_print=0; even_print<even_counter; even_print++)
{
printf("%d ", even[even_print]); //Show even numbers only
}
printf("\n Odd numbers are: ");
for (odd_print=0; odd_print<odd_counter; odd_print++)
{
printf("%d ", odd[odd_print]); //show odd numbers only
}
printf("\n\n Total numbers entered: %d", total_counter); //Show count of all numbers
printf("\n Even numbers entered: %d", even_counter); //Show count of even numbers
printf("\n Odd numbers entered: %d", odd_counter); //Show count of odd numbers
even_p=(even_counter/total_counter)*100; //Calculate % of even numbers
odd_p=(odd_counter/total_counter)*100; //Calculate % of odd numbers
printf("\n\n Even numbers are %d %% of total number (%d out of %d)", even_p, even_counter, total_counter);
printf("\n Odd numbers are %d %% of total number (%d out of %d)", odd_p, odd_counter, total_counter);
}
What works:
- It saves all entered numbers in one array, even and odd numbers in separate arrays.
- It counts how many numbers were entered, how many are even and how many are odd.
- As soon as a non-numeric character is entered it goes to showing the result.
What doesn't work:
- The % calculation always says that it's 0%.
- The first number in the odd array is 0.
What I can't figure out:
- How to make it go to the results if nothing is entered? Tried
if (scanf("%d", &cinput) != "\n") break;
, but it just got stuck in the loop request another number. - How to make the program exit if
exit
is entered? - How to check if a whole number is entered?
EDIT
Counters initialized. scanf("%d", &cinput) changed to scanf("%d", &input). Changed even_p and odd_p to float (from int). Now the calculation is working fine and the first value in odd_array is good to.
#include <stdio.h>
int main(void)
{
int input, all [100], even[100], odd[100], even_counter, even_print, odd_counter, odd_print, total_counter, total_print;
even_counter=0;
even_print=0;
odd_counter=0;
odd_print=0;
total_counter=0;
total_print=0;
for (;;)
{
printf("Enter a number: ");
if (scanf("%d", &input) != 1) break;
all[total_counter]=input; //Insert number in all numbers array
if (input % 2 == 0)
{
even[even_counter]=input; //Insert number in even numbers array
even_counter++;
total_counter++;
}
if (input % 2 != 0)
{
odd[odd_counter]=input; //Insert number in odd numbers array
odd_counter++;
total_counter++;
}
}
printf("\n You entered the following numbers: ");
for (total_print=0; total_print<total_counter; total_print++)
{
printf("%d ", all[total_print]); //Show all entered numbers
}
printf("\n Even numbers are: ");
for (even_print=0; even_print<even_counter; even_print++)
{
printf("%d ", even[even_print]); //Show even numbers only
}
printf("\n Odd numbers are: ");
for (odd_print=0; odd_print<odd_counter; odd_print++)
{
printf("%d ", odd[odd_print]); //show odd numbers only
}
printf("\n\n Total numbers entered: %d", total_counter); //Show count of all numbers
printf("\n Even numbers entered: %d", even_counter); //Show count of even numbers
printf("\n Odd numbers entered: %d", odd_counter); //Show count of odd numbers
float even_p=((float)(even_counter)/total_counter)*100; //Calculate % of even numbers
float odd_p=((float)(odd_counter)/total_counter)*100; //Calculate % of odd numbers
printf("\n\n Even numbers are %f %% of total number (%d out of %d)", even_p, even_counter, total_counter);
printf("\n Odd numbers are %f %% of total number (%d out of %d)", odd_p, odd_counter, total_counter);
}
Now I just need the input to be checked if it's a whole number (display a message, but continue to ask for numbers), go to calculation if no value is entered, quit if exit
is entered. And I forgot - the user should not be able to go to calculation if he enters less than 10 numbers. :(