Vous êtes sur la page 1sur 20

branching

import java .io.*;//from per to find grade


class monika
{public static void main(String args[])throws IOException
{BufferedReader br =new BufferedReader (new InputStreamReader(System.in));
int m ;
float p;
System.out. println("Enter Total Marks");
m =Integer.parseInt(br.readLine());
p=m /5;
System. out.println("Percentage is :"+p);
{ if(p>=80)
System .out.println("Your grade is A");
else
System.out.println ("You have different grade");
}
}
}

import java.io.*;//fine pay for giving book late


class monika
{public static void main (String args[])throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int d ,f;
{ System.out.println("Enter no of days: ");

d= Integer.parseInt(br.readLine());
if((d>=1)&&(d<=10))
System .out .println ("f="+(d*1));
else
if((d>=11)&&(d<=30))
System .out.println ("f="+(d-10)*2+10*1);
else
if(d>=31)
System .out.println("f="+(d-30)*5+50);
else
System .out.println("NON integer value entered");
}
}
}

branching

to find leap year


import java.io.*;
class maths
{public static void main (String args[])throws IOException

{BufferedReader br = new BufferedReader (new InputStreamReader(System.in));


int y;
{ System.out.println("Enter a year");
y=Double.parseInt(br.readLine());
if (y%100==0)
if(y%400==0)

System.out.println("Leap year");
else
System.out.println("not a leap year");

}
}
}

using switch calculate si and ci


import java.lang.*;
class maths
{public static void main (String args [])throws IOException
{BufferedReader br =new BufferedReader (new InputStreamReader (System.in));
Double p,r,t ,n,SI,CI;
int c;
//MENU DISPLAY //
System.out.println("1:Simple Interest");
System.out.println("2:Compound Interest");
System.out.println("Enter your choice");

c=Integer.parseInt(br.readLine());
switch(c)
{case 1:System.out.println("Enter p,r,t");
p=Double.parseDouble(br.readLine());
r=Double.parseDouble(br.readLine());
t=Double.parseDouble(br.readLine());
SI=(p*r*t)/100;
System.out.println("SI="+SI);
break;

case 2:System.out.println("Enter p,r,n");


p=Double.parseDouble(br.readLine());
r=Double.parseDouble(br.readLine());
n=Double.parseDouble(br.readLine());
CI=p*Math.pow(1+(r/100),n)-p;
System.out.println("CI="+ CI);
break;

default:System.out.println("wrong choice");
}
}
}

import java.io.*;
class maths
{public static void main (String args[])throws IOException
{BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int d,m;
{ System.out.println("Enter date of birth");
d=Integer.parseInt(br.readLine());
System.out.println("Enter month of birth");
m=Integer.parseInt(br.readLine());
if(m==9&&d>=23||m==10&&d<=22)
System.out.println("YOUR SUNSHINE IS LIBRA");
else
System.out.println("YOUR SHINEIS NOT LIBRA");
}
}
}

/*print a mathematical table using loop*/

import java .io.*;

class loops

{public static void main (String args[])throws IOException


{BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
int i,n; int f=1;
System.out.println("Enter a number");
n=Integer.parseInt(br.readLine());
{for(i=1;i<=10;i++)
{f=n*i;
System.out.println(n+"*"+i+"="+f);
}

}
}
}

check whether it is prime number or composite no

import java.io.*;

class loops
{public static void main (String args[])throws IOException
{BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int i,n;int c=0;
System.out.println("ENTER A NO");
n=Integer.parseInt(br.readLine());
{for(i=1;i<=n;i++)

if(n%i==0)
c++;
{if(c==2)
System.out.println("NO IS PRIME ");
else
System.out.println("NO IS COMPOSITE NO ");
}
}

}
}

String
// PROGRAM 1-PROGRAM TO ENTER A STRING AND PRINT THIS STRING IN REVERSE
ORDER USING substring() FUNCTION
import java.io.*;
class reverse
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a string");

String str=br.readLine();
int i,l;
l=str.length();
for(i=l-1;i>=0;i--)
{
System.out.print(str.substring(i,i+1));////
}
}
}

