Вы находитесь на странице: 1из 7

Program -1

PROGRAM – 1
Fibonacci series is generated as given below:
0,1,1,2,3,5,8,13.....................................
Write a program to generate the elements of Fibonacci series up to a given limit entered

Algorithm
Step 1 : start
Step 2 : enter number of terms to display in fibonacci series
Step 3 : if n=0 or n=1 then function returns tn (t refers term)
Step 4 : if n>1 then function returns tn-2+tn-1
Step 5 : print the fibonacci series
Step 6 : stop

//a program to generate fibonacci series using recursive


import java.util.*;
class fseries
{
int fiboseries(int num)
{
if(num==1)
return(0);
else if(num==2)
return(1);
else if(num>2)
return(fiboseries(num-1)+fiboseries(num-2));
else
return(-1);
}
public static void main()
{
Scanner sc= new Scanner(System.in);
System.out.println("enter number of terms to display ");
int n,p=0;
n=sc.nextInt();
fseries ob=new fseries();
System.out.println("the fibonacci series:");
for(int i=1;i<=n;i++)
{
p=ob.fiboseries(i);
System.out.println(p+”,”);
}
}
}

input-
enter number of terms to display
8

output-
the fibonacci series:
0,1,1,2,3,5,8,1,3,
Program -1

PROGRAM – 2
Write a program to input two numbers . Use a recursive function that returns HCF of both
the numbers.

ALGORITHM-
Step 1 : Start
Step 2 : input two numbers (n1,n2)
Step 3 : check if n1 = n2 then return (n1) else if n1>n2 then return( n1-n2) else if n2>n1
then return (n2-n1) this process repeat till n1=n2
Step 4 : print the HCF of two numbers
Step 5 : stop

// a program to find the HCF of two numbers by recursion


import java.util.*;
class HCF
{
int findhcf(int n1,int n2)
{
if(n1==n2)
return(n1);
else
if(n1>n2)
n1=n1-n2;
else
if(n2>n1)
n2=n2-n1;
return(findhcf(n1,n2));
}
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter two number");
int num1,num2,numhcf;
num1=sc.nextInt();
num2=sc.nextInt();
HCF ob=new HCF();
numhcf=ob.findhcf(num1,num2);
System.out.println("HCF of two numbers ="+numhcf);
}
}

Enter two number


40
25

HCF of two numbers =5

PROGRAM-3
Program -1
To create Pascal’s triangle

Algorithm:

STEP 1 - START
STEP 2 - pas[0] = 1
STEP 3 - IF i=0 THEN GOTO
STEP 4 - IF j=0 THEN GOTO STEP 5
STEP 5 - PRINT pas[j]+" "
STEP 6 - i++& IF i<n GOTO STEP 4
STEP 7 - j=0 & IF j<=i GOTO STEP 5
STEP 8 - IF j=i+1 THEN GOTO STEP 7
STEP 9 - pas[j]=pas[j]+pas[j-1]
STEP 10 - j--& IF j>0 GOTO STEP 9
STEP 11 - STOP

class pascal
{
public void pascalw(int n)
{
int [] pas = new int [n+1];
pas[0] = 1;
for (int i=0; i<n; i++)
{
for (int j=0; j<=i; ++j)
System.out.print(pas[j]+" ");
System.out.println( );
for (int j=i+1; j>0; j--)
pas[j]=pas[j]+pas[j-1];
}
}
}

Output:

n=5

1
11
121
1331
14641

PROGRAM 4 –
Program -1
To display entered number in words

Algorithm:

STEP 1- START
STEP 2- INPUT amt
STEP 3- z=amt%10 , g=amt/10
STEP 4- IF g!=1 THEN GOTO STEP 5 OTHERWISE GOTO STEP 6
STEP 5- PRINT x2[g-1]+" "+x1[z]
STEP 6- PRINT x[amt-9
STEP 7- END

import java.util.*;
class eng
{
public static void main()
{
Scanner sc=new Scanner(System.in) ;
String x3;
System.out.println("Enter any Number(less than 99)");
int amt=sc.nextInt();
int a,b,c,y,z,g;
String x[]={“
","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eighteen","
Nineteen"};
String x1[]={" ","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"};
String x2[]={" ","Twenty","Thirty","Fourty","Fifty","Sixty","Seventy","Eighty","Ninety"};
z=amt%10;
g=amt/10;
if(g!=1)
System.out.println(x2[g-1]+" "+x1[z]);
else
System.out.println(x[amt-9]);
}
}

Output:

Enter any Number(less than 99)

45

Fourty Five
Program -1
PROGRAM- 5
Given the two positive integers p and q, where p < q. Write a program to determine how
many kaprekar numbers are there in the range between 'p' and 'q'(both inclusive) and
output them. About 'kaprekar' number: A positive whole number 'n' that has 'd' number of
digits is squared and split into 2 pieces, a right hand piece that has 'd' digits and a left hand
piece that has remaining 'd' or 'd-1' digits. If sum of the pieces is equal to the number then
it's a kaprekar number.

Algorithm

Step-1: Enter the limits


Step-2: Run a loop using the entered limits
Step-3: For each number in the range, count the number of digits(d) in it
Step-4: Find the square of the number
Step-5: Now, divide the square in two parts
Step-6: To do this, first store 10 raised to the power d in it
Step-7: For first part, calculate square mod t
Step-8: For second part, calculate square/t
Step-9: Now add the two parts
Step-10: If the sum is equal to the original number then it is a kaprekar number, print it
and count it.
Step-11: If the sum is not equal to the original number it is not a kaprekar number
Step-12: Continue the process till all the numbers in the range are checked
Step-13: Display the frequency of kaprekar numbers.
Step-14: End
Program -1
import java.util.*;
class Question4
{
public static void main()
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter the range : ");
int p=scan.nextInt();
int q=scan.nextInt();
int d,i,n,a,b,s,freq;
freq=0; // to find the frequency of kaprekar numbers
System.out.println("The Kaprekar numbers are : ");
for(i=p;i<=q;i++)
{
n=i;
d=0; //to store the number of digits
//count the number of digits in the number
while(n>0)
{
d++;
n/=10;
}
s=i*i ; // find the square of the number
//extract 'd' digits from the right of the square of the number
a=s%(int)Math.pow(10,d);
//extract 'd' or 'd-1' digits from the left of the square of the number
b=s/(int)Math.pow(10,d);
//Check if the two parts add up to the original number i.e. Condition for Kaprekar number
if(a+b==i )
{
System.out.print(i+" ");
freq++;
}
}
System.out.println("\nFREQUENCY OF KAPREKAR NUMBER IS : "+freq);
} //end of main
} //end of class

INPUT: p=1 Q=1000


OUTPUT: THE KAPREKAR NUMBERS ARE: 1,9,45,55,99,297,999
FREQUENCY OF KAPREKAR NUMBERS IS:8
Program -1

Вам также может понравиться