Computer Science, asked by gghjhhh, 9 months ago

write a program to input a number and check whether it is a niven number or not ( a niven number is such which is divisible by the sum of its digits)​

Answers

Answered by AjayKumarr676
25

import java.util.*;

public class Niven

{

public static void main(String args[])

{

Scanner in = new Scanner(System.in);

int n,s=0,d,p;

System.out.println("Enter the number");

n=in.nextInt();

p=n;

do

{

d=n%10;

s=s+d;

n=n/10;

}

while(n!=0);

if(p%s==0);

System.out.println("Niven number");

else

System.out.println("Not a niven number");

}

}

Answered by duragpalsingh
1

Question:

Write a program to input an integer and check whether it is Harshad or Niven number or not. A number is said to be Harshad if it is divisible by the sum of the digits of that number, example 126 and 1729.

Solution:

Language used: Java

import java.util.*;

public class NivenNo

{

static void main()

{

Scanner sc=new Scanner(System.in);

int d,s=0,n,t;

System.out.println("Enter an integer:");

n=sc.nextInt();

t=n;

while(t!=0)

{

d=t%10;

s=s+d;

t=t/10;

}

if(n%s==0)

System.out.println("Harshad Number");

else

System.out.println("Not a Harshad Number");

}

}

Similar questions