Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Niven.java #10

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Niven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.*;
public class Main{
//main method
public static void main(String args[]){
//declared an integer variable and initialized the value
int originalNumber = 21;
//print the given input number
System.out.println("Given number:"+originalNumber);
//keep a copy of original number
int copyOfOriginalNumber = originalNumber;
//initialize sum as 0
int sum = 0;
//Find sum of all digits of the number
//continue the loop till the number is greater than 0
while(originalNumber > 0){
//get the rightmost digit of the number by using % operator
int rem = originalNumber%10;
//add the digit(rem) to sum
sum = sum + rem;
//remove the rightmost digit from number and get the updated number
originalNumber = originalNumber/10;
}
//printing the result
if(copyOfOriginalNumber % sum == 0)
System.out.println(copyOfOriginalNumber+" is a niven number");
else
System.out.println(copyOfOriginalNumber+" is not a niven number");
}
}