Sunday, 10 December 2017

CS201 - Introduction to Programming -- Assignment No.1 - Fall 2017 - Solution

Leave a Comment
Objectives:
To enable students to understand and practice the concepts of:

  • Variables and operators
  • Expressions in C++
  • Decision structures
  • Repetition structures

Assignment:
Write a program that will
  1. Ask the user to enter lower limit and upper limit in the form of integer numbers.
  2. The program will then add / sum all those numbers between upper limit and lower limit (including the lower and upper limits) which are NOT multiple of 4.
  3. This process should continue for all the remaining integer numbers up until the upper limit is reached.
  4. The program will then show the aggregate sum of all numbers for the given range.

Make sure that lower limit entered by the user should be greater than zero. Also the upper limit value entered by the user should be greater than the lower limit value.

Example:
Lower limit is 1 and Upper limit is 8 then the program will subtract 4 and 8 as these are the multiples of 4. While add remaining integers in the range (1+2+3-4+5+6+7-8 = 12). So, 12 is the calculated number.

Sample output for correct input:
CS201 - Introduction to Programming -- Assignment No.1 - Fall 2017 - Solution

Sample output for the wrong input:
CS201 - Introduction to Programming -- Assignment No.1 - Fall 2017 - Solution

Sample output for the wrong input:
CS201 - Introduction to Programming -- Assignment No.1 - Fall 2017 - Solution

Solution:

#include<iostream>
using namespace std;
main()
{
       int upperlmt, lowerlmt, sum = 0, a;
       cout << "Please Enter Lower Limit <Greater Then 0 >: ";
       cin >> lowerlmt;
       if (lowerlmt > 0)
       {
              cout << "Please Enter Upper Limit <Greater Than Lower Limit>: ";
              cin >> upperlmt;
              if (upperlmt > lowerlmt)
              {
                     for (a = lowerlmt; a <= upperlmt; a++)
                     {
                           if (a % 4 == 0)
                           {
                                  sum = sum - a;
                           }
                           else
                           {
                                  sum = sum + a;
                           }
                     }
                     cout << "Calculated Number is " << sum << endl;
              }
              else
              {
                     cout << "Upper Limit Must Be Greater Than Lower Limit" << endl;
              }
       }
       else
       {
              cout << "Lower Limit Should Be Greater Then Zero" << endl;
       }
       system("pause");
}
If You Enjoyed This, Take 5 Seconds To Share It

0 Questions: