Using a For Loopin C# : Add Up The Odds or Evens Create an array of 6 or more in
ID: 3783808 • Letter: U
Question
Using a For Loopin C# : Add Up The Odds or Evens
Create an array of 6 or more integers. Then, you will prompt the user if they would like to see the sum of the even numbers or odd numbers from the array.
For this question you do NOT have to ask the user for the values inside of the array, these can be hardcoded.
Use a FOR loop to cycle through the array and find the correct sum that the user wants. *Hint- If you need help finding if it is odd or even, review the Modulus operator.
User Inputs:
o Prompt the user to either see the sum of the odd or even numbers.
Result To Print Out:
o “The odd numbers add up to X” or “The even numbers add up to X”
Explanation / Answer
#include <stdio.h>
void main()
{
int array[100], i, num;
printf("Enter the size of an array ");
scanf("%d", &num);
printf("Enter the elements of the array ");
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("Even numbers in the array are - ");
for (i = 0; i < num; i++)
{
if (array[i] % 2 == 0)
{
printf("%d ", array[i]);
}
}
printf(" Odd numbers in the array are -");
for (i = 0; i < num; i++)
{
if (array[i] % 2 != 0)
{
printf("%d ", array[i]);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.