Q1) Write a program to calculate the area of a circle showing the declaration of variables and
methods.
import [Link].*;
public class circle
// instance variables - replace the example below with your own
private int r;
public void setRadius(int r)
this.r=r;
public double area()
return 3.14*r*r;
public static void main(String args[])
int r;
double a;
Scanner sc=new Scanner([Link]);
[Link]("Enter Radius");
r=[Link]();
circle c=new circle();
[Link](r);
a=[Link]();
[Link]("Area of Circle :"+a);
}
}
Q2) Write a program to accept a two digit [Link] the sum of its digit to the product of [Link]
the value is equal print the message”special digit” otherwise print”not a special digit.
import [Link].*;
public class sumproduct
public static void main(String args[])
Scanner sc=new Scanner([Link]);
int num,a,s=0,p=1,k,onum;
[Link]("Enter two digit number:");
num=[Link]();
onum=num;
while(num!=0)
a=num%10;
s=s+a;
p*=a;
num/=10;
[Link] ("Sum of digits :"+s);
[Link]("product of digits :"+p);
k=s+p;
if(k==onum)
[Link]("special number");
else
[Link]("Not a special Number");
Q3) Design a class to overload a function area() as follows:
public class mansu
// instance variables - replace the example below with your own
double area(double a,double b,double c)
double s=(a+b+c)/2;
return [Link](s*(s-a)*(s-b)*(s-c));
double area(int a,int b,int height)
return (height*(a+b))/2;
public static void main(String ap[])
mansu m=new mansu();
[Link]("Area of Tringle :"+[Link](5.3,4.5,3.2));
[Link]("Area of Trapisum :"+[Link](5,4,10));
}
Q4) Define a class Telephone having the following description:
import [Link].*;
public class Telephone
// instance variables - replace the example below with your own
private int prv,pre,calls;
private String name;
private double amt,total;
/**
* Constructor for objects of class Telephone
*/
public Telephone()
// initialise instance variables
public void input()
Scanner sc=new Scanner([Link]);
[Link]("Enter Previous Reading ");
prv=[Link]();
[Link]("Enter Current Reading ");
pre=[Link]();
[Link]("name of customer :");
name=[Link]();
}
void call()
calls=pre-prv;
if(calls<=100)
amt=0;
else if(calls>100 && calls<=200)
amt=(calls-100)*90;
else if(calls>200 && calls<=400)
amt=(calls-200)*80+(100*90);
else if (calls>400)
{ amt=(calls-400)*70+(200)*80+(100*90);
amt=amt/100;
total=180+amt;
void display()
{ call();
[Link] ("Name\t\t Calls made\t\tAmount\t\tTotal Amount");
[Link](name+"\t\t"+calls+"\t\t"+amt+"\t\t"+total);
/**
* An example of a method - replace this comment with your own
* @param y a sample parameter for a method
* @return the sum of x and y
*/
public static void main(String args[])
{
Telephone t=new Telephone();
[Link]();
[Link]();
Q5) Write a program in java to input a [Link] all the alphabets of the word in ascending
order and display the new word.
public class alphabets
public static void main(String ap[])
Scanner sc=new Scanner([Link]);
String n;
String n1="";
[Link]("Enter any string");
n=[Link]();
char c;
char cha[]=[Link]();
for(int i=0;i<[Link];i++)
for( int j=i+1;j<[Link];j++)
if(cha[i]>cha[j])
c=cha[i];
cha[i]=cha[j];
cha[j]=c;
}
}
for(int i=0;i<[Link];i++)
n1+=cha[i];
// [Link](cha[i]);
[Link](n1);
Q6) Write a program in Java to accept an integer arrays from the user and sort the array Bubble sort
technique.
import [Link].*;
public class sortarray
public static void main(String args[])
int a[]=new int[10];
int n;
int i,j,t;
Scanner sc=new Scanner([Link]);
[Link]("Enter how many Numbers in an array :");
n=[Link]();
for(i=0;i<n;i++)
[Link]("Enter Array Elements");
a[i]=[Link]();
}
for ( i=0;i<n;i++)
for(j=0;j<n-1-i;j++)
if(a[j]>a[j+1])
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
for(i=0;i<n;i++)
[Link](a[i]+" ");
Q7) Using scanner class….. end with a small letter.
import [Link].*;
public class printword
public static void main(String args[])
Scanner sc=new Scanner([Link]);
String n,n2="";
[Link]("Enter any string ");
n=[Link]();
int k=0;
for(int i=0;i<[Link]();i++)
//[Link]([Link](i));
if([Link](i)==' ')
if([Link]([Link](i-1)) && [Link]([Link](k)))
n2+=[Link](k,i)+" ";
k=i+1;
if([Link]([Link](k)) && [Link]([Link]([Link]()-1)))
n2+=[Link]([Link](' ')+1,[Link]());
[Link](n2);
Q8) Write a program….. prime or not.
public class coprime
// instance variables - replace the example below with your own
int cal_hcf(int n1,int n2)
{ int n,d,e;
if(n2>n1)
n=n2;
d=n1;
else
n=n1;
d=n2;
while(d>0)
e=n%d;
n=d;
d=e;
return n;
int prime(int n)
{ int i;
for(i=2;i<n;i++)
{ if(n%i==0)
break;
}
if(i==n)
return 1;
else
return 0;
public static void main(String args[])
coprime a=new coprime();
//[Link]("hcf:"+a.cal_hcf(17,3));
int n1,n2;
Scanner sc=new Scanner([Link]);
[Link]("Enter 1st Number :");
n1=[Link]();
[Link]("Enter 2nd Number :");
n2=[Link]();
if([Link](n1)==1 && [Link](n2)==1 && a.cal_hcf(n2,n1)==1)
[Link]("Number is co-prime ");
else
[Link]("Not a co-prime number ");
}
Q9) Write a program…. Searched word.
import [Link].*;
public class findword
public static void main(String args[])
Scanner sc=new Scanner([Link]);
[Link]("Enter Sentence:");
String str=[Link]();
[Link]("Enter word to Search :");
String word=[Link]();
int l=[Link]();
int p=[Link]();
int k=0,count=0;
String chk="";
for(int i=0;i<p;i++)
if([Link](i)!=' ')
chk=chk+[Link](i);
//chk=[Link](k,i-1);
// [Link](chk);
else if([Link](i)==' ')
if([Link](word))
{ [Link](chk);
count++;
chk="";
[Link]("Word Occur :"+count + " times");
Q 10) Write a program… separate line.
import [Link].*;
public class upper
public static void main(String args[])
Scanner sc=new Scanner([Link]);
[Link]("Enter Sentence:");
String str=[Link]();
int p=[Link]();
int k=0,count=0;
String chk="";
for(int i=0;i<p;i++)
{
if([Link](i)!=' ')
chk=chk+[Link](i);
else if([Link](i)==' ')
[Link]([Link]());
chk="";
[Link]([Link]());
Q 11) Sum of series…
import [Link].*;
public class series
public static void main(String ap[])
{
int p=2;
int x,s=0;
Scanner sc=new Scanner([Link]);
[Link]("Enter value for x:");
x=[Link]();
for(int i=p;i<=20;i+=3)
{ [Link](x+":"+i+" ");
s+=[Link](x,i);
[Link]("sum is :"+s);
Q12) Print the series..
import [Link].*;
public class series2
// instance variables - replace the example below with your own
public static void main(String args[])
int s=0,i=2,s1=0,k,n;
Scanner sc=new Scanner([Link]);
[Link]("enter value for n :");
n=[Link]();
for(i=2;i<=n;i++)
s=s+i;
k=11+s;
s1=s1+k;
}
[Link]("sum is "+s1);
Q13) Write a program.. pattern.
public class pattern
public static void main(String args[])
for(int i=1;i<=5;i++)
for(int j=5;j>=i;j--)
[Link](j+" ");
[Link]();
Q14) Define a class taximeter..
import [Link].*;
public class taximeter
int taxino;
String passengerName;
int km;
public taximeter()
taxino=0;
passengerName="";
km=0;
void input()
Scanner sc=new Scanner([Link]);
[Link]("Enter taxi Number :");
taxino=[Link]();
[Link]("Enter Passenger Name :");
passengerName=[Link]();
[Link]("Enter Kilometer travel :");
km=[Link]();
int calculate()
if(km<=1)
return 25;
else if(km>1 && km<=6)
return 25+(km-1)*10;
else if(km>6 && km<=12)
return 25+(km-6)*15+5*10;
else if(km>12 && km<=20)
return 25+(km-12)*20+(6*12)+(5*10);
else
return 25+(km-20)*25+(8*20)+(6*12)+(5*10);
}
void display()
{ int r=calculate();
[Link]("Taxi Number\t Name \t\t Kilometer Travel \t\t+Bill Amount");
[Link](taxino+"\t"+passengerName+"\t\t"+km+"\t\t"+r);
// instance variables - replace the example below with your own
public static void main(String ap[])
taximeter t=new taximeter();
[Link]();
[Link]();
Q15) Write a menu… 1 or 2.
import [Link].*;
public class menudriven
// instance variables - replace the example below with your own
public static void main(String args[])
int ch;
do
Scanner sc=new Scanner([Link]);
[Link]("1 Series 1");
[Link]("2 Series 2");
[Link]("Enter Your choice");
ch=[Link]();
switch(ch)
case 1:
int n;
double s=0;
[Link]("Enter Limit for Series:");
n=[Link]();
for(int i=4;i<=n;i+=4)
s+=1/(double)i;
[Link]("sum of first Series :'"+s);
break;
case 2:
int n1,f=1;
double s1=0;
[Link]("Enter Limit for Series:");
n1=[Link]();
for(int i=1;i<=n1;i++)
f=f*i;
if(i%2!=0)
s1+=1/(double)f;
else
s1-=1/(double)f;
}
[Link]("sum of first Series :'"+s1);
break;
default:
[Link]("Wrong Choice");
break;
} while(ch<=3);
Q16) Write a program.. full stop.
import [Link].*;
public class capital
public static void main(String ap[])
Scanner sc=new Scanner([Link]);
[Link]("Enter any String :");
String s=[Link]();
int l=[Link]();
String s1="";
char c=[Link](0);
s1+=c;
for(int i=0;i<l;i++)
if([Link](i)==' ')
s1+=".";
c=[Link](i+1);
s1+=c;
[Link]([Link]());
Q17)
import [Link].*;
public class options
public static void main(String ap[])
Scanner sc=new Scanner([Link]);
int ch;
[Link]("1 Print Total digits in a string");
[Link]("2 Count number of white spaces in a string");
[Link]("3 Count Upper case Letter in a String");
[Link]("Enter Your Choice :");
ch=[Link]();
switch(ch)
case 1:
int dcount=0;
String dstring="";
[Link]("Enter any String");
sc=new Scanner([Link]);
dstring=[Link]();
for(int i=0;i<[Link]();i++)
if([Link]([Link](i)))
dcount++;
[Link]("total Number of digits :"+dcount);
break;
case 2:
int scount=0;
[Link]("Enter any String");
sc=new Scanner([Link]);
String sstring=[Link]();
for(int i=0;i<[Link]();i++)
if([Link](i)==' ')
scount++;
[Link]("total Number of spaces :"+scount);
break;
case 3:
int ucount=0;
[Link]("Enter any String");
sc=new Scanner([Link]);
String ustring=[Link]();
//[Link]();
for(int i=0;i<[Link]();i++)
if([Link]([Link](i)))
ucount++;
[Link]("total Number of Uppercase Digits :"+ucount);
Q18) Java program to find transpose of a matrix.
public class transpose
// instance variables - replace the example below with your own
public static void main(String ap[])
int a[][]={{1,2,3},
{4,5,6},
{7,8,9}};
int b[][]=new int[3][3];
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
b[i][j]=a[j][i];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
[Link](b[i][j]+"\t");
[Link]();
Q19) Write a program… reverse sentence.
import [Link].*;
public class reverse
// instance variables - replace the example below with your own
public static void main(String args[])
String str;
Scanner sc=new Scanner([Link]);
[Link]("Enter any String :");
str=[Link]();
String word[]=[Link](" ");
String str2=" ";
for(int i=0;i<[Link];i++)
str2=word[i]+' '+str2;
//[Link](word[i]);
[Link](str2);
Q20) Switch case pattern..
import [Link].*;
public class testpattern
public static void main(String ap[])
int ch;
Scanner sc=new Scanner([Link]);
[Link]("1 for First Pattern");
[Link]("2 for Second Pattern");
[Link]("Enter Your Choice:");
ch=[Link]();
switch(ch)
case 1:
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
[Link](j);}
[Link]();
break;
case 2:
for(int i=1;i<=4;i++)
for(int j=4;j>=i;j--)
[Link](j);}
[Link]();
break;
case 3:
[Link]("Wrong choice");