Computer Science, asked by sharmamanoj9563, 4 months ago

Write a program to input a number. Check and display whether it is a Niven number or not. (A number is said to be Niven which is divisible by the sum of its digits).Example: Sample Input 126Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.

Answers

Answered by shauryakumarsharma10
2

Answer:

num = int(input("Enter a number: "))

temp= num

while (temp>0):

sum+=temp%10

temp=int(temp/10)

result=num%sum

if(result==0):

print("Niven")

else:

print("Not Niven")

Explanation:

I have written this code in python as you didn't mentioned any particular programming language but I think that you might understand the logic.

And I think so this code shall work.

Answered by BrainlyProgrammer
15

Question:-

  • Write a program to input a number. Check and display whether it is a Niven number or not.

What is Niven Number?

  • A number is said to be Niven which is divisible by the sum of its digits

Sample Input:

126

Output:

126 is a Niven Number

As it is not mentioned in the question that in which language the program should be...So the answer is in 3 languages .... JAVA, Python, QBASIC.

➡️JAVA Code:-

package Coder;

import java.util.*;

public class NivenNum{

public static void main (String ar []){

Scanner sc=new Scanner (System.in);

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

int n=sc.nextInt();

int s=0,n1=n;

while(n!=0){

s+=(n%10);n/=10;

}

if(n1%s==0)

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

}

}

Java output attached

➡️ QBASIC CODE:-

CLS

PRINT "ENTER A NUMBER"

INPUT N

S=0

N1=N

WHILE N<>0

S=S+(N MOD 10)

WEND

IF(N1 MOD S=0)

PRINT "NIVEN"

ELSE

PRINT"NOT NIVEN"

_______________________

Explaination:-

  • The program accepts the number from the user
  • Then the program runs a while loop under a condition that the number should not be 0
  • Then it calculates the sum of the digit.
  • Given , S+=n%10; here '%' symbol finds the remainder
  • In QBASIC, we use "MOD" instead of '%' to find the remainder
  • After calculation of the digit last digit of the number is removed.
  • Then again while loop will check the condition thus repeating the procedure until the number becomes 0
  • Then finally Program checks if the number is divisible by the sum, if yes Niven number else not a Niven Number.

______________________

Variable Description:-

  1. n:- to accept the number from the user
  2. n1 :- to make a copy of the number entered
  3. s:- to find the sum of the digits
Attachments:

Anonymous: thank you! :)
BrainlyProgrammer: Welcome!!
Similar questions