Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Computer Programs (2)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 39

public class Hello

void main()

System.out.println ("Hello! Welcome");

public class Hello1

System.out.println ("Good Morning");

System.out.println ("Good Night");

System.out.println ("Good Bye");

public class Calculator

void main()

{
System.out.println ("5");

System.out.println (5);

System.out.println (5+6);

System.out.println (5-6);

System.out.println (5*6);

System.out.println (5/6);

System.out.println (5%6);

public class CalculateTime

void main(int dist, int sp)

int t=dist/sp;

System.out.println("time taken = "+t+" hrs");

public class ProfitLoss

void main(int cp, int sp)

if (cp>sp)

System.out.println(" Loss ");

}
else if(sp>cp)

System.out.println(" Profit ");

else if(cp==sp)

System.out.println(" Neither profit nor loss ");

public class AreaRectangle

void main(int l, int b)

int a=l*b;

int p=2*(l+b);

System.out.println("area = "+a);

System.out.println("perimeter = "+p);

import java.io.*;

public class Percentage

void main() throws Exception

InputStreamReader read=new InputStreamReader(System.in);


BufferedReader ob=new BufferedReader(read);

int s1,s2,s3,s4,s5; double per;

System.out.println("Enter marks of 5 subjects out of 100");

s1=Integer.parseInt(ob.readLine());

s2=Integer.parseInt(ob.readLine());

s3=Integer.parseInt(ob.readLine());

s4=Integer.parseInt(ob.readLine());

s5=Integer.parseInt(ob.readLine());

per=((s1+s2+s3+s4+s5)*100)/500;

if(per>80)

System.out.println("A grade");

else if(per>=60 && per<=80)

System.out.println("B grade");

else

System.out.println("C grade");

import java.util.*;

public class Kilometers

void main()

Scanner sc = new Scanner(System.in);

System.out.println("Pl enter the name");

String n = sc.nextLine();
System.out.println("Pl enter no. of kilometers");

int k =sc.nextInt();

int bill=0;

if(k>0 && k<=1)

bill=k*25;

else if (k>1 && k<=5)

bill=1*25+(k-1)*30;

else if(k>5 && k<=15)

bill=1*25+1*30+(k-5)*45;

else if(k>15)

bill=1*25+1*30+1*45+(k-15)*50;

System.out.println("name\t\t No.of kms\t\t Bill Amt.");

System.out.println(n+"\t\t"+k+"\t\t"+bill);

public class UserCalculator

void main(int a, int b)

System.out.println ("sum ="+(a+b));

System.out.println ("diff ="+(a-b));

System.out.println ("prod ="+(a*b));


System.out.println ("quotient ="+(a/b));

System.out.println ("remainder ="+(a%b));

import java.util.*;

public class Hours

void main()

Scanner sc = new Scanner(System.in);

System.out.println("Pl enter the name ");

String n = sc.nextLine();

System.out.println("Pl enter the no. of hours ");

int h = sc.nextInt();

int bill = 0;

if (h>0 && h<=30)

bill= h*10;

else if(h>30 && h<=40)

bill = 10*30 + (h-30)*15;

else if (h>40 && h<=50)

bill= 30*10 + 10*15+(h-40)*25;

else if (h>50)
{

bill= 30*10 + 10*15+ 10*25 + (h-50)*50;

System.out.println("Name\t\t No. of hrs\t\t Bill Amt. ");

System.out.println(n+"\t\t" + h+"\t\t"+ bill);

import java.io.*;

public class Commission

void main() throws IOException

InputStreamReader read = new InputStreamReader (System.in);

BufferedReader ob= new BufferedReader (read);

int cp,sp; double c;

System.out.println("Enter cp,sp");

cp = Integer.parseInt(ob.readLine());

sp = Integer.parseInt(ob.readLine());

if(cp<sp)

c=cp+(0.1*sp);

System.out.println("Profit");

System.out.println("Commission "+c);

else if(cp>sp)

System.out.println("Loss");
}

else

System.out.println("no profit, no loss");

import java.io.*;

public class Cinema

void main() throws IOException

InputStreamReader read=new InputStreamReader(System.in);

BufferedReader ob=new BufferedReader(read);

int c,n;

System.out.println("Enter 1-lower stall, 2-upper stall, 3-balcony, 4-gold");

c=Integer.parseInt(ob.readLine());

switch(c)

case 1:

System.out.println("Enter no. of tickets");

n=Integer.parseInt(ob.readLine());

