0% found this document useful (0 votes)
8 views5 pages

Worksheet On Nested Loops

Uploaded by

sjurie21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Worksheet On Nested Loops

Uploaded by

sjurie21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ONT1000 / SDS1000

Worksheet on Nested Loops


Name: Surname: Student Number: Subject
Code:

QUESTION 1
Indicate the output that will be displayed to the console.
int i, j;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= i; j++)
{
Console.Write("{0}{1}", i,j);
}
Console.WriteLine();
}

QUESTION 2
Indicate the output that will be displayed to the console.
int i, j, answer=0;

for (i = 1; i <= 2; i++)


{
for (j = 1; j <= 3; j++)
{
answer += i * j;
}
Console.Write("{0} ", answer);
Console.WriteLine();
}

Console.WriteLine();
Console.ReadLine();

Page 1 of 5 Compiled by Yiota Moutzouris


QUESTION 3

int i, j;
for (i = 1; i <= 5; i++)
for (j = 1; j <= i; j++)
{
Console.Write("*");
Console.WriteLine();
}
The above code prints outs the following to the console.

Amend it (rewrite all code) so that it prints out as follows:

QUESTION 4

int i, j;
for (i = 1; i <= 5; i++)
for (j = 1; j <= i; j++)
{
Console.Write("*");
Console.WriteLine();
}
The above code prints outs the following to the console.

Amend it (rewrite all code) so that it prints out as follows:

Page 2 of 5 Compiled by Yiota Moutzouris


QUESTION 5
Write a program that will accept 2 rainfall figures (Jan - Feb) for a number of years.
The user will input the number of years. For each year, the user will enter the rainfall figures for the
2 months. Make use of a Nested For Loop.
Example Input:
Enter no. of years: 3

Enter year: 1986


Enter rainfall for month 1: 0.88
Enter rainfall for month 2 1.11

Enter year: 1987


Enter rainfall for month 1: 0.76
Enter rainfall for month 2: 0.94

Enter year: 1988


Enter rainfall for month 1: 0.82
Enter rainfall for month 2: 0.80

Example Output:
Year: 1986
Rainfall for month 1: 0.88
Rainfall for month 2: 1.11
Average Rainfall for 1986: 0.995

Year: 1987
Rainfall for month 1: 0.76
Rainfall for month 2: 0.94
Average Rainfall for 1987: 0.850

Year: 1988
Rainfall for month 1: 0.82
Rainfall for month 2: 0.80
Average Rainfall for 1988: 0.81

Highest Average Rainfall: 0.995

Page 3 of 5 Compiled by Yiota Moutzouris


QUESTION 6
Convert the code below to a nested FOR loop.
tipRate = 0.10;

while (dinnerPrice <= 100.00)


{
Console.Write("{0,8}", dinnerPrice.ToString("C"));

while (tipRate <= 0.25)


{
tip = dinnerPrice * tipRate;
Console.Write("{0,8}",tip.ToString("c"));
tipRate += 0.05;
}

dinnerPrice += 10.00;
tipRate = 0.10;

Console.WriteLine();
}

QUESTION 7
Identify and correct the EIGHT errors in this code so it works as shown.

static void Main(string[] args)


{
string studentName;
int testMark, sum = 0;
double average;

for (row = 1; row <= 3; row++)


{
Console.Write("Enter student name: ");
studentName = Console.ReadLine();

for (col = 1; col <= 2; col++)


{
Console.Write("Enter test mark:", col);
mark = int.Parse(Console.ReadLine());

sum = testMark;
}

average = sum / 3;
Console.WriteLine("Average: {0}");
Console.WriteLine();
}

Console.ReadLine();
}

Page 4 of 5 Compiled by Yiota Moutzouris


QUESTION 8

Trace through. Write down what will be printed out.

string[] soccerPlayer = { "Ryan", "David" };


int[] team1 = { 1, 2, 1, 1 };
int[] team2 = { 3, 1, 0, 5 };
int lowest = 99;

for (int i = 0; i < soccerPlayer.Length; i++)


{
lowest = Math.Min(lowest, team1[i]);
lowest = Math.Min(lowest, team2[i]);

Console.WriteLine("{0} {1}", soccerPlayer[i], lowest);


}

QUESTION 9

Trace through. Write down what will be printed out.

for (int row = 3; row >= 0; row--)


if ((row == 0) || (row == 2))
{
for (int n = 0; n < 2; n++)
Console.Write("11");
Console.WriteLine();
}
else
{
for (int n = 0; n < 2; n++)
Console.Write("##");

Console.Write('*');
Console.WriteLine();
}

Page 5 of 5 Compiled by Yiota Moutzouris

You might also like