Sie sind auf Seite 1von 58

Question I

Write a program in Java to take numbers from The User and create a Pascals Triangle with
them.

1
Algorithm I

Step 1: Start.

Step 2: pas[0]=1.

Step 3: if i=0 then go to Step 4.

Step 4: if j=0 then go to Step 5.

Step 5: Print pas[j]+ .

Step 6: i++ and if i<n go to Step 4.

Step 7: j=0 and if j<=i go to Step 5.

Step 8: if j=i+1 then go to Step 7.

Step 9: pas[j]=pas[j]+pas[j-1].

Step 10: j-- and if j>0 go to Step 9.

Step 11: End.

2
Coding I

import java.io.*;
class Pascal
{
public void pascalw()throws IOException //pascalw() function
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a no.");
int n = Integer.parseInt(br.readLine()); //accepting value
int [ ] pas = new int [n + 1];
pas[0] = 1;
for (int i = 0; i<n; i++) //loop evaluating the elements
{
for (int j = 0; j <= i; ++j)
System.out.print(pas[j]+" "); //printing the Pascal Triangle elements
System.out.println();
for (int j = i + 1; j > 0; j--)
pas[j]=pas[j] + pas[j - 1];
}
}
}

3
Output I

4
Variable Description I

Variable Name Variable Type Variable Method Variable Purpose

br BufferedReader pascalw() BufferedReader Object

n Integer pascalw() Input Value

pas Integer [ ] pascalw() Matrix Storing Pascal Numbers

i Integer pascalw() Loop Variable

j Integer pascalw() Loop Variable

5
Question II

Write a program in Java to take a Number from The User and Convert it to Words.

6
Algorithm II

Step 1: Start.

Step 2: Input amt.

Step 3: z=amt%10 , g=amt/10.

Step 4: if g!=1 then go to Step 5 otherwise go to Step 6.

Step 5: Print x2[g-1]+ +x1[z].

Step 6: Print x[amt-9].

Step 7: End.

7
Coding II

import java.io.*;
class Numbers
{
public static void main(String args[])throws IOException//main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter any Number(less than 99)");
int amt=Integer.parseInt(br.readLine());//accepting number
int z,g;
String
x[]={"","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eight
een","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;//finding the number in words
g=amt/10;
if(g!=1)
System.out.println(x2[g-1]+" "+x1[z]);
else
System.out.println(x[amt-9]);
}
}

8
Output II

9
Variable Description II

Variable Name Variable Type Variable Method Variable Purpose

br BufferedReader main() BufferedReader Object

z Integer main() amt%10

g Integer main() amt/10

x String[] main() String Array storing no.inwords

x1 String[] main() String Array storing no.inwords

x2 String[] main() String Array storing no.inwords

amt Integer main() Input Number

10
Question III

Write a Program in Java to display an AP series and its sum.

11
Algorithm III

Step 1: Start.

Step 2: a=d=0.

Step 3: import a, d.

Step 4: this.a=a and this.d=d.

Step 5: import n.

Step 6: return (a+(n-1)*d).

Step 7: import n.

Step 8: return (n*(a+nTHTerm(n))/2).

Step 9: Import n.

Step 10: Print \n\tSeries\n\t".

Step 11: if i=1;i<=n;i++ go to Step 12.

Step 12: Print nTHTerm(i)+" ".

Step 13: i++ and if i<=n go to Step 12.

Step 14: Print n\tSum : "+Sum(n).

Step 15: End.

12
Coding III

import java.io.*;
class APSeries
{
private double a,d;
APSeries() //default constructor
{
a=d=0;
}
APSeries(double a,double d) //parameterized constructor
{
this.a=a;this.d=d;
}
double nTHTerm(int n)//final AP term
{
return (a+(n-1)*d);
}
double Sum(int n)//function calculating sum
{
return (n*(a+nTHTerm(n))/2);
}
void showSeries(int n)//displaying AP Series
{
System.out.print("\n\tSeries\n\t");
for(int i=1;i<=n;i++)
{
System.out.print(nTHTerm(i)+" ");
}
System.out.print("\n\tSum :"+Sum(n));
}
void main()throws IOException //main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 1st term");

13
a=Integer.parseInt(br.readLine());//accepting 1st term
System.out.println("Enter Common difference");
d=Integer.parseInt(br.readLine());//accepting common difference
System.out.println("Enter no.of terms");
int n=Integer.parseInt(br.readLine());//accepting no. of terms
nTHTerm(n);
Sum(n);
showSeries(n);
}
}

14
Output III

15
Variable Description III

Variable Variable Variable Method Variable Purpose


Name Type

a Integer - 1st Number

d Integer - 2nd Number

n Integer Sum(), showSeries(),nTHTerm() 3rd Number

i Integer showSeries() Temporary Number

16
Question IV

Write a program in java to display the calendar of any month of any year.

17
Algorithm IV

Step 1: Start.

Step 2: Input int month,int year.

Step 3: int i, count=0, b, c, d=1 and String w="SMTWTFS".

Step 4: if (year%100==0 && year%400==0) || (year%100!=0 && year%4==0).

Step 5: days[1]=29.

Step 6: Print "================The Calendar of"+month1[month-1]+"


"+year+"is==================").

Step 7: if i=0 then go to Step 8.

Step 8: Print (i)+"\t" & " ".

Step 9: if i=1 go to Step 10.

Step 10: if (year%100==0 && year%400==0) || (year%100!=0 && year%4==0) then go to Step
11 otherwise go to Step 12.

Step 11: count+=2.

Step 12: count+=1.

Step 13: If i=0 go to Step 14.

Step 14: count+=days[i] , count+=1, count%=7 & b=7-count.

Step 15: If b!=1 || b!=7 go to Step 16.

Step 16: If count>0 go to Step 17,18.

Step 17: Print ' '+"\t").

Step 18: count--.

Step 19: if i=1 go to Step 20.

Step 20: if b>0 && if d<=days[month-1] go to Step 21,22.


18
Step 21: Print d+"\t".

Step 22: d++ and b--.

Step 23: b=7.

Step 24: i++ and if i<MONTH go to Step 14.

Step 25: Print " ".

Step 26: End.

19
Coding IV

import java.io.*;
class Calendar
{
public void dee()throws IOException//dee() function
{
int i,count=0,b,d=1;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter month");//accepting month and year
int month=Integer.parseInt(br.readLine());
System.out.println("Enter Year");
int year=Integer.parseInt(br.readLine());/* Computing and displaying calendar*/
String w="MTWTFSS";
int days[]={31,28,31,30,31,30,31,31,30,31,30,31};
String
month1[]={"January","February","March","April","May","June","July","August","September","
October","November","December"};
if((year%100==0 && year%400==0) || (year%100!=0 && year%4==0))
days[1]=29;
System.out.println("================ The Calendar of "+month1[month-1]+"
"+year+" is ==================");
for(i=0;i<w.length();i++)
System.out.print(w.charAt(i)+"\t");
System.out.println(" ");
for(i=1;i<year;i++)
if((year%100==0 && year%400==0)||(year%100!=0 && year%4==0))
count+=2;
else
count+=1;
for(i=0;i<month;i++)
count+=days[i];
count+=1;
count%=7;
b=7-count;

20
if(b!=1 || b!=7)
while(count>0)
{
System.out.print(' '+"\t");count--;
}
for(i=1;i<7;i++)
{
while(b>0 && d<=days[month-1])
{
System.out.print(d+"\t");d++;b--;
}
b=7;
System.out.println(" ");
}
}
}

21
Output IV

22
Variable Description IV

Variable Name Variable Type Variable Method Variable Purpose

br BufferedReader dee() BufferedReader Object

i Integer dee() Loop Variable

count Integer dee() Counter

b Integer dee() Week Counter

d Integer dee() Day Counter

month Integer dee() Input Month

year Integer dee() Input Year

w String dee() Weekdays

days String[] dee() Array Soring Days

month1 String[] dee() Array Sorting Months

23
Question V

Write a program in java to calculate the factorial using a recursion.

24
Algorithm V

Step 1: Start.

Step 2: Input N.

Step 3: if (n<2) then return 1 otherwise return (n*fact(n-1)).

Step 4: End.

25
Coding V

import java.io.*;
class Factorial
{
public static void main(String args[])throws IOException //main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter no = ");
int n=Integer.parseInt(br.readLine()); //accepting no.
Factorial obj=new Factorial();
long f=obj.fact(n);
System.out.println("Factorial ="+f); //displaying factorial
}
public long fact(int n) //recursive fact()
{
if(n<2)
return 1;
else
return (n*fact(n-1));
}
}

26
Output V

27
Variable Description V

Variable Name Variable Type Variable Variable Purpose


Method

br BufferedReader main() BufferedReader Object

n Integer main() Input Number

obj Factorial main() Factorial Object

f Long main() Variable Storing Factorial

n Integer fact() Parameter in fact()

28
Question VI

Write a program in Java to display the Fibonacci Series using recursion.

29
Algorithm VI

Step 1: Start.

Step 2: Print n.

Step 3: if (n<=1) then return 1 other return (fib(n-1)+fib(n-2)).

Step 4: End.

30
Coding VI

import java.io.*;
class Fibonacci
{
public static void main(String args[])throws IOException //main function
{
Fibonacci obj=new Fibonacci();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter no of term ="); //accepting no. of terms
int n=Integer.parseInt(br.readLine());
System.out.println();
for(int i=1;i<=n;i++) //Fibonacci element display loop
{
int f=obj.fib(i);
System.out.print(f+" ");
}
}
public int fib(int n) //Recursive function fib() for calculation of Fibonacci element
{
if(n<=1)
return n;
else
return (fib(n-1) +fib(n-2));
}
}

31
Output VI

32
Variable Description VI

Variable Name Variable Type Variable Variable Purpose


Method

br BufferedReader main() BufferedReader Object

obj Fibonacci main() Fibonacci Object

n Integer main() Input Number

i Integer main() Loop Variable for Fibonacci Element

f Integer main() Fibonacci Element

n Integer fib() Parameter in fib()

33
Question VII

Write a program in Java to calculate GCD using Recursion.

34
Algorithm VII

Step 1: Start.

Step 2: Input p, q.

Step 3: if (q=0) then return p otherwise return calc(q,p%q).

Step 4: End.

35
Coding VII

import java.io.*;
class GCD
{
public static void main(String args[])throws IOException //main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the numbers = ");
int p=Integer.parseInt(br.readLine()); //accepting nos.
int q=Integer.parseInt(br.readLine());
GCD obj=new GCD();
int g=obj.calc(p,q);
System.out.println("GCD = "+g);
}
public int calc(int p,int q) //recursive function calculating GCD
{
if(q==0)
return p;
else
return calc(q,p%q);
}
}

