WS9 Iterativel - Constructs ANSWERS
WS9 Iterativel - Constructs ANSWERS
s=s+(n%10);
n=n/10;
}
System.out.println(s);
}
}
3. Input a number and calculate the sum of even and odd digits
public class ws9_q3_sum_ofeven_odd_digits
{
public static void main(int n)
{
int se=0;
int so=0;
int t=0;
while (n!=0)
{
t=n%10;
if(t%2==0)
{
se=se+t;
n=n/10;
}
else
{
so=so+t;
n=n/10;
}
}
System.out.println("sum of even digits"+se);
System.out.println("sum of odd digits"+so);
}
s=(s*10)+(n%10);
n=n/10;
}
System.out.println(s);
}
}
5. Input a number from user.Check and print whether the number is palindrome number or not.
(Hint: If a number and its reverse form is same then the number is a Palindrome number)
Eg :121 is a palindrome number as its reverse is also 121
123 is not a palindrome number as its reverse is 321.
s=(s*10)+(n%10);
n=n/10;
}
if (s==t)
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not a Palindrome");
}
}
}
6. Input a number and sum its first and last digit.
public class ws9_sumof_1standlastdigit
{
public static void main(int n)
{
int s=0;
int t=n;
while (n!=0)
{
s=(s*10)+(n%10);
n=n/10;
}
int f=s%10;
int l=t%10;
System.out.println(f+l);
}
}
7. Input a number and print its smallest and largest digit present.
public class ws9_max_mindigit
{
public static void main(int n)
{
int l=0;
int t=n;
int max=0;
int min=9;
while(n!=0)
{
l=n%10;
if(l>=max)
{
max=l;
}
else if(l<=min)
{
min=l;
}
n=n/10;
}
System.out.println("Maximum number is"+max);
System.out.println("Minimum number is"+min);
}
}
s=s+(Math.pow((n%10),3));
n=n/10;
}
if (t==s)
{
System.out.println("Amstrong");
}
else
{
System.out.println("Not Amstrong");
}}
9. Input a number and calculate sum of prime digits.
Eg. N=183452
S=2+3+5=10
public class ws9_sumof_primedigits
{
public static void main(int n)
{
int s=0;
int f=0;
while (n!=0)
{
s=n%10;
{
int c=0;
for(int j=1;j<=s;j=j+1)
{
if(s%j==0)
l=l+1;
}
if(c==2)
f=f+s;
n=n/10;
}
} System.out.println(f);
}
10. Input two numbers and print all the prime numbers between them
public class ws9_primenumber_between2nums
{
public static void main(int a,int b)
{
System.out.println("Prime numbers between "+a+"and"+b+":");
for (int i = a; i <= b; i++)
{
int count=0;
for (int c=1;c<=i;c++)
{
if(i%c==0)
{
count=count+1;
}
}
if (count==2)
{
System.out.println(i);
}
}
}}
11. Input two numbers and print all the perfect numbers between them
}
}}
12. Input a number ,check and print whether it is a neon number or not.
(If the sum of digit of square of a number is equal to the original number
Eg. N=9 , square of 9=81 , sum of digits of 91-> 8+1=9 so 9 is a neon number)
// 9 is a neon number 9square=81 i.e.8+1=9
public class ws9_neonnumber
{
public static void main(int n)
{
int s=0;
int temp=n*n;
while(temp>0)
{
int d=temp%10;
s=s+d;
temp=temp/10;
}
if (n==s)
System.out.println(n+"is a neon number");
else
System.out.println(n+"is not a neon number");
}
}
13. Input a number and check whether it is a spy number or not
(Hint: If the sum of all the digits is equal to the product of all the digits
Eg: 132 ,as sum of numbers(1+3+2=6) = product of numbers(1*3*2=6))
s=s+(n%10);
p=p*(n%10);
n=n/10;
}
if (s==p)
System.out.println(t+"is spy number");
else
System.out.println(t+"is not a spy number");
}
}
14. Input a number and check whether a number is Niven number /Harshad number or not
(A positive number which is divisible by the sum of its digits
Eg. 126 /(1+2+6)=14
15. Input a number and check whether the number is Automorphic number or not.
(A number which itself is contained in the last digits of its square)
Eg, 52 =25, (76)2=5776
16. 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 126
Sum of its digits = 1 + 2 + 6 = 9 and 126 is divisible by 9.
import java.util.*;
public class niven{
public static void main()
{
Scanner src=new Scanner(System.in);
System.out.println("Enter the number");
int x= src.nextInt();
int y=x;
int digitsum=0;
while(x!=0){
int digit= x%10;
x=x/10;
digitsum+=digit;
}
if (digitsum != 0 && y % digitsum == 0)
System.out.println("It is a Niven number");
else
System.out.println("It is not a Niven number");
}
}
. A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result
is equal to the original two-digit number. Example: Consider the number 59.
Sum of digits = 5 + 9 = 14 Product of digits = 5 * 9 = 45
Sum of the sum of digits and product of digits = 14 + 45 = 59
Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is
equal to the number input, then display the message "Special 2 - digit number" otherwise, display the message
"Not a special two-digit number".
import java.util.Scanner;
// * If num becomes 0 after extracting first digit ,it means that user entered a single digit number.
// * We should check for such invalid inputs.
if (num == 0) {
System.out.println("Invalid input. Entered number is not a 2 digit number");
System.exit(0);
}
if (num != 0) {
System.out.println("Invalid input. Entered number is not a 2 digit number");
System.exit(0);
}
}
}