/* PROGRAM 2-"26 JANUARY IS CELEBRATED AS THE REPUBLIC DAY OF INDIA


* PROGRAM TO CHANGE 26 TO 15,JANUARY TO AUGUST,REPUBLIC TO
INDEPENDENCE AND FINALLY PRINT("15 AUGUST IS CELEBRATED AS THE
INDEPENDENCE DAY OF INDIA
*/
import java.io.*;
class independence
{
public static void main(String args[])
{
String Str1="26 january is celebrated as the republic day of india";
String Str2="26 january";
String Str3="republic";
int ln1=Str1.length();
int idx1=Str1.indexOf(Str2);

int ln2=Str2.length();
int idx2=Str1.indexOf(Str3);
int ln3=Str3.length();
String StrN1=Str1.substring(ln2+1,ln1);
String StrN2="august 15 "+StrN1;
int ln4=StrN2.length();
String StrN3=StrN2.substring(0,idx2-1);
String StrN4=StrN2.substring(idx2+ln3,ln4);
String FinalStr=StrN3+"independence "+StrN4;
System.out.println(FinalStr);
}
}

/* PROGRAM 3-TO ENTER A SENTENCE AND FIND HOW MUCH TIME DOES A
PARTICULAR WORD OCCURS
EG-:ENTER A SENTENCE:THE QUICK BROWN FOX JUMPS THE LAZY DOG.
* OUTPUT:- ENTER A WORD TO BE SEARCHED:THE
* SEARCHED WORD OCCURS :2 TIMES
*/
import java.io.*;
class CountWord
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter a sentence");
String Str1=br.readLine();
System.out.println("enter a word to be searched");
String wd=br.readLine();
int lnStr1=Str1.length();

int lnwd=wd.length();
char ch1,ch2;
int ctr=0;
String S=Str1.substring(0,lnwd);
if(S.equalsIgnoreCase(wd))
ctr=ctr+1;
for(int i=0;i<=lnStr1-1;i++)
{
ch1=Str1.charAt(i);
ch2=Str1.charAt(i++);
if(ch1==' '&& ch2 !=' ')
{
S=Str1.substring(i+1,i+1+lnwd);
if(S.equalsIgnoreCase(wd))
ctr=ctr+1;
}
}
System.out.println("searched word occurs:"+ctr+ " times");
}
}

/*PROGRAM 4- PROGRAM TO CHECK THAT THE ENTERED WORD IS PALINDROM OR


NOT
* -PALINDROM IS THE WORD WHICH READS THE WORD SAME FROM BACKWARDS
OR FORWARDS
* EG- LIRIL
*/
import java.io.*;
class palindrom
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,j,l;
System.out.println("enter a string");

String str=br.readLine();
l=str.length();
for(i=0,j=l-1;i<=j;i++,j--)
{
if(str.charAt(i)!=str.charAt(j))
{
break;
}
}
if(i>j)
System.out.print("palindrom string");
else
System.out.print("not a palindrom String");
}
}

/*PROGRAM 5-PROGRAM READ A NAME AND PRINT THE INITIALS OF EACH WORD
EXCEPT SIR NAME
EG. SACHIN GUPTA
OUTPUT= S.GUPTA
*/
import java.io.*;
class name
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int i,l,j,c=0,d=0;
System.out.println("enter your name");
String str=br.readLine();
str=str+" ";
l=str.length();
for(i=0;i<l;i++)
{
if(str.charAt(i)==' ')
c=c+l;
}
for(i=0;i<l;i++)
{
if(str.charAt(i)==' ')
{
d=d+1;
if(d<c)
{
System.out.println(str.charAt(i+l)+" ");
}
else
{
break;
}
}
}
for(j=i;j<1;j++)

{
System.out.println(str.charAt(i));
}

}
}