System.out.println("Price = "+(100*n));

break;

case 2:

System.out.println("Enter no. of tickets");

n=Integer.parseInt(ob.readLine());

System.out.println("Price = "+(150*n));

break;

case 3:
System.out.println("Enter no. of tickets");

n=Integer.parseInt(ob.readLine());

System.out.println("Price = "+(250*n));

break;

case 4:

System.out.println("Enter no. of tickets");

n=Integer.parseInt(ob.readLine());

System.out.println("Price = "+(350*n));

break;

default:

System.out.println("Invalid Input");

import java.io.*;

public class AreasUsingSwitch

void main() throws IOException

InputStreamReader read = new InputStreamReader (System.in);

BufferedReader in = new BufferedReader(read);

System.out.println ("1. Area of square");

System.out.println ("2. Area of rectangle");

System.out.println ("3. Area of circle");

System.out.println ("Please enter ur choice");

int ch = Integer.parseInt(in.readLine());

switch (ch)

case 1:

System.out.println ("Pl enter the value for side");

int s= Integer.parseInt(in.readLine());

System.out.println (" Area of square = "+(s*s));


break;

case 2:

System.out.println ("Pl enter the value for length");

int l= Integer.parseInt(in.readLine());

System.out.println ("Pl enter the value for breadth");

int b= Integer.parseInt(in.readLine());

System.out.println (" Area of rectangle = "+(l*b));

break;

case 3:

System.out.println ("Pl enter the value for radius");

int r= Integer.parseInt(in.readLine());

System.out.println (" Area of circle = "+(3.14*r*r));

break;

default:

System.out.println ("invalid choice");

System.out.println ("Bye");

import java.util.*;

public class UsingSwitchAsChar

void main()

Scanner sc=new Scanner (System.in);

System.out.println ("a. Add");

System.out.println ("b. Subtract");

System.out.println ("c. Multiply");

System.out.println ("d. Divide");

System.out.println ("e. Remainder");


System.out.println ("Pls enter your choice ");

char ch = sc.next().charAt (0);

switch (ch)

case 'a':

case 'A':

System.out.println ("Pl enter the nos. ");

int z= sc.nextInt();

int y = sc.nextInt();

int s=z+y;

System.out.println ("sum = "+s);

break;

case 'b':

case 'B':

System.out.println ("Pl enter the nos. ");

int x = sc.nextInt();

int w = sc.nextInt();

int d=x-w;

System.out.println ("diff = "+d);

break;

case 'c':

case 'C':

System.out.println ("Pl enter the nos. ");

int v = sc.nextInt();

int u = sc.nextInt();

int m=v*u;

System.out.println ("multiplication = "+m);

break;

case 'd':
case 'D':

System.out.println ("Pl enter the nos. ");

int t = sc.nextInt();

int l = sc.nextInt();

int o=t/l;

System.out.println ("div. = "+o);

break;

case 'e':

case 'E':

System.out.println ("Pl enter the nos. ");

int p = sc.nextInt();

int q = sc.nextInt();

int r=p%q;

System.out.println ("remainder = "+r);

break;

default:

System.out.println ("Invalid Choice ");

public class UsingLogicalOperators

void main (int a, int b, int c)

if (a==b && a==c)

System.out.println ("all are equal");

else if (a>=b && a>=c)


System.out.println (a+ " is the largest");

else if (b>=a && b>=c)

System.out.println (b+ " is the largest");

else if (c>=a && c>=b)

System.out.println (c+ " is the largest");

public class UnaryBinary

void main()

int a =5;

int c= ++a;

System.out.println("value of c = "+c);

System.out.println("value of a = "+a);

int d=a++;

System.out.println("value of d = "+d);

System.out.println("value of a = "+a);

public class UnaryBinary1

void main()

int a =5;
int c= --a;

System.out.println("value of c = "+c);

System.out.println("value of a = "+a);

int d=a--;

System.out.println("value of d = "+d);

System.out.println("value of a = "+a);

System.out.println("value of a = "+d);

public class UnaryBinary2

void main()

int x=5 , y=10 , z=14;

int k= x++ - ++y + --z;

System.out.println("value of k = "+k);

public class UnaryBinary3

void main()

int a=5 , b=9 , c=10;

int x= c++/a-b-- *c;

System.out.println("value of x = "+x);

}
public class UnaryBinary4

void main()

int p=12 , q=3 , r=6;

int x= q*--p+--r%q++;

System.out.println("value of x = "+x);

