0% found this document useful (0 votes)
8 views

Method practice program class 11-1

The document describes multiple Java classes designed to perform various mathematical operations, including calculating series sums, checking for Armstrong numbers, generating prime factors, identifying composite magic numbers, digit frequency analysis, happy number checks, sumproduct number validation, and merging integers. Each class has specific data members and methods to accomplish its tasks, along with a main function to execute the operations. The classes include SeriesSum, ArmNum, PrimeFact, CompMagic, Number, Happy, sum_product, and Merger.

Uploaded by

jeeaspirantom
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)
8 views

Method practice program class 11-1

The document describes multiple Java classes designed to perform various mathematical operations, including calculating series sums, checking for Armstrong numbers, generating prime factors, identifying composite magic numbers, digit frequency analysis, happy number checks, sumproduct number validation, and merging integers. Each class has specific data members and methods to accomplish its tasks, along with a main function to execute the operations. The classes include SeriesSum, ArmNum, PrimeFact, CompMagic, Number, Happy, sum_product, and Merger.

Uploaded by

jeeaspirantom
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/ 13

/*A class SeriesSum is designed to calculate the sum of the following series:

Sum=(x2 / 1!)+(x4 / 3!)+(x6 / 5!)+….(xn /(n- 1)!)


Some of the members of the class are given below:
Classname:SeriesSum
Data members / instance variables:
x:to store an integer number
n:to store number of terms
sum:to store the sum of the series
Member functions:
SeriesSum(int xx,int nn):constructor to assign x=xx and n=nn
double findfact(int m):to return the factorial of m using recursive technique.
double findpower(int x,int y):to return x raised to the power of y using recursive technique.
void calculate():to calculate the sum of the series by invoking the recursive functions respectively
void display():to display the sum of the series
Specify the class SeriesSum, giving details of the constructor(int, int),double findfact(int), double findpower(int , int),
void calculate( ) and void display(). Define the main() function to create an object and call the functions accordingly to
enable the task.

*/

import java.util.Scanner;
public class SeriesSum
{
int x,n;
double sum;
SeriesSum(int xx,int nn)
{
x=xx;
n=nn;
sum=0.0;
}

double findfact(int a)
{
double f=1;
for(int i=1;i<=a;i++)
{
f=f*i;
}
return f;
}

double findpower(int a,int b)


{

return (Math.pow(a,b));
}

void calculate()
{
for(int i=2;i<=n;i=i+2)
{
sum=sum+(double)findpower(x,i)/findfact(i-1);
}
}

void display()
{
System.out.println("sum="+sum);
}

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER A NUMBER");
int a=sc.nextInt();
System.out.println("ENTER NUMBER OF TERMS");
int b=sc.nextInt();
SeriesSum obj1=new SeriesSum(a,b);
obj1.calculate();
obj1.display();
}
}

