//Program to calculate area of circle using SWITCH statement
import java.util.*;
class switch1
{
public static void main(String args[])
{
float r,d,area;
int n;
System.out.println(" You want to calculate area of circle using");
System.out.println(" 1.Radius \n 2.Diameter");
System.out.println(" Enter your choice:");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
switch(n)
{
case 1:
{
System.out.println(" Enter radius:");
r=obj.nextFloat();
System.out.println(" Area of circle = "+3.14*2*r);
break;
}
case 2:
{
System.out.println(" Enter diameter:");
d=obj.nextFloat();
System.out.println(" Area of circle = "+3.14*d);
break;
}
default:
System.out.println(" Invalid choice ");
}
}
}
A similar program to this
Java Program to calculate area of circle using if-else statement
11 September 2018
Java Program to calculate area of circle using SWITCH statement
08 September 2018
Java Program to find word count in a given text
//Program to find word count in a given text
import java.util.*;The Output will be similar to this
class wcount
{
public static void main(String args[])
{
String s;
int i,w=1;
System.out.println(" Enter text : ");
Scanner obj=new Scanner(System.in);
s=obj.nextLine();
char a[]=s.toCharArray();
if(a.length==0)
w=0;
else
{
for(i=0;i<a.length;i++)
{
if(a[i]==' '||a[i]=='\0')
w++;
}
}
System.out.println("\n Word count is : "+w);
System.out.println();
}
}
when you don't enter anything
Java Program to find frequency count of letters in a given text
//Program to find frequency count of letters in a given text
import java.util.*;The Output will be similar to this
class fcount
{
public static void main(String args[])
{
char ch;
String s;
int i,f=0;
System.out.println(" Enter a string : ");
Scanner obj=new Scanner(System.in);
s=obj.nextLine();
char a[]=s.toCharArray();
System.out.println("Enter a character to find its frequency : ");
ch=obj.next().charAt(0);
for(i=0;i<a.length;i++)
{
if(ch==a[i])
f++;
}
System.out.println("\n Frequency count of "+ch+" is : "+f);
System.out.println();
}
}
when you enter a character which is not entered in the string
Similar program:-
Java Program to find word count in a given text
Java Program to check given string is palindrome (or) not.
//Program to check given string is palindrome (or) not.
import java.util.*;
class palindrome
{
public static void main(String args[])
{
String s;
Scanner in=new Scanner(System.in);
System.out.println(" Enter your text: ");
s=in.nextLine();
char a[]=s.toCharArray();//converting string to array.
int i=0,is_palin=1,j=a.length;//j stores the length of array.
for(i=0,j=j-1;i<j;i++,j--)
{
if(a[i]!=a[j])
{
is_palin=0;
break;
}
}
if(is_palin==1)
{
System.out.println("It is palindrome");
}
else
{
System.out.println(" It is not palindrome");
}
}
}
The Output will be similar to this
when you enter WELCOME and MADAM
Java Program to sort a list of names in Descending order
//Program to sort a list of names in Descending order
import java.util.*;The output will be similar to this
class Descendnames
{
public static void main(String args[])
{
int n,i,j;
System.out.println(" How many names you want to sort in Descending order:");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
String temp,a[]=new String[n];//Declaring array of Strings
Scanner obj1=new Scanner(System.in);
System.out.println(" Enter names");
for(i=0;i<n;i++)
{
a[i]=obj1.nextLine();
}
System.out.println(" Unsorted names : ");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i].compareTo(a[j])<0) //compareTo() method is used for comparing two //strings lexicographically.
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("\n Sorted names in Descending order ");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println( );
}
}
Java Program to sort a list of names in ascending order
//Program to sort a list of names in ascending order
import java.util.*;The output will be similar to this:
class Ascendnames
{
public static void main(String args[])
{
int n,i,j;
System.out.println(" How many names you want to sort in Ascending order:");
Scanner obj=new Scanner(System.in);
n=obj.nextInt();
String temp,a[]=new String[n];//Declaring array of Strings
Scanner obj1=new Scanner(System.in);
System.out.println(" Enter names");
for(i=0;i<n;i++)
{
a[i]=obj1.nextLine();
}
System.out.println(" Unsorted names : ");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (a[i].compareTo(a[j])>0) //compareTo() method is used for comparing two //strings lexicographically.
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.println("\n Sorted names in ascending order ");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
System.out.println( );
}
}
Java Program to illustrate method overloading
//Program to illustrate method overloading
import java.util.*;
class overload
{
public static void print(int a,int b)
{
System.out.println("Printing int values");
System.out.println("Before swapping ");
System.out.println("\t a = "+a+" b = "+b);
int temp;
temp=a;
a=b;
b=temp;
System.out.println("After swapping ");
System.out.println("\t a = "+a+" b = "+b);
}
public static void print(char a,char b)
{
System.out.println("Printing char values");
System.out.println("Before swapping ");
System.out.println("\t a = "+a+" b = "+b);
char temp;
temp=a;
a=b;
b=temp;
System.out.println("After swapping ");
System.out.println("\t a = "+a+" b = "+b);
}
public static void print(double a,double b)
{
System.out.println("Printing real values");
System.out.println("Before swapping ");
System.out.println("\t a = "+a+" b = "+b);
double temp;
temp=a;
a=b;
b=temp;
System.out.println("After swapping ");
System.out.println("\t a = "+a+" b = "+b);
}
public static void main(String args[])
{
System.out.println(" ");
print(10,20);
print('X','Y');
print(10.2,12.5);
}
}
Subscribe to:
Posts (Atom)
To convert from Upper case to lowercase(A to a)
//converting from Upper to lower(A to a) import java.util.*; class case { public static void main(String args[]) { System.out.pr...
-
//Program to calculate area of circle using SWITCH statement import java.util.*; class switch1 { public static void main(String a...
-
Perfect number : is a positive integers which is equal to the sum of its proper positive divisors, that is, the sum of its positive...
-
//Program to calculate area of circle using if-else statement import java.util.*; class areaofc { public static void main(String ...





