Looping Statements Activity #1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

1.

Write a program in C++ that prompts the user to input an integer and then outputs both the
individual digits of the number and the sum of the digits.

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
int inputNumber, sum, individualNumber;
cin >> inputNumber;

inputNumber = abs(inputNumber); //Handle negative numbers


sum = 0;

do {
individualNumber = inputNumber % 10; //Extract the last digit of the number
sum += individualNumber;
inputNumber = inputNumber / 10; //Remove the last digit
} while (inputNumber > 0);

cout << "The sum of the individual numbers is: " << sum << endl;

return 0;
}
2. Write a program in C++ that prompts the user to enter two integers. The program outputs
how many numbers are multiples of 3 and how many numbers are multiples of 5 between the
two integers (inclusive).

#include <iostream>
using namespace std;
int main() {

int num1, num2;


int multiple3count = 0;
int multiple5count = 0;
int temp;

cout << "Enter two numbers: ";


cin >> num1 >> num2;
temp = num1;
while (temp <= num2) {

if (temp % 3 == 0)
multiple3count++;
if (temp % 5 == 0)
multiple5count++;
temp++;
}
cout << "Multiples of 3 between " << num1 << " and " << num2 << ": " << multiple3count << endl;

cout << "Multiples of 5 between " << num1 << " and " << num2 << ": " << multiple5count << endl;

return 0;
}
3. Write a program in C++ that prompts the user to input a positive integer. It should then
output a message indicating whether the number is a prime number.

#include <iostream>
using namespace std;
int main()
{
std::cout << "Please input a positive integer: ";
int number;
std::cin >> number;

if(number < 0)
{
std::cout << "Error: integer is negative\n";
return 1;
}

if(number < 2)
{
std::cout << "Integer is not a prime number\n";
return 0;
}

if(number == 2)
{
std::cout << "Integer is a prime number\n";
return 0;
}

if(number % 2 == 0)
{
std::cout << "Integer is not a prime number\n";
return 0;
}

for(int i=3; (i*i)<=number; i+=2)


{
if(number % i == 0)
{
std::cout << "Integer is not a prime number\n";
return 0;
}
}

std::cout << "Integer is a prime number\n";


return 0;
}
if(number == 2)
{
std::cout << "Integer is a prime number\n";
return 0;
}
if(number % 2 == 0)
{
std::cout << "Integer is not a prime number\n";
return 0;
}

for(int i=3; (i*i)<=number; i+=2)


{
if(number % i == 0)
{
std::cout << "Integer is not a prime number\n";
return 0;
}
}

std::cout << "Integer is a prime number\n";


return 0;
}
4. Write a program in C++ that uses while loops to perform the following steps: a. Prompt the
user to input two integers: firstNum and secondNum (firstNum must be less than
secondNum). b. Output all odd numbers between firstNum and secondNum. c. Output the sum
of all even numbers between firstNum and secondNum. d. Output the sum of the square of the
odd numbers between firstNum and secondNum.

#include<iostream>
using namespace std;
int main()

int i, sum = 0, sqSum = 0, firstNum = 1, secondNum = 0;


char ch;
while (!(firstNum < secondNum))

cout << "Enter starting number: ";


cin >> firstNum;
cout<<"Enter ending number(must be > startingNumber): ";
cin >> secondNum;

i = firstNum;

cout << "The odd numbers between " << firstNum


<< " and " << secondNum << " are:\n";
while (i <= secondNum)

{
if (i % 2 == 0)
sum = sum + i;

else

cout << i << " ";


sqSum = sqSum + i * i;

i++;

cout << "\n\nThe sum of the even numbers is:"


<< sum << endl << endl;
cout << "The sum of squares the odd numbers is:"
<< sqSum << endl;

i = 1;
cout << "\nNumber Square\n";
while (i <= 10)

cout << " " << i << "\t " << i * i << endl;
i++;
}

return 0;

5. Write a program in C++ that will loop by inputting any integer and displays whether the
integer is “POSITIVE” or “NEGATIVE”, and terminate the loop if the integer is equal to zero
using any looping construct.

#include <iostream>
using namespace std;

int main()
{
signed long num1 = 0;
cout << "\n\n Check whether a number is positive, negative or zero :\n";
cout << "-----------------------------------------------------------\n";
cout << " Input a number : ";
cin >> num1;
if(num1 > 0)
{
cout << " The entered number is positive.\n\n";
}
else if(num1 < 0)
{
cout << " The entered number is negative.\n\n";
}
else
{
std::cout << " The number is zero.\n\n";
}
return 0;
}

You might also like