/*Design a class ArmNum to check if a given number is an Armstrong number or not.


[A number is said to be Armstrong if sum of its digits raised to the power of length of the number is equal to the
number]
Example : 371 = 33 + 73 + 13
1634 = 14 + 64 + 34 + 44
54748 = 55 + 45 + 75 + 45 + 85
Thus 371, 1634 and 54748 are all examples of Armstrong numbers.
Some of the members of the class are given below:
Class name:ArmNum
Data members/instance variables:
n:to store the number
l:to store the length of the number
Methods/Member functions:
ArmNum (int nn):parameterized constructor to initialize the data member n=nn
int sum_pow(int i):returns the sum of each digit raised to the power of the length of the number using recursive
technique
eg. 34 will return 32 + 42 (as the length of the number is 2)
void isArmstrong( ):checks whether the given number is an Armstrong number by invoking the function sum_pow() and
displays
the result with an appropriate message
Specify the class ArmNum giving details of the constructor( ), int sum_pow(int) and void isArmstrong( ).
Define a main( ) function to create an object and call the functions accordingly to enable the task
*/
import java.util.Scanner;
public class ArmNum
{
int n,l;
ArmNum(int nn)
{
n=nn;
l=0;
}
int sum_pow(int i)
{
int n=i,rem=0,n1=i,sum=0;
while(n>0)
{
rem=n%10;
l++;
n=n/10;
}
while(n1>0)
{
rem=n1%10;
sum=sum+(int)(Math.pow(rem,l));
n1=n1/10;
}
System.out.println(sum);
return sum;

void isArmstrong()
{
if(sum_pow(n)==n)
{
System.out.println(n + " is an Armstrong number");
}
else
{
System.out.println(n + "is not an Armstrong number");
}
}

public static void main(String args[])


{
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NUMBER");
int num=sc.nextInt();
ArmNum ob1=new ArmNum(num);
ob1.isArmstrong();
}
}

/*A class PrimeFact generates all the prime factors of a positive Example :
If the number is 24, integer greater than 0. then its prime factors will be: 2, 2, 2, 3. If the number is 11,
its prime factors will be

Class name : PrimeFact


Data members/instance variables
num integer to store a number.
Sum integer to store sum of all the prime factors.
Member functions/methods
PrimeFact( ) constructor to initialize the data members with iniial values.
void Accept( ) to input the value of the data member num.
void generateFacts( ) to generate and print all the prime factors of 'num' and also store the
sum of all the prime factors to the data member 'sum'.
void print( ) to display the original number and the sum of prime factor.
Specify the class PrimeFact, giving details of the constructor, functions void Accept( ), void generateFact( ) and void
Print( ). Define a a main() function to create the object and call the methods accordingly to enable the task.
*/
import java.util.*;
public class PrimeFact
{
int num,sum;
PrimeFact()
{
num=0;
sum=0;
}
void Accept()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of num");
num=sc.nextInt();
}
void generateFacts()
{
int i=2,n=num;
while(n>1)
{
if(n%i==0)
{
System.out.println(i);
sum=sum+i;
n=n/2;
}
else
i++;
}

}
void print()
{
System.out.println("num="+num);
System.out.println("sum="+sum);
}
public static void main(String args[])
{
PrimeFact obj=new PrimeFact();
obj.Accept();
obj.generateFacts();
obj.print();
}
}
/*
class CompMagic has bcen defined to print the composite magic numbers between start limit (m) and the last limit (n)
both inclusive and also the count of such numbers.
A composite Magic number is a positive intcger which is composite as well as magic number Composite Number: A
number which has more than two factors is known as Composite number.
For example: Input: 10. its factors are I, 2, 5, 10 i.e. four factors, so 10 is a composite number.
Few composite magic numbers are: 28, 46, 55, 1225,
Class name : CompMagic
Data members:
m integer to store the starting limit.
n integer to store the last limit.
Member functions/methods:
CompMagic constructor to initialize data members with legal initial values.
void readLimits( ): input start and last limits in 'm' and 'n' respectively.
int IsComposite( int x ) returns 1 if the parameter value 'x'is a composite number otherwise returns 0.
int SumDigits( int v ) returns the sum of digits of the number in the argument 'v
by invoking functions int isComposite and int SumDigits(int)
void Show( ) print all composite-magic-numbers along with count of such numbers both inclusive if m>n,
otherwise print the message "Limits out of range".
Specify the class CompMagic, giving details of the constructor, functions void readLimits( ), int IsComposite( ). int
SumDigits(int) and void Show ). Definc a a main( ) function to create the object and call the methods accordingly.*/
import java.util.Scanner;
public class CompMagic
{
int m,n;
CompMagic()
{
m=0;
n=0;
}
void readLimits()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter m");
m=sc.nextInt();
System.out.println("Enter n");
n=sc.nextInt();
}
int IsComposite( int x )
{
int count = 0,i=0;
for(i = 1; i <= x; i++)
{
if(x % i == 0)
{
count++;
}
}
if(count>2)
{
return 1;
}
else
{
return 0;
}
}
int SumDigits( int v )
{
int sum = 0,rem=0;
while(v>9)
{
sum=0;
while(v>0)
{
rem=v%10;
sum =sum+rem;
v =v/10;
}
v=sum;
}
return sum;
}

void Show( )
{
for(int i=m;i<=n;i++)
{
if(IsComposite(i)==1 && SumDigits(i)==1)
{
System.out.println(i);
}
}
}

public static void main(String args[])


{
CompMagic obj=new CompMagic();
obj.readLimits();
obj.Show();
}
}

/* A class called Number has been defined to find the frequency of each digit
present in it and the sum of the digits and to display the results
Class name :Number
Data Member
num long integer type
Number() Contructor to assign default value
Number(long n) Contructor to assign n to num
void digitFrequency() :to find frequency of each digit and display it
int sumDigits():to return sum of digit of number
*/
import java.util.*;
public class Number
{
long num;
Number()
{
num=0;
}
Number(long n)
{
num=n;
}
void digitFrequency()
{
int n=(int)num,rem=0,count=0;
for(int i=0;i<=9;i++)
{
n=(int)num;
count=0;
while(n>0)
{
rem=n%10;
if(rem==i)
{
count++;
}
n=n/10;
}
if(count>0)
{
System.out.println(i+"\t"+count);
}
}
}
int sumDigits()
{
int rem=0,sum=0;
while(num>0)
{
rem=(int)num%10;
sum=sum+rem;
num=num/10;
}
return sum;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter n");
long n=sc.nextLong();
Number obj = new Number();
Number obj1 = new Number(n);
obj1.digitFrequency();
System.out.println("sum of digit="+obj1.sumDigits());
}
}
/*A happy number is a number in which the eventual sum of the square of the digits of the number is equal to 1.
Example:28=2^2+8^2=4+64=68
68=6^2+8^2=36+64=100
100=1^2+0^2+0^2=1 +0+0=1
Hence,28 is a happy number.
Example:12 =1^2+2^2=1+4=5
Hence, 12 is not a happy number.
Design a class Happy to check if a given number is a happy number.Some of the member of the class are given below:
Classname:Happy
Data members/ instance variables:
n: stores the number
Member functions:
Happy(): constructor to assign 0 to n
void getnum(int nn): to assign the parameter value to the number n=nn
int sum_sq_digits(int x): returns the sum of the square of the digit of the number x. using the recursive technique
void ishappy(): checks if the given number is a happy number by calling the function sum_sq_digits (int) and displays an
appropriate message
Specify the class Happy giving details of the constructor( ). void getnum(int) , int sum_sq_digits(int) and void ishappy() .
Also define a main() functionc to create an object and call the methods to check for happy number.*/
import java.util.Scanner;
public class Happy
{
int n;
Happy()
{
n=0;
}
void getnum(int nn)
{
n=nn;
}
int sum_sq_digits(int x)
{
int sum = 0,rem=0;
while(x>9)
{
sum=0;
while(x>0)
{
rem=x%10;
sum =sum+rem;
x =x/10;
}
x=sum;
}
return sum;
}

void ishappy()
{
if(sum_sq_digits(n)==1)
{
System.out.println(n+" IS A HAPPY NUMBER");
}
else
{
System.out.println(n+" IS NOT A HAPPY NUMBER");
}
}

public static void main()


{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
Happy ob1=new Happy( );
ob1.getnum(n);
ob1.ishappy();
}
}