36
Output VII

37
Variable Description VII

Variable Name Variable Type Variable Variable Purpose


Method

br BufferedReader main() BufferedReader Object

p Integer main(), calc() Input Number

q Integer main(), calc() Input Number

obj GCD main() GCD Object

g Integer main() Value Storing the GCD

38
Question VIII

Write a program in Java to display a spiral matrix.

39
Algorithm VIII

Step 1: Start.

Step 2: Input a[][].

Step 3: if p!=(int)Math.pow(l,2) go to Step 4.

Step 4: if co!=0 go to Step 5.

Step 5: re=1

Step 6: if ri=1; ri<=k1-re; ri++ then go to 7

Step 7: p++,c++

Step 8: if c==l go to Step 9

Step 9: Break.

Step 10: a[r][c]=p

Step 11: if c==l then go to Step 12

Step 12: Break.

Step 13: if dw=1 then go to Step 14

Step 14: p++,r++,a[r][c]=p

Step 15: if le=1 then go to Step 16

Step 16: p++,c--,a[r][c]=p

Step 17: if up=1 then go to Step 18

Step 18: p++,r--,a[r][c]=p

Step 19: k1=k1+2, k2=k2+2 & co++

Step 20: up++ & if up<=k2-1 then go to Step 18

Step 21: le++ & if le<=k2-1 then go to Step 16

Step 22: dw++ & if dw<=k1-1 then go to Step 14

40
Step 23: if y=0 then go to Step 24.

Step 24: if yy=0 then go to Step 25.

Step 25: Print ("\t"+a[y][yy]) and ().

Step 26: yy++ and if yy<l then go to Step 25.

Step 27: y++ and if y<l then go to Step 24.

Step 28: End.

41
Coding VIII

import java.io.*;
class Spiral
{
public static void main(String[] args)throws IOException//main function
{
int a[][],r,c,k1=2,k2=3,p=0,co=0,re=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Dimension of Matrix AxA: ");
int l=Integer.parseInt(br.readLine()); //accepting dimension of square spiral matrix
a=new int[l][l];
r=l/2;
c=r-1;
if(l%2==0)
{
System.out.println("Wrong Entry for Spiral Path.");
System.exit(0);
} /*Calculating and displaying spiral matrix*/
while(p!=(int)Math.pow(l,2))
{
if(co!=0)
re=1;
for(int ri=1;ri<=k1-re;ri++)
{
p++;
c++;
if(c==l)
break;
a[r][c]=p;
}
if(c==l)
break;
for(int dw=1;dw<=k1-1;dw++)
{
p++;
r++;
42
a[r][c]=p;
}
for(int le=1;le<=k2-1;le++)
{
p++;
c--;
a[r][c]=p;
}
for(int up=1;up<=k2-1;up++)
{
p++;
r--;
a[r][c]=p;
}
k1=k1+2;
k2=k2+2;
co++;
}
for(int y=0;y<l;y++) //Displaying matrix
{
for(int yy=0;yy<l;yy++)
System.out.print("\t"+a[y][yy]);System.out.println();System.out.println();
}
}
}

43
Output VIII

44
Variable Description VIII

Variable Name Variable Type Variable Variable Purpose


Method

br BufferedReader main() BufferedReader Object

a[] Integer[] main() Spiral Matrix

r Integer main() l/2

c Integer main() r-1

k1 Integer main() Stores 2

