Computer Applications
Define a class to accept a number and check whether the number is valid number or not. A valid number is a number in which the eventual sum of digits of the number is equal to 1.
e.g., 352 = 3 + 5 + 2 = 10 = 1 + 0 = 1
Then 352 is a valid number.
Answer
import java.util.Scanner;
public class KboatValidNum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number : ");
int num = in.nextInt();
int n = num;
while (n > 9) {
int sum = 0;
while (n != 0) {
int d = n % 10;
n /= 10;
sum += d;
}
n = sum;
}
if (n == 1)
System.out.println(num + " is Valid Number");
else
System.out.println(num + " is not Valid Number");
}
}
Output
Related Questions
Define a class to accept the elements of an array from the user and check for the occurrence of positive number, negative number and zero.
Example
Input Array: 12, -89, -56, 0, 45, 56
Output:
3 Positive Numbers
2 Negative Numbers
1 ZeroDefine a class Anagram to accept two words from the user and check whether they are anagram of each other or not.
An anagram of a word is another word that contains the same characters, only the order of characters is different.
For example, NOTE and TONE are anagram of each other.Write the code to print following patterns
(i)
1 2 6 3 7 10 4 8 11 13 5 9 12 14 15
(ii)
A A A A A A A A B B A A C C C A D D D D E E E E E
Write a program to accept name and total marks of N number of students in two single subscript arrays name[ ] and totalmarks[ ].
Calculate and print:
(i) The average of the total marks obtained by N number of students.
[average = (sum of total marks of all the students)/N]
(ii) Deviation of each student's total marks with the average.
[deviation = total marks of a student - average]