public class MathFunctions

void main()

int a = 16;

double s= Math.sqrt(a);

System.out.println(Math.sqrt(a));

System.out.println(Math.sqrt(4));

System.out.println(Math.cbrt(27));

System.out.println(Math.pow(4,4));

System.out.println(Math.min(4,8.4));

System.out.println(Math.max(4,8));

System.out.println(Math.abs(-4));

System.out.println(Math.round(4.55));

System.out.println(Math.floor(4.15));

System.out.println(Math.ceil(4.15));

}
import java.util.*;

public class MathFuntions2

void main()

Scanner sc= new Scanner (System.in);

System.out.println("Please Enter A for square and B for circle");

char ch= sc.nextLine(). charAt(0);

switch(ch)

case 'a':

case 'A':

System.out.println("Pls enter the side of the square" );

int s= sc.nextInt();

double diag=Math.sqrt(Math.pow(s,2)+Math.pow(s,2));

System.out.println("Diagonal = "+diag);

break;

case 'b':

case 'B':

System.out.println("Pls enter the radius of the circle" );

int r= sc.nextInt();

double c= 2*3.14*r;

double a= 3.14*r*r;

System.out.println("Circumference = "+c );

System.out.println("Area = "+a );

break;
default:

System.out.println("Invalid Input" );

import java.io.*;

public class Factors

public static void main() throws IOException

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

System.out.println (" enter the no. to find factors");

int i = Integer.parseInt (in.readLine());

System.out.println (" factors are ");

for (int j=1; j<=i; j++)

if( i%j==0)

System.out.println(j);

public class ArmstrongLoop

public static void main()


