write a java program to input a number and check whether the is divisible by 7
Answers
/*
Project Type: Brainly Answer
Date Created: 11-02-2021
Date Edited Last Time: ---NIL---
Question Link: https://brainly.in/question/34997592
Question: Write a java program to input a number and check
whether the is divisible by 7.
Program Created By: atrs7391
Programming Language: Java
Language version (When program created or last edited): jdk-15.0.2
*/
package Brainly_Answers;
import java.util.Scanner;
public class Number_Check_if_Divisible_by_7 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
double n = sc.nextDouble();
if (n%7 == 0) {
System.out.println("Given number is divisible by 7. ");
}
else {
System.out.println("Given number is not divisible by 7. ");
}
}
}
Answer:
// Java program to check whether a number is divisible by 7
import java.io.*;
class GFG
{
// Function to check whether a number is divisible by 7
static boolean isDivisibleBy7(int num)
{
// If number is negative, make it positive
if( num < 0 )
return isDivisibleBy7( -num );
// Base cases
if( num == 0 || num == 7 )
return true;
if( num < 10 )
return false;
// Recur for ( num / 10 - 2 * num % 10 )
return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) );
}
// Driver program
public static void main (String[] args)
{
int num = 616;
if(isDivisibleBy7(num))
System.out.println("Divisible");
else
System.out.println("Not Divisible");
}
}
Explanation: