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.
Lets 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 cant be a negative number. | Enter a Number in Range [2 to N]: 17No Factor for the Number 17Total Factors are: 0Sum of Factors is: 0 |
Example 3 | Example 4 |
Enter a Number in Range [2 to N]:0Error! Input cant be zero. | Enter a Number in Range [2 to N]:1Error! Input cant be one. |
Example 5 | Example 6 |
Enter a Number in Range [2 to N]: 12The Factor(s) of 12 are: 2 3 4 6Total Factors are: 4Sum of Factors is: 15 | Enter a Number in Range [2 to N]:2No Factor for the Number 2Total Factors are: 0Sum 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.
- How many positive numbers are entered?
- How many negative numbers are entered?
- How many zeros are entered?
- Sum of positive numbers
- Sum of negative numbers
- 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: -999Total Positive Numbers are: 5Total Negative Numbers are: 3Total Zeros are: 2Sum of Positive Numbers is: 15Sum of Negative Numbers is: -6Average of Positive Numbers is: 3 | Enter Numbers! Enter -999 to Stop:-999Total Positive Numbers are: 0Total Negative Numbers are: 0Total Zeros are: 0Sum of Positive Numbers is: 0Sum of Negative Numbers is: 0Average of Positive Numbers is: 0 |
Example 3 | Example 4 |
Enter Numbers! Enter -999 to Stop: Total Positive Numbers are: 0Total Negative Numbers are: 7Total Zeros are: 2Sum of Positive Numbers is: 0Sum of Negative Numbers is: -17Average of Positive Numbers is: 0 | Enter Numbers! Enter -999 to Stop: Total Positive Numbers are: 4Total Negative Numbers are: 3Total Zeros are: 2Sum of Positive Numbers is: 10Sum of Negative Numbers is: -7Average of Positive Numbers is: 2.5 |
Q-3. (to be marked)
Write a program to produce a square matrix with 0s down the main diagonal, 1s in the entries just above and below the main diagonal, 2s above and below that, etc.
- 1 2 3 4
- 0 1 2 3
- 1 0 1 2
- 2 1 0 1
- 3 2 1 0
Example 1 | Example 2 |
Enter the number of rows: 50 1 2 3 41 0 1 2 32 1 0 1 23 2 1 0 14 3 2 1 0 | Enter the number of rows: 80 1 2 3 4 5 6 71 0 1 2 3 4 5 62 1 0 1 2 3 4 53 2 1 0 1 2 3 44 3 2 1 0 1 2 35 4 3 2 1 0 1 26 5 4 3 2 1 0 1 7 6 5 4 3 2 1 0 |
Example 3 | Example 4 |
Enter the number of rows: 0Please enter positive integer. | Enter the number of rows: 30 1 21 0 12 1 0 |
Reviews
There are no reviews yet.