0% found this document useful (0 votes)
54 views4 pages

Icse 10th Computer

The document contains code snippets for several Java programs that demonstrate user-defined methods to: 1) Check if a number is an Armstrong number using a method called Armstrong() 2) Check if a number is a pronic number using a method called Pronic() 3) Display the two factors of a two-digit number using a method called fact() 4) Find the greatest common divisor (GCD) and least common multiple (LCM) of two numbers using a method called Glcm() 5) Calculate income tax based on income level using a method called Tax()

Uploaded by

HIMA K R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views4 pages

Icse 10th Computer

The document contains code snippets for several Java programs that demonstrate user-defined methods to: 1) Check if a number is an Armstrong number using a method called Armstrong() 2) Check if a number is a pronic number using a method called Pronic() 3) Display the two factors of a two-digit number using a method called fact() 4) Find the greatest common divisor (GCD) and least common multiple (LCM) of two numbers using a method called Glcm() 5) Calculate income tax based on income level using a method called Tax()

Uploaded by

HIMA K R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Unsolved programs (pg – 337)

Ch 5 – User Defined Methods

2. // Armstrong no.
import java.util.*;
public class q2
{
int Armstrong (int n)
{
int i,a=1,m=1,s=0, num, ld;

num = n;
while(n != 0)
{
ld=n%10;
s = s+ (ld * ld * ld);
n = n /10;
}
if (s == num)
return 1;
else
return 0;
}
public static void main(String args[])
{
int n1;
Scanner in=new Scanner(System.in);
System.out.println("enter no");
n1=in.nextInt();
q2 ob = new q2();
int x = ob.Armstrong(n1);
if (x==1)
System.out.println(n1+" - is an Armstrong no");
else
System.out.println(n1+" - is not an Armstrong no");
}
}

3. // pronic number
import java.util.*;
public class pronic3
{
int Pronic(int n)
{
int i, pn=0;
for(i=1; i<n; i++)
{
if(i *(i+1) == n)
{
pn = 1;
break;
}}
return(pn);
}
public static void main(String args[])
{
Scanner in=new Scanner (System.in);
int d, a;
System.out.println("enter a number");
d=in.nextInt();
pronic3 ob=new pronic3();
a=ob.Pronic(d);
if(a==1)
System.out.println(d+" is a pronic number");
else
System.out.println(d+" is not a pronic number");
}
}
4. //Program to display two factors of a no.
import java.util.*;
class Factors4
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a two digit no.");
int m = in.nextInt();
Factors4 ob = new Factors4();
ob.fact(m);
}
void fact(int n)
{
int c=0;
int p =n;
while(p!=0)
{
int a=p%10;
c++;
p=p/10;
}

if (c==2)
{
int a, s=0;
for(a=2; a<=n; a++)
{
if(n%a==0)
{
s = a;
break;
}
else
continue;
}
System.out.println("1st Factor - "+s);
int d = n/s;
System.out.println("2nd Factor - "+d);
}
else
System.out.println("Enter 2 digit no");
}
}
7. // To find GCD and LCM
import java.util.*;
public class Q_7
{
public void Glcm(int a, int b)
{
int t=0;
//to make sure b is greater than a
//if a greater than b then swap
if(a>b)
{
int c =b;
b= a;
a= c;
}
while(b%a != 0) //this loop calculates gcd.
{
t=b%a;
b= a;
a= t;
}

int gcd=a;
System.out.println("gcd "+a);

int lcm=((a*b) / gcd); // formula for lcm given in pg 113


System.out.println("lcm"+ lcm);
}
public static void main( String args[])
{
Scanner in =new Scanner (System.in);
System.out.println("enter num");
int x= in.nextInt();
System.out.println("enter num");
int y=in.nextInt();
Q_7 ob= new Q_7();
ob. Glcm( x , y);
}
}
12. // To find income tax
import java.util.*;
public class IncomeTax
{
public void Tax (String name, int income)
{
double tax;
if (income <= 250000)
tax = 0;
else if (income >= 250001 && income <= 500000)
tax = 10/100.0 * (income - 250000);
else if (income >= 500001 && income <= 1000000)
tax = 30000 + 20/100.0 * (income - 500000);
else
tax = 50000 + 30/100.0 * (income - 1000000);
System.out.println("Name : "+name);
System.out.println("Income Tax : "+tax);
}
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
System.out.print("Enter employee name : ");
String n = in.nextLine();
System.out.print("Enter annual income : ");
int i = in.nextInt();
IncomeTax obj = new IncomeTax();
obj.Tax (n,i);
}
}

_____________________

You might also like