Docsity

Loop Statements, Exercises of Law

Toggle sidebar
bg1
Page 1 of 4
Lab: Loop Statements
1. Sum of Odd Numbers
Write a program that prints the next n odd numbers (starting from 1) and on the last row prints the sum of them.
Input
On the first line, you will receive a number – n. This number shows how many odd numbers you should print.
Output
Print the next n odd numbers, starting from 1, separated by new lines. On the last line, print the sum of these
numbers.
Constraints
n will be in the interval [1…100]
Examples
Input Output Input Output
5 1
3
5
7
9
Sum: 25
3 1
3
5
Sum: 9
Hints
2. Multiplication Table
You will receive an integer as an input from the console. Print the 10 times table for this integer. See the examples
below for more information.
Output
Print every row of the table in the following format:
{theInteger} X {times} = {product}
Constraints
pf3
pf4

Partial preview of the text

Download Loop Statements and more Exercises Law in PDF only on Docsity!

Lab: Loop Statements

1. Sum of Odd Numbers

Write a program that prints the next n odd numbers (starting from 1) and on the last row prints the sum of them.

Input

On the first line, you will receive a number – n. This number shows how many odd numbers you should print.

Output

Print the next n odd numbers, starting from 1 , separated by new lines. On the last line, print the sum of these numbers.

Constraints

n will be in the interval [1…100]

Examples

Input Output Input Output 5 1 3 5 7 9 Sum: 25

Sum: 9

Hints

2. Multiplication Table

You will receive an integer as an input from the console. Print the 10 times table for this integer. See the examples below for more information.

Output

Print every row of the table in the following format: {theInteger} X {times} = {product}

Constraints

 The integer will be in the interval [1…100]

Examples

Input Output Input Output 5 5 X 1 = 5 5 X 2 = 10 5 X 3 = 15 5 X 4 = 20 5 X 5 = 25 5 X 6 = 30 5 X 7 = 35 5 X 8 = 40 5 X 9 = 45 5 X 10 = 50

2 2 X 1 = 2
2 X 2 = 4
2 X 3 = 6
2 X 4 = 8
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 =

Hints

3. Multiplication Table 2. Rewrite you program so it can receive the multiplier from the console. Print the table from the given multiplier to 10. If the given multiplier is more than 10 - print only one row with the integer , the given multiplier and the product. See the examples below for more information.

Output

Print every row of the table in the following format: {theInteger} X {times} = {product}

Constraints

 The integer will be in the interval [1…100]

Examples

Input Output Input Output Input Output 5 1

5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 =
2 X 5 = 10
2 X 6 = 12
2 X 7 = 14
2 X 8 = 16
2 X 9 = 18
2 X 10 =
2 X 14

Hints

This code uses two nested for loops to print the desired pattern. The outer loop iterates through each row of the triangle, while the inner loop prints the appropriate number for that row. Since we want to print the same number for each row, we simply use the outer loop index variable i for the number. The Console.Write(i) statement prints the current number without moving to a new line, while Console.WriteLine() moves to a new line after all numbers have been printed for that row.

Solution