Professional Documents
Culture Documents
Solution:
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( );
SOLUTION:
import java.io.*;
class eng
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String x3;
System.out.println("Enter any Number(less than 99)");
int amt=Integer.parseInt(br.readLine());
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]);
}
}
SOLUTION:
class APSeries
{
private double a,d;
APSeries()
{
a = d = 0;
}
APSeries(double a,double d)
{
this.a = a;
this.d = d;
}
double nTHTerm(int n)
{
return (a+(n-1)*d);
}
double Sum(int n)
{
return (n*(a+nTHTerm(n))/2);
}
void showSeries(int n)
{
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));
}
}
SOLUTION:
class calendar
{
public void dee(int month,int year)
{
int i,count=0,b,c,d=1;
String w="SMTWTFS";
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;
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(" ");
}
}
}
SOLUTION:
import java.io.*;
class factorial
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter no =");
int n = Integer.parseInt(br.readLine());
factorial obj = new factorial();
long f = obj.fact(n);
System.out.println("factotial ="+f);
}
SOLUTION:
import java.io.*;
class fibonacci
{
public static void main(String args[]) throws IOException
{
fibonacci obj = new fibonacci();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter no of term =");
int n = Integer.parseInt(br.readLine());
System.out.println();
for(int i=1;i<=n;i++)
{
int f = obj.fib(i);
System.out.print(f+" ");
}
}
public int fib(int n)
{
if(n<=1)
return n;
else
return (fib(n-1) +fib(n-2));
}
}
SOLUTION:
import java.io.*;
class gcd
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the numbers =");
int p = Integer.parseInt(br.readLine());
int q = Integer.parseInt(br.readLine());
gcd obj = new gcd();
int g = obj.calc(p,q);
System.out.println("GCD ="+g);
}
else
return calc(q,p%q);
}
}
SOLUTION:
import java.io.*;
class spiralsm
{
public static void main(String[] args) throws IOException
{
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 =");
int l = Integer.parseInt(br.readLine());
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);
}
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++;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++)
{
for(int yy=0;yy<l;yy++)
System.out.print("\t"+a[y][yy]);
System.out.println();
System.out.println();
}
}
}
SOLUTION:
import java.io.*;
class magicalsquare
{
public static void main(String args[])throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the dimension of magical square=");
int n = Integer.parseInt(br.readLine());
int arr[][]=new int[n][n],c=n/2-1,r=1,num;
for(num=1;num<=n*n;num++)
{
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++)
{
for(c=0;c<n;c++)
System.out.print(arr[r][c]+" ");
System.out.println();
}
SOLUTION:
import java.io.*;
class linear_search
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
}
}
if(flag== -1 )
System.out.println("not found");
else
System.out.println(v+" found at position - "+flag);
}
public static void main(String args[]) throws IOException
{
linear_search obj = new linear_search(10);
obj.input();
obj.display();
System.out.println("enter no. to be searched -");
int v = Integer.parseInt(br.readLine());
obj.search(v);
}
}
class binary_search
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
if(a[m] == v)
flag = m;
else
if(a[m] < v)
l = m+1;
else
u = m-1;
if(flag== -1 )
System.out.println("not found");
else
System.out.println(v+" found at position - "+flag);
SOLUTION:
import java.io.*;
class selection_sort
{
int n,i;
int a[] = new int[100];
}
}
for(i=0;i<n-1;i++)
{
min =i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
min =j;
if(min!=i)
{
temp = a[i];
a[i] =a[min];
a[min] = temp;
}
}
}
}
}
SOLUTION:
import java.io.*;
class bubble_sort
{
int n,i;
int a[] = new int[100];
}
}
}
}
}
}
SOLUTION:
import java.io.*;
class dec_bin
{
int n,i;
int a[] = new int[100];
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
while(temp != 0)
{
a[c++] = temp % 2;
temp = temp / 2;
}
SOLUTION:
import java.io.*;
class daytodate
{
static BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
public void calc(int n, int yr)
{
int a[ ] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ;
if ( yr % 4 == 0)
a[1] =29;
int t=0,s=0;
while( t < n)
{
t =t + a[s++];
}
int d = n + a[--s] - t;
if( d == 1|| d == 21 || d == 31 )
{
System.out.println( d + "st" + m[s] + " , "+yr);
}
if( d == 2 || d == 22 )
{
System.out.println( d + "nd" + m[s] + " , "+yr);
}
if( d == 3|| d == 23 )
{
System.out.println( d + "rd" + m[s] + " , "+yr);
}
else
{
System.out.println( d + "th" + m[s] + " , "+yr);
}
}
Program :
import java.io.*;
class pattern
{
public static void main (String args[]) throws IOException
{
int i,sp,j,k,l;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string =");
String s = br.readLine();
l=s.length();
for(i=0;i<l;i++)
if(i==l/2)
System.out.println(s);
else
{
sp=Math.abs((l/2)-i);
for(j=sp;j<l/2;j++)
System.out.print(" ");
k=0;
while(k<3)
{
System.out.print(s.charAt(i));
for(j=0;j<sp-1;j++)
System.out.print(" ");
k++;
}
System.out.println(" ");
}
}
}
Solution:
import java.io.*;
class palindrome
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter the string=");
String s = br.readLine();
StringBuffer sb = new StringBuffer(s);
sb.reverse();
String rev = new String(sb);
if(s.equals(rev))
System.out.println("Palindrome " );
else
System.out.println("Not Palindrome " );
}
}
Solution :
import java.io.*;
class frequency
{
public frequency()
{
p=0;
freq=0;// initialise instance variables
}
public void count(String str)
{
int ii;
l=str.length();
System.out.print(str);
for(i=0;i<l;i++)
{
char a=str.charAt(i);
for(ii=0;ii<l;ii++)
{
char b = str.charAt(ii);
if (a==b)
freq=freq+1;
}
System.out.println(a+" occurs "+freq+" times");
freq=0;
}
}
public static void main(String args[]) throws IOException
{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter string");
String str = br.readLine();
frequency x = new frequency();
x.count(str);
}
}
Solution:
import java.util.StringTokenizer;
import java.io.*;
if(flag ==-1)
{
System.out.println("the word not found");
}
else
{
System.out.println("the word found");
}
}
}
Solution :
class decode
{
int j,i,l,c=0,y,n1;
l=name.length();
try
{
c=(int)c1 ;
}
catch(NumberFormatException e)
{}
if(n>0)
{
if((c+n)<=90)
System.out.print((char)(c+n));
else
{
c=c+n;c=c%10;
c=65+(c-1);
System.out.print((char)(c));
}
}
else if(n<0)
{
n1=Math.abs(n);
if((c-n1) >=65)
System.out.print((char) (c-n1));
else
{
if(c>65)
c=c-65;
else
c=n1;
System.out.print((char)(90-(c-1)));
}
}
else if (n==0)
{
System.out.println("no change "+name);
break;
}
}
}
}
Solution:
import java.io.*;
class Alpha
{
String str;
int l;
char c[] = new char[100];
public Alpha()
{
str = "";
l =0;
}
for(i=0;i<l-1;i++)
{
for(j=0;j<l-1-i;j++)
{
if(c[j] > c[j+1])
{
temp = c[j];
c[j] = c[j+1];
c[j+1] = temp;
}
}
}
}
Solution:
class p42
{
public static void main(String args[])
{
String a="Computer Applications";//initialising string
int z=a.length(),y,x=0,b=0;
for(y=0;y<z;y++)//loop for counting number of vowels
{
if(a.charAt(y)=='a'||a.charAt(y)=='e'||a.charAt(y)=='i'||a.charAt(y)=='o'||
a.charAt(y)=='u')
x++;
else
b++;
}
System.out.println("Number of vowels in string ="+x);
System.out.println("Number of consonants in string ="+b);
}
}
PROGRAM 23 - To create a string and count number of words.
Solution:
class p45
{
public static void main(String args[])
{
String a="Computer Applications"; //initialising string
System.out.println("The string is -"+a);
int z=a.length(),y,x=0;
for(y=0;y<z;y++)//loop for counting number of spaces
{
if(a.charAt(y)==' ')
x=x+1;
}
System.out.println("Number of words in string ="+(x+1));
}
}
Solution:
import java.io.*;
class p48
{
public static void main(String args[])
{
StringBuffer a=new StringBuffer("Computer Applications");
System.out.println("Original String -"+a);
int z=0;
for(z=0;z<a.length();z++)//loop for replacing vowels with "*"
{
if(a.charAt(z)=='a'||a.charAt(z)=='e'||a.charAt(z)=='i'||a.charAt(z)=='o'||
a.charAt(z)=='u')
a.setCharAt(z,'*');
}
System.out.println("New String -"+a);
}
}
PROGRAM 25 - To create a double-dimensional array of 4*4
subscripts.
Solution:
class p31
{
public static void main(String args[])throws IOException
{ int a[][]=new int[3][3], x,y,z;
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the array");
for(x=0;x<3;x++)//loop for reading the array
{
for(y=0;y<3;y++)
{ z=Integer.parseInt(aa.readLine());
a[x][y]=z;
}
}
System.out.println("Array -");
for(x=0;x<3;x++)//loop for printing the array
{
for(y=0;y<3;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
}
}
Solution:
class p33
{
public static void main(String args[])throws IOException
{ int a[][]=new int[5][5];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<5;x++)//loop for reading array
{
for(y=0;y<5;y++)
{ z=Integer.parseInt(aa.readLine());
a[x][y]=z;
}
}
System.out.println("Array -");
for(x=0;x<5;x++)//loop for printing array
{
for(y=0;y<5;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
for(x=0;x<5;x++)//loop for printing sum of array elements
{
for(y=0;y<5;y++)
{
Sum=Sum+a[x][y];
}
}
System.out.println("Sum of Array elements="+Sum);
}
}
Solution:
class p35
{
public static void y(int a[],int b[])
{ int c[]=new int[5];
int i;
System.out.println("Product of two arrays is-");
for(i=0;i<5;i++)//loop for finding product of the two arrays
{ c[i]=a[i]*b[i];
System.out.print(+c[i]+" ");
}
}
}
Solution:
class p39
{
public static void main(String args[])throws IOException
{
int a[][]=new int[4][4];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");//reading array
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
z=Integer.parseInt(aa.readLine());
a[x][y]=z;
}
}
System.out.println("Array -");//printing the array in matrix form
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
for(y=0;y<4;y++)
{
for(x=0;x<4;x++)
{
Sum=Sum+a[x][y];
}
System.out.println("Sum of column "+(y+1)+" is "+Sum);//printing sum of
Sum=0; //column
}
}
}
PROGRAM 29 - To find sum of diagonal of a double dimensional
array of 4*4 subscripts.
Solution:
class p40
{
public static void main(String args[])throws IOException
{
int a[][]=new int[4][4];
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
int x,y,z,Sum=0;
System.out.println("Enter the array");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
z=Integer.parseInt(aa.readLine());
a[x][y]=z;
}
}
System.out.println("Array -");
for(x=0;x<4;x++)
{
for(y=0;y<4;y++)
{
System.out.print(a[x][y]+" ");
}
System.out.print("\n");
}
y=0;
for(x=0;x<4;x++)//loop for finding sum of diagonal
{
Sum=Sum+a[x][y];
y=y+1;
}
System.out.println("Sum of diagonal is " +Sum);
Sum=0;
}
}
PROGRAM 30 - To calculate the commission of a salesman as
per the following data:
Sales Commission
>=100000 25% of sales
80000-99999 22.5% of sales
60000-79999 20% of sales
40000-59999 15% of sales
<40000 12.5% of sales
Solution:
class p14
{
public static void main(String args[])throws IOException
{
double sales,comm;
BufferedReader aa=new BufferedReader(new InputStreamReader(System.in));
System.out.println(“Enter sales”);
sales=Double.parseDouble(aa.readLine());//reading sales from the keyboard
if(sales>=100000)
comm=0.25*sales;
else
if(sales>=80000)
comm=0.225*sales;
else
if(sales>=60000)
comm=0.2*sales;
else
if(sales>=40000)
comm=0.15*sales;
else
comm=0.125*sales;
System.out.println("Commission of the employee="+comm);
}
}