Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A five digit integer is said to be friendly if the leftmost digit is divisible b

ID: 3672644 • Letter: A

Question

A five digit integer is said to be friendly if the leftmost digit is divisible by 1, the leftmost two digits are divisible by 2, the leftmost three digits are divisible by 3, the leftmost four digits are divisible by 4, and the leftmost five digits (the five digit number itself) is divisble by 5. For example, the number 42325 is friendly because 4 is divisble by 1, 42 is divisible by 2, 423 is divisible by 3, 4232 is divisible by 4, and 42325 is divisible by 5. Write a program that prompts for a five digits integer and determines whether or not the number is "friendly."

Explanation / Answer


#include<iostream>
using namespace std;

int main()
{
   int num = 0;
   cout << "Enter a five digit number" << endl;
   cin >> num;
   bool isFriendly = false;

   //not checking for leftmost digit because it will always be divisible by 1
   //leftmost two digits
   int a = num / 1000;
   //leftmost three digits
   int b = num / 100;
   //leftmost four digits
   int c = num / 10;
   //checking for all the conditions for being a friendly number
   if (a % 2 == 0 && b % 3 == 0 && c % 4 == 0 && num % 5 == 0)
   {
       isFriendly = true;
   }

   if (isFriendly)
   {
       cout << num << " is a friendly number" << endl;
   }
   else
   {
       cout << num << " is not a friendly number" << endl;
   }

   return 0;
}

sample output:

Enter a five digit number
12345
12345 is not a friendly number

Enter a five digit number
42325
42325 is a friendly number

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote