Method practice program class 11-1
Method practice program class 11-1
*/
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;
}
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);
}
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");
}
}
/*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
}
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);
}
}
}
/* 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");
}
}
/*
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);
}
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();
}
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();
}
}