/*
A number is said to be sumproduct number, if the sum of its digits multiplied by product of its digits is equal to the
original number.
Example: Input : 144 = (1+4+4) * (1*4*4), =9* 16 = 144, so 144 is a sumproduct number.
Design a class sum_product with the following details :
Class name sum_product
Data members/instance variables
N : integer to store a number
Member functions/methods
sum_product( ) constructor to initialize the data members with initial values.
void readNum( ) to accept value of data member N from the input.
int Sum( int v ) returns sum of all the digits of the number v.
int Product( intv) returms product of all the digits of the number v.
void Check( ) decide and print whether the number N is a sumproduct number or
not by invoking functions Sum(int) and Product(int).
Specify the class sum_product, giving details of the constructor, functions void readNum( ), int Sum( ), int Product()
and void Check(). Define a a main( ) function to create the object and call the methods accordingly
*/
import java.util.*;
public class sum_product
{
int N;
sum_product()
{
N=0;
}
void readNum()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
N=sc.nextInt();
}
int Sum(int v)
{
int s=0,rem=0;
while(v>0)
{
rem=v%10;
s=s+rem;
v=v/10;
}
return s;
}
int Product(int v)
{
int p=1,rem=0;
while(v>0)
{
rem=v%10;
p=p*rem;
v=v/10;
}
return p;
}
void check()
{
if(Sum(N)*Product(N)==N)
System.out.println(N+"is a sumproduct number");
else
System.out.println(N+"is not a sumproduct number");
}
public static void main(String args[])
{
sum_product obj=new sum_product();
obj.readNum();
obj.check();
}
}
/*A class Merger concatenates two positive integers that are greater than 0 and produces a new merged integer.
Example:If the first number is 23 and the second is 764,then the concatenated number will be 23764.
Some of the members of the class are given below:
Classname:Merger
Data members/instance variables:
n1:long integer to store first number
n2:long integer to store second number
mergNum:long integer to store mergednumber
Member functions:
Merger():constructor to initialize the data members
void readNum():to accept the values of the data members n1 and n2
void JoinNum():to concatenate the numbers n1 and n2 and store it in mergNum
void show():to display the original numbers and the merged number with appropriate messages
Specify the class Merger,giving the details of the constructor,void readNum(),void JoinNum() and void show( ) .
Define the main() function to create an object and call the functions accordingly to enable the task.*/

import java.util.Scanner;
public class Merger
{
long nl,n2,mergNum;
Merger()
{
nl=0;
n2=0;
mergNum=0;
}

void readNum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter two numbers");
nl=sc.nextLong();
n2=sc.nextLong();
}

void JoinNum()
{
String s1=Long.toString(nl);
String s2=Long.toString(n2);
mergNum=Long.parseLong(s1+s2);
}

void show()
{
System.out.println("First Number="+nl);
System.out.println("Second Number="+n2);
System.out.println("Merged Number="+mergNum);
}

public static void main(String args[])


{
Merger ob1=new Merger();
ob1.readNum();
ob1.JoinNum();
ob1.show();
}
}

import java.util.*;
public class Revno
{
int num;
public Revno()
{
num=0;
}
public void inputnum()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number");
num=sc.nextInt();
}

public void reverse(int num)


{
if(num< 10)
{
System.out.println(num);
return;
}
else{
System.out.print(num%10);
reverse(num/10);
}
}
public void display()
{
System.out.println("Original Number is:"+num);
System.out.println("Reversed Number is:");
reverse(num);
}

public static void main(String args[])


{
Revno rev=new Revno();
rev.inputnum();
rev.display();
}
}

import java.util.*;
public class Lcm
{
int n1,n2,large,sm,lcm;
Lcm()
{
n1=0;
n2=0;
large=0;
sm=0;
lcm=0;
}
void acceptData()
{
Scanner sc=new Scanner(System.in);
System.out.println("enter n1");
n1=sc.nextInt();
System.out.println("enter n2");
n2=sc.nextInt();
}
void getLCM()
{
for(int i=1;i<=n1&&i<=n2;i++)
{
if(n1%i==0&&n2%i==0)
{
large=i;
}
}
lcm=(n1*n2)/large;
}
void printData()
{
System.out.println("n1="+n1);
System.out.println("n2="+n2);
System.out.println("Lcm="+lcm);
}
public static void main(String args[])
{
Lcm obj = new Lcm();
obj.acceptData();
obj.getLCM();
obj.printData();
}
}

You might also like