Starting from:

$30

CS2310-Lab 5 Looping Statements Solved

Please test the correctness of your programs in Q-1, Q-2 and Q-3 using PASS.

 

Q-1.

 

Write a program which reads a positive integer n and outputs all the factors of n, total factors and sum of factors. A number i is a factor of n if i divides n, and 1<i<n. 

 

Let’s use for-loop in your program.

 

Hint: Write a for-loop with an integer counter i. In each iteration, check if n is divisible by i (one way to perform such check is to use the modulo operator).

 

Expected Output:

 

Example 1
Example 2
Enter a Number in Range [2 to N]: -10  

Error! Input can't be a negative number.
Enter a Number in Range [2 to N]: 17

No Factor for the Number 17

Total Factors are: 0

Sum of Factors is: 0  
Example 3
Example 4
Enter a Number in Range [2 to N]:

0

Error! Input can't be zero.
Enter a Number in Range [2 to N]:

1

Error! Input can't be one.
Example 5
Example 6
Enter a Number in Range [2 to N]: 12

The Factor(s) of 12 are: 2 3 4 6

Total Factors are: 4

Sum of Factors is: 15  

  
Enter a Number in Range [2 to N]:

2

No Factor for the Number 2

Total Factors are: 0

Sum of Factors is: 0  

 
 

NOTE: Your program MUST follow the EXACT input/output format! Otherwise, you may not pass the test cases even your calculation is correct.

 

 

Q-2.

 

Write a program which reads numbers until -999 is entered and compute the following.

a)     How many positive numbers are entered?

b)     How many negative numbers are entered?

c)     How many zeros are entered?

d)     Sum of positive numbers

e)     Sum of negative numbers

f)      Average of positive numbers

 

Hints:  1) Exclude -999 from all computations.

       2) Use while-loop in your program.

 

 

Expected Output:

 

Example 1
Example 2
Enter Numbers! Enter -999 to Stop:

 

 

 

-999

Total Positive Numbers are: 5

Total Negative Numbers are: 3

Total Zeros are: 2

Sum of Positive Numbers is: 15

Sum of Negative Numbers is: -6

Average of Positive Numbers is: 3  

 
Enter Numbers! Enter -999 to Stop:

-999

Total Positive Numbers are: 0

Total Negative Numbers are: 0

Total Zeros are: 0

Sum of Positive Numbers is: 0

Sum of Negative Numbers is: 0

Average of Positive Numbers is: 0  
Example 3
Example 4
Enter Numbers! Enter -999 to Stop:

 

Total Positive Numbers are: 0

Total Negative Numbers are: 7

Total Zeros are: 2

Sum of Positive Numbers is: 0

Sum of Negative Numbers is: -17

Average of Positive Numbers is: 0

  
Enter Numbers! Enter -999 to Stop:

 

Total Positive Numbers are: 4

Total Negative Numbers are: 3

Total Zeros are: 2

Sum of Positive Numbers is: 10

Sum of Negative Numbers is: -7

Average of Positive Numbers is: 2.5  
 

 

 

 

 

 

 

 

 

 

Q-3. 
 

Write a program to produce a square matrix with 0's down the main diagonal, 1's in the entries just above and below the main diagonal, 2's above and below that, etc.

0  1 2 3 4

1  0 1 2 3

2  1 0 1 2

3  2 1 0 1

4  3 2 1 0

 

 

Example 1
Example 2
Enter the number of rows: 5

0  1 2 3 4

1  0 1 2 3

2  1 0 1 2

3  2 1 0 1

4  3 2 1 0
Enter the number of rows: 8

0        1 2 3 4 5 6 7                             

1        0 1 2 3 4 5 6                             

2        1 0 1 2 3 4 5                             

3        2 1 0 1 2 3 4                             

4        3 2 1 0 1 2 3                             

5        4 3 2 1 0 1 2                             

6        5 4 3 2 1 0 1                              7 6 5 4 3 2 1 0
Example 3
Example 4
Enter the number of rows: 0

Please enter positive integer.
Enter the number of rows: 3

0  1 2

1  0 1

2  1 0

More products