Array
// PROGRAM 1-PROGRAM TO ENTER ELEMENTS INTO AN ARRAY AND PRINT THEIR
SUM
import java.io.*;
class sum
{
public static void main(String args[])throws IOException
{
int[] mat=new int[10];
int i,sum=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter how many numbers in array");
String n=br.readLine();
String num;
int tn=Integer.parseInt(n);
for(i=0;i<tn;i++)

{
System.out.println("enter the numbers"+(i+1)+" : ");
num=br.readLine();
mat[i]=Integer.parseInt(num);
}
for(i=0;i<=tn;i++)
sum=sum+mat[i];
System.out.println("Sum of array elements is="+sum);
}
}

/* PROGRAM 2-PROGRAM TO READ TEMPERATURE FOR A NUMBER OF DAYS AND


CALCULATE THE MINIMUM,MAXIMUM,AND AVERAGE TEMPERATURE
*/
import java.io.*;
class temperature
{
public static void main(String args[])throws IOException
{
int[] temp=new int[31];
int days,i,max,min,avg;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("how many days you want to enter temperature?");
String n=br.readLine();
days=Integer.parseInt(n);
String Tem;

int ctr=1;
for(i=0;i<days;i++)
{
System.out.println("enter temp. for day"+ ctr+ ":");
Tem=br.readLine();
temp[i]=Integer.parseInt(Tem);
ctr++;
}
avg=0;
for(i=0;i<days;i++)
avg=avg+temp[i];
System.out.println("average temperature:"+avg/days);
min=100;
max=0;
for(i=0;i<=days;i++)
{
if(min>temp[i])
min=temp[i];
if(max<temp[i])
max=temp[i];
}
System.out.println("maximum temperature="+max);
System.out.println("minimum temperature="+min);
}
}

// PROGRAM 3-SORTING
// SORT ELEMENTS IN ASCENDING ORDER
// 1. BUBBLE SORT
import java.io.*;
class sort
{
public static void main(String args[])
{
int i,j,temp;
int num[]={20,18,13,24,9,5};
int ln1=num.length;
System.out.println("the array initialized values are=");
for(i=0;i<ln1;i++)
System.out.println(num[i]);
for(i=0;i<ln1-1;i++)
for(j=i+1;j<ln1;j++)
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
System.out.println("the sorted values are=");
for(i=0;i<ln1;i++)
System.out.println(num[i]);

// PROGRAM 4-SORTING
// SORT ELEMENTS IN ASCENDING ORDER
// 1. SELECTION SORT
import java.io.*;
class sort
{
public static void main(String args[])throws IOException
{
int loc,lowest,T,N,i,j,x;
int[] range=new int[101];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("enter the number of elements in your array=");
String n=br.readLine();
N=Integer.parseInt(n);
for(i=0;i<N;i++)
{
{
System.out.print("location"+i+".value:"+(i+1)+":");
n=br.readLine();
range[i]=Integer.parseInt(n);

}
for(i=0;i<=N-1;i++)
{
lowest=(range[i]);
loc=i;
for(j=i+1;j<N;j++)
{
if(lowest>range[j])
{
loc=j;
lowest=range[j];
}
}
T=range[i];
range[i]=range[loc];
range[loc]=T;
}
System.out.print("the sorted list is...\n");
for(i=0;i<N;i++)
System.out.println(range[i]);
}
}
}

/* PROGRAM 5-PROGRAM TO SEARCH AN ELEMENT AND ITS POSITION IN AN ARRAY


LINEAR SEARCH

*/
import java.io.*;
class search
{
public static void main(String args[])throws IOException
{
int[] mat=new int[101];
int i,sum=0,search,loc=0;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter how many numbers in the array?");
String n=br.readLine();
String num;
int tn=Integer.parseInt(n);
for(i=0;i<tn;i++)
{
System.out.println("enter the number"+(i+1)+":");
num=br.readLine();
mat[i]=Integer.parseInt(num);
}
System.out.println("enter the element to search");
String find=br.readLine();
search=Integer.parseInt(find);
for(i=0;i<tn;i++)
{
if(mat[i]==search)
{

loc=i+1;
}
}
if(loc>0)
System.out.println("the number" + search + "found at index:"+loc);
else
System.out.println("the number" + search + "does not found ");
}
}

Vous aimerez peut-être aussi