How to Understand Basics of C++ Via Simple Program

Vu Old Solved Programming Based Assignments Codes are Available in this Section.
Post Reply
User avatar
ALI RAZA
★★★★★★★★★★★★★
★★★★★★★★★★★★★
Posts: 177
Joined: Fri Dec 22, 2017 8:10 am
Location: Lahore
Has thanked: 6 times
Been thanked: 7 times
Contact:
Status: Offline

How to Understand Basics of C++ Via Simple Program

Post by ALI RAZA » Sat Sep 29, 2018 9:43 am

Write a Program which allows the user to input an integer value for a variable name Limit. Based on the input value, the program should perform the following tasks:

• Check whether the value entered by the user falls within the range from 10 to 150. (10 and 150 included in the given range.)
• Display those numbers which are divisible by both 3 and 5 in the range from 1 up to the Limit.
• Calculate and display the sum of those numbers which are divisible by either 3 or 5.
• Final task will be to count and display those numbers which are not divisible by 3 or 5.



For example if user enters Limit=21
Numbers which is divisible by both 3 and 5 is = 15
Numbers which are divisible by either 3 or 5= 3,5,6,9,10,12,18,20,21 (as 15 divisible by both 3 and 5, so it is not included)
Sum will be = 3+5+6+9+10+12+18+20+21= 104
Answer:

Code: Select all

#include<iostream>
using namespace std;

int main() {
	
	int limit =0;
	int count = 0;
	int sum = 0;
	int start = 1;
	
	
	cout << "Enter the limit in the range <10..150>:";
	cin >> limit;
	
	if(limit <10){
	
		cout << "Wrong input!! Limit should not be less than 10" << endl;
	
	} else if(limit > 150) {
	
			cout <<"Wrong input!! Limit should not be greater than 150" << endl;
	
	} else {
		
			cout << endl;
			cout << "Numbers multiples of both 3 and 5: ";
			while (start<=limit){
			
				if(start % 3==0 && start % 5== 0){ 
					cout << start << " ";
				} else if (start % 3 == 0 || start % 5 == 0) {
					sum = sum + start;
				} else {
					count++;
				}
				
			  start++;
			
			}
		
			cout << endl;
			cout << "Calculated Sum: " << sum << endl;
			cout << "Total numbers which are not multiples of 3 or 5: " << count << endl;
		}
	
	system("pause");

}



Post Reply

Last 10 Members Who Visited This Topic

User avatar BILAL KHAN (1)

  • Similar Topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: No registered users and 1 guest