{

int x;

int sum=0;

for(int n=100; n<=1000; n++)

x=n;

sum=0;

while(x!=0)

int r=x%10;

sum = sum+r*r*r;

x=x/10;

if (sum==n)

System.out.println("Armstrong numbers from 100 to 1000 are "+n);

import java.io.*;

public class PrimeNo

public static void main()throws IOException

InputStreamReader read= new InputStreamReader(System.in);

BufferedReader in = new BufferedReader (read);

System.out.println("Pls enter a no.");

int n=Integer.parseInt(in.readLine());

int flag=0;

//boolean f=false;

for (int i=2;i<n;i++)


{

if(n%i==0)

flag++;

//f=true;

break;//jump statement

if (flag==0)//(f=false)//(flag==0)

System.out.println(n+"is a prime no.");

else

System.out.println(n+"is not a prime no.");

import java.io.*;

public class SimpleFor1

public static void main()throws IOException

InputStreamReader read= new InputStreamReader(System.in);

BufferedReader in= new BufferedReader(read);

System.out.println("pls enter the no");

int n= Integer.parseInt(in.readLine());

for (int i =1; i<=5; i++)

System.out.println(n+"*"+i+"="+(n*i));
}

public class SimpleFor2

public static void main()

int sum=0;

for(int i=1; i<=10; i++)

sum= sum+i;

System.out.println("sum ="+sum);

//wap to calculate and print the sum of first 10 natural nos.

import java.io.*;

public class SimpleFor3

public static void main() throws IOException

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in=new BufferedReader (read);

int sum=0; int x;

System.out.println("Enter any 10 natrural nos.");

for (int i=1; i<=10; i++)

x=Integer.parseInt(in.readLine());
sum=sum+x;

System.out.println("sum ="+sum);

//wap to accept and show sum of 10 natural nos entered by the user

import java.io.*;

public class SimpleFor4

public static void main()throws IOException

InputStreamReader read=new InputStreamReader(System.in);

BufferedReader in =new BufferedReader (read);

int i= Integer.parseInt(in.readLine());

System.out.println("factors are");

for (int j=1; j<=i; j++)

if (i%j==0)

System.out.println(j);

//wap to accept a no. and display its factors

import java.io.*;

public class PerfectNo

{
public static void main() throws IOException

InputStreamReader read= new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

System.out.println("Enter the no.");

int x =Integer.parseInt(in.readLine());

int s=0;

for(int i=1; i<x;i++)

if(x%i==0)

s=s+i;

if(s==x)

System.out.println("number entered is perfect no.");

else

System.out.println("number entered is not a perfect no.");

//wap to check if the no. entered is perfect or not

import java.io.*;

public class LargestNo

public static void main() throws IOException

{
InputStreamReader read=new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

System.out.println("enter a no.");

int x = Integer.parseInt(in.readLine());

int m =x;

System.out.println("enter 9 nos.");

for (int i=1; i<=9; i++)

x=Integer.parseInt(in.readLine());

if(x>m)

m=x;

System.out.println("The largest no. is "+m);

import java.io.*;

public class Series

public static void main() throws IOException

InputStreamReader read= new InputStreamReader(System.in);

BufferedReader in= new BufferedReader (read);

System.out.println("Enetr the value");

double a= Double.parseDouble(in.readLine());

System.out.println("enter the value for n");

int n = Integer.parseInt(in.readLine());

double s=0;
for (int i=0; i<=n; i++)

s=s+a/(i+1);

System.out.println("Sum = "+s);

public class SimpleWhile

static void main()

int n=15;

while (n!=0)

System.out.println(n);

n--;// n=n-1;

System.out.println("bye");

public class ArmstrongLoop

public static void main()

int x;

int sum=0;

for(int n=100; n<=1000; n++)

x=n;
sum=0;

while(x!=0)

int r=x%10;

sum = sum+r*r*r;

x=x/10;

if (sum==n)

System.out.println("Armstrong numbers from 100 to 1000 are "+n);

import java.io.*;

public class SimpleFor1

public static void main()throws IOException

InputStreamReader read= new InputStreamReader(System.in);

BufferedReader in= new BufferedReader(read);

System.out.println("pls enter the no");

int n= Integer.parseInt(in.readLine());

for (int i =1; i<=5; i++)

System.out.println(n+"*"+i+"="+(n*i));

public class SimpleFor2

{
public static void main()

int sum=0;

for(int i=1; i<=10; i++)

sum= sum+i;

System.out.println("sum ="+sum);

//wap to calculate and print the sum of first 10 natural nos.

import java.io.*;

public class SimpleFor3

public static void main() throws IOException

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in=new BufferedReader (read);

int sum=0; int x;

System.out.println("Enter any 10 natrural nos.");

for (int i=1; i<=10; i++)

x=Integer.parseInt(in.readLine());

sum=sum+x;

System.out.println("sum ="+sum);

}
//wap to accept and show sum of 10 natural nos entered by the user

import java.io.*;

public class SimpleFor4

public static void main()throws IOException

InputStreamReader read=new InputStreamReader(System.in);

BufferedReader in =new BufferedReader (read);

int i= Integer.parseInt(in.readLine());

System.out.println("factors are");

for (int j=1; j<=i; j++)

if (i%j==0)

System.out.println(j);

//wap to accept a no. and display its factors

import java.io.*;

public class PrimeNo

public static void main()throws IOException

InputStreamReader read= new InputStreamReader(System.in);

BufferedReader in = new BufferedReader (read);

System.out.println("Pls enter a no.");

int n=Integer.parseInt(in.readLine());
int flag=0;

//boolean f=false;

for (int i=2;i<n;i++)

if(n%i==0)

flag++;

//f=true;

break;//jump statement

if (flag==0)//(f=false)//(flag==0)

System.out.println(n+"is a prime no.");

else

System.out.println(n+"is not a prime no.");

import java.io.*;

public class PerfectNo

public static void main() throws IOException

InputStreamReader read= new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

System.out.println("Enter the no.");

int x =Integer.parseInt(in.readLine());
int s=0;

for(int i=1; i<x;i++)

if(x%i==0)

s=s+i;

if(s==x)

System.out.println("number entered is perfect no.");

else

System.out.println("number entered is not a perfect no.");

//wap to check if the no. entered is perfect or not

import java.io.*;

public class LargestNo

public static void main() throws IOException

InputStreamReader read=new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

System.out.println("enter a no.");

int x = Integer.parseInt(in.readLine());

int m =x;

System.out.println("enter 9 nos.");
for (int i=1; i<=9; i++)

x=Integer.parseInt(in.readLine());

if(x>m)

m=x;

System.out.println("The largest no. is "+m);

import java.io.*;

public class Series

public static void main() throws IOException

InputStreamReader read= new InputStreamReader(System.in);

BufferedReader in= new BufferedReader (read);

System.out.println("Enter the value");

double a= Double.parseDouble(in.readLine());

System.out.println("enter the value for n");

int n = Integer.parseInt(in.readLine());

double s=0;

for (int i=0; i<=n; i++)

s=s+a/(i+1);

System.out.println("Sum = "+s);

}
}

public class SimpleWhile

static void main()

int n=15;

while (n!=0)

System.out.println(n);

n--;// n=n-1;

System.out.println("bye");

import java.io.*;

public class Palindrome

static void main()throws IOException

InputStreamReader read= new InputStreamReader(System.in);

BufferedReader in= new BufferedReader(read);

System.out.println("Enter the no.");

int n = Integer.parseInt(in.readLine());

int x=n;

int rev=0; //extraction of digits

while (n!=0)

int r=n%10;// last digit


rev = rev*10+r;

//creating no.

n=n/10;

if(x==rev)

System.out.println("Palindrome no.");

else

System.out.println("Not palindrome no.");

import java.io.*;

public class SumOfDigits

public static void main() throws IOException

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in= new BufferedReader(read);

int sum =0; int x;

System.out.println("please enter the number");

x= Integer.parseInt(in.readLine());

while (x!=0)

int r = x%10;

sum = sum+r;

x=x/10;

}
System.out.println("Sum of Digits"+ sum);

