[go: up one dir, main page]

0% found this document useful (0 votes)
6 views8 pages

WS9 Iterativel - Constructs ANSWERS

The document contains a worksheet for Class IX on iterative constructs in Java, featuring various programming exercises. Each exercise requires writing a Java program to perform tasks such as counting digits, summing digits, checking for palindromes, and identifying special numbers. The document provides code snippets for each task, demonstrating the use of loops and conditional statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

WS9 Iterativel - Constructs ANSWERS

The document contains a worksheet for Class IX on iterative constructs in Java, featuring various programming exercises. Each exercise requires writing a Java program to perform tasks such as counting digits, summing digits, checking for palindromes, and identifying special numbers. The document provides code snippets for each task, demonstrating the use of loops and conditional statements.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

2023-24

Subject: Computer Applications


WORKSHEET-9
(Iterative constructs in Java)
Class: IX
NUMBERS AND DIGITS
Write program in java to:
1. Input a number n and print each digit separately and print the count of digits (total number of digits).
For eg. 3456
Output: Digits are: 3 4 5 6
Total digits=4
//
ount number of digits
public class ws9_q1_count_no_ofdigits
{
public static void main(int n)
{
int c=0;
while (n!=0)
{
// int p=(n%10);
n=n/10;
c=c+1;
// System.out.println(p);
}
System.out.println(c);
}
}
2. Input a number and sum the digits

public class ws9_q2_sum_ofdigits


{
public static void main(int n)
{
int s=0;
while (n!=0)
{

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);
}

4. Input a number and print it in a reverse order.


public class ws9_q3_reversenumber
{

public static void main(int n)


{
int s=0;
while (n!=0)
{

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.

public class ws9_q4_palindrome


{
public static void main(int n)
{
int s=0;
int t=n;
while (n!=0)
{

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);
}
}

8. Input a number and check whether it is an Amstrong number or not.


(Hint: If the sum of cubes of each digit of a number is equal to the original number
Eg. 153=13+53+33 =153

public class ws9_amstrong


{
public static void main(int n)
{
double s=0.0;;
double t=n;
while (n!=0)
{

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

public class ws9_perfectnumber_between2num


{
public static void main(int a,int b)
{
System.out.println("Perfect numbers between "+a+"and"+b+":");
for (int i = a; i <= b; i++)
{
int sum=0;
for (int c=1;c<i;c++)
{
if(i%c==0)
{
sum=sum+c;
}
}
if (i==sum)
{
System.out.println(i);
}

}
}}

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))

public class ws9_spynumber


{
public static void main(int n)
{ int t=n;
int s=0,p=1;
while (n!=0)
{

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

public class ws9_AutomorphicNumber


{
public static void main(int num)
{
int count=0;
int square = num*num;
int temp = num;
while(temp>0)
{
count++;
temp=temp/10;
}
//determines the last digit of the variable square
int lastDigit = (int) (square%(Math.pow(10, count)));
if(num == lastDigit)
System.out.println(num+ " is an automorphic number.");
else
System.out.println(num+ " is not an automorphic number.");
}
}

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;

public class WS9_SpecialNumber


{
public void checkNumber(int num ) {

int num = orgNum;


int digit1 = num % 10;
num =num/10;

// * 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);
}

int digit2 = num % 10;


num /= 10;
/* If num does not become 0 after extracting second digit
it means that entered number has 3 or more digits.
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);
}

int digitSum = digit1 + digit2;


int digitProduct = digit1 * digit2;

if ((digitSum + digitProduct) == orgNum)


System.out.println("Special 2-digit number");
else
System.out.println("Not a special 2-digit number");

}
}

You might also like