k2 Integer main() Stores 3

p Integer main() Loop Gate

co Integer main() Row Index

re Integer main() Column Index

l Integer main() Dimensions of the Matrix

ri Integer main() Right Side Matrix Loop Variable.

le Integer main() Left Side Matrix Loop Variable.

dw Integer main() Down Side Matrix Loop Variable.

up Integer main() Up Side Matrix Loop Variable.

y Integer main() Loop Variable

yy Integer main() Loop Variable

45
Question IX

Write a program in Java to display magic square.

46
Algorithm IX

Step 1: Start.

Step 2: arr[][]=new int[n][n],c=n/2-1,r=1,num.

Step 3: if num=1; num<=n*n; num++ then go to Step 4.

Step 4: r--, c++.

Step 5: if r==-1 go to Step 6.

Step 6: r=n-1.

Step 7: if c>n-1 then go to Step 8.

Step 8: c=0.

Step 9: if arr[r][c]!=0 then go to Step 10.

Step 10: arr[r][c]=num.

Step 11: End.

Step 12: arr[r][c]=num.

Step 13: if r==0 && c==0 then go to Step 14.

Step 14: r=n-1, c=1 & arr[r][c]=++num.

Step 15: if c==n-1 && r==0 then go to Step 16.

Step 16: arr[++r][c]=++num.

Step 17: Print ().

Step 18: if r=0 then go to Step 19.

Step 19: if c=0 then go to Step 20.

Step 20: print arr[r][c]+" " & ().

Step 21: c++ and if c<n then go to Step 20.


47
Step 22: r++ and if r<n then go to Step 19.

Step 23: End.

48
Coding IX

import java.io.*;
class Magic
{
public static void main(String args[])throws Exception //main function
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Dimension of Magical Square: ");
int n=Integer.parseInt(br.readLine()); //accepting dimensions
int arr[][]=new int[n][n],c=n/2-1,r=1,num;
for(num=1;num<=n*n;num++) //loop for finding magic square elements
{
r--;
c++;
if(r==-1)
r=n-1;
if(c>n-1)
c=0;
if(arr[r][c]!=0)
{
r=r+2;c--;
}
arr[r][c]=num;
if(r==0&&c==0)
{
r=n-1;
c=1;
arr[r][c]=++num;
}
if(c==n-1&&r==0)arr[++r][c]=++num;
}
System.out.println();
for(r=0;r<n;r++)//loop displaying magic square
{

49
for(c=0;c<n;c++)
System.out.print(arr[r][c]+" ");
System.out.println();
}
}
}

50
Output IX

51
Variable Description IX

Variable Name Variable Type Variable Method Variable Purpose

br BufferedReader main() BufferedReader Object

n Integer main() Input Dimensions

arr[] Integer [ ][] main() Magic Square Matrix

num Integer main() Loop Variable

r Integer main() Rows

c Integer main() Columns

52
Question X

Write a program in java to search in an array using Linear Search.

53
Algorithm X

Step 1: Start.

Step 2: Input a[].

Step 3: From i=0 to i<n repeat Step 4.

Step 4: Print a[i]+" ".

Step 5: flag=-1.

Step 6: From i=0 to i<n repeat Step 7.

Step 7: if (a[i] == v) then flag =i.

Step 8: if (flag=-1) then go to Step 9 otherwise go to Step 10.

Step 9: Print Not Found.

Step 10: Print v+" is Found at Position "+flag

Step 11: End.

54
Coding X

import java.io.*;
class Linear
{
int n,i;
int a[]=new int[100];
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public Linear(int nn)
{
n=nn;
}
public void input() throws IOException //function for obtaining values from user
{
System.out.println("Enter Elements:");
for(i=0;i<n;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
}
public void display() //function displaying array values
{
System.out.println();
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
}
public void search(int v) //linear search function
{
int flag=-1;
for(int i=0;i<n;i++)
{
if(a[i]==v)flag=i;
}

55
if(flag==-1)
System.out.println("Not Found");
else
System.out.println(v+" is Found at Position "+flag);
}
public static void main(String args[])throws IOException //main function
{
Linear obj = new Linear(10);
obj.input();
obj.display();
System.out.println("Enter Number to be Searched: "); //accepting the values to be searched
int v = Integer.parseInt(br.readLine());
obj.search(v);
}
}

56
Output X

57
Variable Description X

Variable Name Variable Type Variable Variable Purpose


Method

br BufferedReader input BufferedReader Object

n Integer - Array Length

i Integer - Loop Variable

a[] Integer[] - Input Array

nn Integer LinearSearch() Parameter in Constructor

v Integer search(), main() Search Element

flag Integer search() Flag

obj LinearSearch main() LinearSearch Object

58

Das könnte Ihnen auch gefallen