210k views
2 votes
write a program in C# that reads a set of integers and then finds and prints the sum of the even and odd integers?​

User Masebase
by
3.6k points

1 Answer

7 votes

Answer:

The program in C# is as follows:

using System;

class Prog {

static void Main() {

int n;

Console.WriteLine("Number of integers: ");

n = Convert.ToInt32(Console.ReadLine());

int evensum = 0, oddsum = 0; int inpt;

for(int i = 0;i<n;i++){

inpt = Convert.ToInt32(Console.ReadLine());

if(inpt%2==0){ evensum+=inpt; }

else{ oddsum+=inpt; }

}

Console.WriteLine(evensum);

Console.WriteLine(oddsum);

}

}

Step-by-step explanation:

This declares n, the number of integer inputs

int n;

This prompts for n

Console.WriteLine("Number of integers: ");

This gets input for n

n = Convert.ToInt32(Console.ReadLine());

This declares and initializes the even and odd sum.

int evensum = 0, oddsum = 0; int inpt;

This iterates through n

for(int i = 0;i<n;i++){

This gets each input

inpt = Convert.ToInt32(Console.ReadLine());

Check for even numbers and add,if even

if(inpt%2==0){ evensum+=inpt; }

Otherwise, add input as odd

else{ oddsum+=inpt; }

}

Print even sum

Console.WriteLine(evensum);

Print odd sum

Console.WriteLine(oddsum);

}

User Gluxon
by
3.4k points