Write a program to get 3 numbers from user for integer variable a, b,and c. if a is not zero, find out whether it is the common divisor of b and c.

Dani AI

Generated

A concise, portable approach for the stated task (read three integers a, b, c and, if a != 0, test whether a is a common divisor of b and c). asked for that check; 's advice to start from a simple I/O skeleton is sound, and 's example shows why the standard headers and int main() are preferable to non‑portable headers.

Algorithm summary: read the three integers; verify input succeeded; if a == 0 report that testing is invalid (modulus/division by zero is undefined); otherwise test b % a == 0 and c % a == 0. A zero remainder means divisibility even if values are negative, but absolute-value interpretation of "divisor" can be enforced with std::abs if desired.

#include <iostream>

int main()
{
    int a, b, c;
    if (!(std::cin >> a >> b >> c)) {
        std::cout << "Invalid input\n";
        return 1;
    }

    if (a == 0) {
        std::cout << "a must not be zero (modulus by zero is undefined)\n";
        return 0;
    }

    if (b % a == 0 && c % a == 0)
        std::cout << a << " is a common divisor of " << b << " and " << c << '\n';
    else
        std::cout << a << " is not a common divisor of " << b << " and " << c << '\n';

    return 0;
}

Notes and improvements: handle non‑integer input by checking std::cin as shown; if the concept of "divisor" should treat signs uniformly, use std::abs or, with C++17 and later, compute std::gcd(b, c) from <numeric> and check whether std::gcd(b, c) % std::abs(a) == 0.

Recommended Answers

All 3 Replies

Do you know how to code a 'Hello World' C++ program?

Do you know how to get integer user input (using cin) with a prompt for input?

Do you know how to code to test if a is zero ?

Do you know what 7 % 3 is ... in C++ ? ( or... 6 % 3 ? )

Do you that a && b is true only if both a is true and b is true?

Start with a working 'Hello World' shell ... and add / compile / test from there.

When you get into a bind ...afer going back to your lasting working stage ... and trying again from there ...

Show us your last working stage ... and the compiler errors printed out where you can not see what the problem is.

#Include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
cout<<"Hello World!!!";

getch();
}

Look here for some more ideas about getting input ...

Do you see the code option ... use that to submit code to preserve the indentation

#include <iostream>

// do NOT use to keep code portable
//#include<conio.h> 

int main() // NOT void main
{
    // clrscr(); // NOT portable code
    std::cout<<"Hello World!!!";
    // getch(); // NOT portable code

    std::cout << "Press 'Enter' to continue ... ";
    std::flush;
    std::cin.get();
    return 0; // if left off, C++ will supply this
}

Now ... code for some input of a, b and c

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.