Add code (below) so that the program will ask the user how many whole odd number
ID: 3830587 • Letter: A
Question
Add code (below) so that the program will ask the user how many whole odd numbers to sum up, and will then allocate an array, and fill it with that many odd numbers, counting down. The final space in the array (the one with the highest-numbered index) should have the value 1, the one prior to that should have the value 3, etc, etc. So if the user asks for 4 numbers, you should fill in the array with 7, 5, 3, and 1 (where 7 is in slot 0, and 1 is in slot 4). You must print out the sum of all the numbers that are in the array, which you must get from actually adding up the numbers that are in the array. You must make use of the provided code, as much as possible. static void Main (string[] args) {Console.WriteLine ("How many odd number would you like to sum up?"); Int32.TryParse (Console.ReadLine(), out n);//we'll assume input is an integer int sum;Explanation / Answer
using System.IO;
using System;
class Program
{
static void Main()
{
Console.WriteLine("How many odd numbers you would like to sum up"); //taking number of data from user
int n = Convert.ToInt32(Console.ReadLine());
int[] a = new int [n];
int c=1;
int i;
int g=n-1;
int sum=0;
for(i=0;i<n;i++) //inserting odd number s into the array
{
a[g--]=c;
c=c+2;
}
for(i=0;i<n;i++) //calculating the sum
{
sum=sum+a[i];
}
Console.WriteLine("Elemnts are "); //printing the element of the array
for(i=0;i<n;i++)
{
Console.WriteLine(a[i]);
}
Console.WriteLine("The sum is {0} ",sum); //printing the sum
Console.ReadKey();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.