1. Write a loop that will read in a series of even numbers from the console (suc
ID: 3834549 • Letter: 1
Question
1. Write a loop that will read in a series of even numbers from the console (such as 2, -4, 8, 6) and compute the total of the numbers in the sequence. The series is ended with a sentinel value that is any odd number. Print the result for your user.
2. Write a FOR loop that will calculate and return the sum of all the odd numbers in [0, 1000]. Print the result after the loop is finished.
3. Write a loop that will generate and display 10 random numbers in [1, 50]. Output all of the randomly generated numbers.
4. Write a nested loop that asks the user for symbol (store in a char) and a number. The loop should output a rectangle (or square) of the character that is of the height and width of the number. E.g., if I input 4 and *, I would get:
****
****
****
****
(C# Language)
Explanation / Answer
1.
using System;
public class Test
{
public static void Main()
{
int num;
int sum = 0;
Console.WriteLine("Enter even numbers to add .Enter odd number to stop");
do
{
num = Convert.ToInt32(Console.ReadLine());
if(num % 2 != 0)
break;
else
sum = sum+ num;
}while(num % 2 ==0);
Console.WriteLine(" The sum of even numbers = "+ sum);
}
}
output:
Enter even numbers to add .Enter odd number to stop
2
-4
8
6
7
The sum of even numbers = 12
2.
using System;
public class Test
{
public static void Main()
{
int sum = 0;
for(int i=0;i<=1000;i++)
{
if(i%2 != 0)
sum = sum+i;
}
Console.WriteLine(" The sum of odd numbers between 0 and 1000 = "+ sum);
}
}
Output:
The sum of odd numbers between 0 and 1000 = 250000
3.
using System;
public class Test
{
public static void Main()
{
int num;
Random r = new Random();
for(int i=0;i<=10;i++)
{
num = r.Next(1,50);
Console.WriteLine(num);
}
}
}
Output:
16
13
25
41
38
45
3
38
34
31
22
4.
using System;
public class Test
{
public static void Main()
{
Console.WriteLine("Enter the symbol and number");
char symbol = Convert.ToChar(Console.ReadLine());
int num = Convert.ToInt32(Console.ReadLine());
for(int i=0;i<num;i++)
{
for(int j=0;j<num;j++)
{
Console.Write(symbol);
}
Console.WriteLine();
}
}
}
output:
Enter the symbol and number
* 4
****
****
****
****
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.