Write a program in c++ to input a number and find the last digit of the number is a multiple of 5 or not. explain
Answers
Answered by
1
Answer:
→ This is the required C++ program for the question.
#include <iostream>
using namespace std;
int main()
{
int n,ld;
cout << "Enter a number: ";
cin >> n;
ld=n%10;
if(ld%5==0)
cout << "Last digit of the given number is a multiple of 5.";
else
cout << "Last digit of the given number is not a multiple of 5.";
return 0;
}
Explanation:
- The modulus operator is used to calculate the remainder obtained when a number is divided by another number. So, to get the last digit of the number, we can use modulus operator and divide the number by 10 to get he remainder.
- After finding out the remainder obtained, we will divide the last digit by 5. If the last digit is a multiple of 5, then the number is divisible by 5, i.e., the remainder obtained by dividing the number by 5 is 0.
- So, we will check if the last digit is exactly divisible by 5 or not. If true, display the message that "Last digit of the number is divisible by 5" else not.
→ Refer to the attachment.
Attachments:
Similar questions