public class Pattern1

public static void main()

for (int i=1;i<=5; i++)

for(int j=1; j<=i; j++)

System.out.println(j+" ");

System.out.println();

public class Pattern2

public static void main()

for (int i=5; i>=1; i--)

for (int j=1; j>=i; j++)

System.out.println(j+" ");

System.out.println();

}
}

import java.io.*;

public class ArmstrongNo

public static void main() throws IOException

InputStreamReader read = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader (read);

int x; int sum =0;

System.out.println("Please enter the number");

x= Integer.parseInt(in.readLine());

int t =x;

while (x!=0)

int r=x% 10;

sum=sum+r*r*r;

x=x/10;

if (sum==t)

System.out.println("It is an Armstrong Number");

else

System.out.println("It is not an Armstrong Number");

import java.util.*;

public class Factorial

static void main()


{

Scanner sc= new Scanner (System.in);

System.out.println("Enter the number");

int x = sc.nextInt();

int f=1;

for (int i=1; i<=x; i++)

f=f*i;

System.out.println("factorial of "+x+ "=" +f);

public class Factorial2

static void main (int n)

double f=1; double s=0;

for (int j=1; j<=n; j++)

f=1;

for(int i=1; i<=j; i++)

f=f*i;

s=s+1/f;

System.out.println("Factorial");

import java.util.*;

public class KrishnamorthyNo


{

static void main()

Scanner sc= new Scanner(System.in);

System.out.println("Enter the no.");

int x= sc.nextInt();

int f=1;

int s=0;

int y=x;

while(x!=0)

f=1;

int r=x%10;

for(int i=1; i<=r; i++)

f=f*i;

s=s+f;

x=x/10;

if(s==y)

System.out.println("Number entered is a spiral no.");

import java.io.*;

import java.util.*;

public class AcceptString

{
public static void main() throws IOException

InputStreamReader read= new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(read);

String y="";//declaring empty string

System.out.println("Enter the string");

y=in.readLine();

System.out.println("the string entered is");

System.out.println(y);

Scanner sc= new Scanner(System.in);

System.out.println("enter the string");

String x= sc.nextLine();

sc.nextLine();

System.out.println("enter the word");

String w= sc.next();

System.out.println("the string entered is");

System.out.println(x);

System.out.println("the word entered is");

System.out.println(w);

public class StringFns

static void main()

String x="Malayalam";

int len=x.length();//returns the length of the string

System.out.println("Length= "+len);

int c=x.indexOf('a');

//returns the firts occurence of the argument


System.out.println(c);

int y=x.lastIndexOf('a');

//last occurence of the argument

System.out.println(y);

int l=x.indexOf("lam");

System.out.println(l);

int k=x.lastIndexOf(' ');

System.out.println(k);

System.out.println(x.substring(3));

//part of the string beginning at 3rd position

//going till end of string

System.out.println(x.substring(3,7));

//returns from the 3rd position ton the 7th

//postion, excluding the 7th char

String p = x.replace('a','b');

System.out.println(p);

System.out.println(x.concat(p));

//concatetanation-joining(x+b)

System.out.println(x.equals(p));

//checks if first string is same as second string

//returns true or false

String n="malayalam";

System.out.println(x.equals(n));

System.out.println(x.equalsIgnoreCase(n));

System.out.println(x.compareTo(n));

System.out.println("Shubha".compareTo("Shubham"));

String q="Wel come ";

System.out.println(q.trim());

System.out.println(n.endsWith("yam"));

System.out.println("Item".compareTo("It"));

}
}

You might also like