Você está na página 1de 41

1) Wap to display an entered number in words

import java.io.*; class Num2Words { public static void main(String args[])throws IOException//main function { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter any Number(less than 99)"); int amt=Integer.parseInt(br.readLine());//accepting number int z,g; int z,g; String x[ ]={" ","Ten","Eleven","Twelve","Thirteen","Fourteen","Fifteen","Sixteen","Seventeen","Eig hteen","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;//finding the number in words g=amt/10; if(g!=1) System.out.println(x2[g-1]+" "+x1[z]); else System.out.println(x[amt-9]); } }

Variable description Variable z g x[] x1[] x2[] amt Data Type int int string string string int Description amt%10 amt%10 Storing number in words Storing number in words Storing number in words Inputted number

Output
Enter any Number(less than 99) Input:- 88 Output:- Eighty Eight

2) Wap to display ap series and its sum


import java.io.*; class APSeries { private double a,d; APSeries()//default constructor { a = d = 0; } APSeries(double a,double d) //parameterized constructor { this.a = a; this.d = d; } double nTHTerm(int n)//final AP term { return (a+(n-1)*d); } double Sum(int n)//function calculating sum { return (n*(a+nTHTerm(n))/2); } void showSeries(int n)//displaying AP Series { 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)); } void main()throws IOException//main function

{ BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter 1st term"); a=Integer.parseInt(br.readLine());//accepting 1st term System.out.println("Enter Common difference"); d=Integer.parseInt(br.readLine());//accepting common difference System.out.println("Enter no.of terms"); int n=Integer.parseInt(br.readLine()); //accepting no. of terms nTHTerm(n); Sum(n); showSeries(n); }}

Variable description
Variable Data Type Description

a d n i

int int int int

1st term Common difference Total terms To run the loop

Output
Enter 1st term 21 Enter Common difference 5 Enter no.of terms 8 Series 21.0 26.0 31.0 36.0 41.0 46.0 51.0 56.0 Sum : 308.0

3)Wap to display calendar of any month of any year


import java.io.*; class Calendar { public void dee()throws IOException//dee() function { int i,count=0,b,d=1; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter month");//accepting month and year int month=Integer.parseInt(br.readLine()); System.out.println("Enter Year"); int year=Integer.parseInt(br.readLine());/* Computing and displaying calendar*/ 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","Sept ember","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(" "); } } }

Variable description
Variable i count b d month year w days month1 Data Type int int int int int int String String String Description To run the loop Counter variable Week counter Day counter Input Month Input Year Week days Array storing days Array storing months

Output Enter month 6 Enter Year 2012 =============The Calendar of June 2012is =============== S 3 10 M 4 11 T 5 12 W 6 13 T 7 14 F 1 8 15 S 2 9 16

17 24

18 25

19 26

20 27

21 28

22 29

23 30

4) Wap to calculate GCD using recursion


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); } public int calc(int p,int q) { if(q==0) return p; else return calc(q,p%q); } }

Variable description
Variable Data Type Description

p q

int int

Inputted number Inputted number

int

Variable storing the GCD

Output enter the numbers = 176 56 GCD =8

5) Wap to insert values in a spiral matrix and display that


class spiral_matrix { public static void main( ) { int m[][] = new int [4][4]; int n=1, s=4, c=1, i=0, j=0; while(n>=1) { do { m[i][j] = c++; n++; j++; } while(n<s); n = 0; do { m[i][j] = c++; n++; i++; } while(n<s-1); n = 0; do { m[i][j]=c++; n++; j--; } while(n<s-1);

n = -1; do { m[i][j] = c++; n++; i--; } while(n<s-2); n= n - 2; } for(i=0; i<s; i++) { for(j=0; j<s; j++) { System.out.print(m[i][j] + " "); } System.out.println(); } } }

Variable description
Variable Data Type Description

m[] n

int int

To store the matrix To store a value in spiral matrix To store some value Counter variable Matrix for rows Matrix for columns

s c i j

int int int int

Output 1 2 3 4 12 0 0 5

11 0 0 6 10 9 8 7

6) Wap to display magical square


import java.io.*; class MagicSquare { public static void main(String args[])throws Exception//main function { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the dimension of magical square="); int n = Integer.parseInt(br.readLine());//accepting dimensions int arr[ ][ ]=new int[n][n],c=n/2-1,r=1,num; for(num=1;num<=n*n;num++)//loop for finding magic square elements { 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++)//loop displaying magic square { for(c=0;c<n;c++) System.out.print(arr[r][c]+" "); System.out.println(); } } }

Variable description
Variable Data Type Description

n arr num

int int int

Input Dimensions Magic square matrix Loop variable for magic square

r c

int int

Variable for rows Variable for columns

Output enter the dimension of magical square= 11 68 81 94 107 120 1 14 27 40 53 66 80 93 106 119 11 13 26 39 52 65 67 92 105 118 10 12 25 38 51 64 77 79 104 117 9 22 24 37 50 63 76 78 91 116 8 21 23 36 49 62 75 88 90 103 7 20 33 35 48 61 74 87 89 102 115 19 32 34 47 60 73 86 99 101 114 6 31 44 46 59 72 85 98 100 113 5 18

43 45 58 71 84 97 110 112 4 17 30 55 57 70 83 96 109 111 3 16 29 42 56 69 82 95 108 121 2 15 28 41 54

7) Wap to search an array using binary search


import java.io.*; class BinarySearch { int n,i; int a[] = new int[100]; static BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); public BinarySearch(int nn)//default constructor { n=nn; } public void input() throws IOException//function accepting array elements { System.out.println("enter elements"); for(i=0;i<n;i++) { a[i] = Integer.parseInt(br.readLine()); } } public void display()//displaying array elements { System.out.println(); for(i=0;i<n;i++) { System.out.print(a[i]+" "); } } public void search(int v)//function to search array elements using binary search technique { int l=0; int u = n-1; int m; int flag=-1;

while( l<=u && flag == -1) { m = (l+u)/2; 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); } public static void main(String args[]) throws IOException//main function { BinarySearch obj = new BinarySearch(10); obj.input(); obj.display(); System.out.println("enter no. to be searched -"); int v = Integer.parseInt(br.readLine());//accepting integer to be searched by Binary search obj.search(v); } }

Variable description
Variable n i a[ ] nn v flag l u m Data Type int int int int int int int int int Description Array length To run the loop Input array Parameter in constructor Search element Flag value Lower element Upper element Middle Index

Output enter elements 16 15 2

5 8 74 20 32 54 587 16 15 2 5 8 74 20 32 54 587 enter no. to be searched 23 not found

8)Wap to sort an array using selection sort


import java.io.*; class SelectionSort { int n,i; int a[] = new int[100]; public SelectionSort(int nn)//parameterized constructor { n=nn; } public void input() throws IOException//function accepting array elements { BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter elements"); for(i=0;i<n;i++) { a[i] = Integer.parseInt(br.readLine()); } } public void display()//function displaying array elements { System.out.println(); for(i=0;i<n;i++) { System.out.print(a[i]+" "); } } public void sort()//function sorting array elements using selection sort technique { int j,temp,min; 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; } } } public static void main(String args[]) throws IOException//main function { SelectionSort x = new SelectionSort(5); x.input(); System.out.print("Before sorting - "); x.display(); System.out.print("After sorting - "); x.sort(); x.display(); } }

Variable description
Variable Data Type Description

n i a[] nn j temp

int int int int int int

Length of the array To run the loop Inputted array Parameter in constructor Sort index Temporary storage

min

int

Minimum value

Output enter elements 2 8

5 12 10 Before sorting 2 8 5 12 10 After sorting 2 5 8 10 12

9) Wap to sort an array using bubble sort


import java.io.*; class BubbleSort { int n,i; int a[] = new int[100]; public BubbleSort(int nn)//parameterized constructor { n=nn; } public void input() throws IOException//function accepting array elements { BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter elements"); for(i=0;i<n;i++) { a[i] = Integer.parseInt(br.readLine()); } } public void display()//function displaying array elements { System.out.println(); for(i=0;i<n;i++) { System.out.print(a[i]+" "); } } public void sort() //function sorting array elements using Bubble Sort technique { int j,temp; for(i=0 ; i<n-1 ; i++) { for(j=0 ; j<n-1-i ; j++) { if(a[j] > a[j+1]) {

temp = a[j]; a[j] =a[j+1]; a[j+1] = temp; } } } }

public static void main(String args[]) throws IOException//main function { BubbleSort x = new BubbleSort(5); x.input(); System.out.print("Before sorting - "); x.display(); System.out.print("After sorting - "); x.sort(); x.display(); } }

Variable description
Variable Data Type Description

n i a[] nn j temp

int int int int int int

Length of the array To run the loop Inputted array Parameter in constructor Sort index Temporary storage

Output enter elements 22 26 14

85 56 Before sorting 22 26 14 85 56 After sorting 14 22 26 56 85

10)Wap to convert a decimal no into its binary equivalent


import java.io.*; class Dec2Bin { int n,i; int a[] = new int[100]; static BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); public Dec2Bin(int nn)//parameterized contructor { n=nn; } public void dectobin(int no)//function converting decimalto binary number { int c = 0; int temp = no; while(temp != 0) { a[c++] = temp % 2; temp = temp / 2; } System.out.println("Binary eq. of "+no+" = "); for( i = c-1 ; i>=0 ; i--)//Displaying binary number System.out.print( a[ i ] ); } public static void main(String args[])throws IOException // main function { Dec2Bin obj = new Dec2Bin(30); System.out.println("Enter decimal no"); int no=Integer.parseInt(br.readLine()); obj.dectobin(no); } }

Variable description
Variable Data Type Description

n i a[ ] nn no temp c

int int int int int int int

Length of the array To run the loop Array storing binary number Parameter in constructor Inputted number Temporary storage Counter variable

Output Enter decimal no 26 Binary eq. of 26 = 11010

11)Wap to display date from its entered no of days


import java.io.*; class Day2Date { static BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); public void calc(int n, int yr)//function to calculate date { int a[ ] = { 31,28,31,30,31,30,31,31,30,31,30,31 } ; String m[ ] = { "Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" } ; if ( yr % 4 == 0) a[1] =29; int t=0,s=0; while( t < n)//loop calculating date { 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); } } public static void main(String args[]) throws IOException//main function

{ Day2Date obj = new Day2Date(); System.out.println( "Enter day no = "); //accepting day no. int n = Integer.parseInt(br.readLine()); System.out.println( "Enter year = ");//accepting year int yr = Integer.parseInt(br.readLine()); obj.calc(n,yr); }}

Variable description
Variable Data Type Description

n yr a[] m[] t s d

int int int int int int int

Day number Year Array storing day Array storing month Array index Array index Calculating day to date

Output Enter day no = 123 Enter year = 2009 Output: 3rdMay , 2009

12)Wap to create the following pattern: I N INDIA I A


public class pattern { public static void main( ) { char i; String s="INDIA"; for(i=0; i<s.length( ); i++) { if(i==2) System.out.println(("I N D I A")+" "); else System.out.println(" "+" "+" "+" "+s.charAt(i)); } } }

Variable description

Variable

Data Type

Description

i s

int String

To run the loop Entered String INDIA

13)WAP to display the frequency of each character in an entered string


import java.io.*; class Frequency { private int i,a1,l,p,j,freq; public Frequency( )//default constructor { p=0; freq=0; // initialise instance variables } public void count(String str)//counting character frquency { 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//main function { BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter string"); String str = br.readLine(); Frequency x = new Frequency(); x.count(str); } } }

Variable description
Variable Data Type Description

i a1 l p freq ii a b str

int int int int int int char char String

To run the loop Instance variable To calculate the length of string Instance variable Frequency of characters To run the loop Character at index i) Character at index ii) Inputted String

Output
enter string Bastab Dey Output:- Bastab Dey B occurs 1 times a occurs 2 times s occurs 1 times t occurs 1 times a occurs 2 times b occurs 1 times occurs 1 times D occurs 1 times e occurs 1 times y occurs 1 times

14)Wap to find a word in an entered string


import java.util.*; import java.io.*; public class WordSearch { public static void main(String[] args) throws IOException//main function { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter the string="); String s = br.readLine();//accepting string StringTokenizer st = new StringTokenizer(s," ");//StringTokenizer initialization System.out.println("enter the word to be searched ="); String look = br.readLine(); int flag = -1; while(st.hasMoreElements())//searching for word { if(look.equals(st.nextElement())) flag =1; } if(flag ==-1) { System.out.println("The word not found"); // displaying the result } else { System.out.println("Word found"); } } }

Variable description
Variable s look flag Data Type String String char[ ] Description Inputted string To find the certain word in the inputted string To store a value

Output enter the string=


My name is Bastab Dey

enter the word to be searched =


name

Word found

15)Wap to decode the entered string


class decode { public static void main(String s, int d) { int i, a; char ch; for(i=0 ; i<s.length() ; i++) { ch=s.charAt(i); a=(ch+d); if(a>90) { System.out.print((char)(a-26)); } else if(a>=65 && a<=90) { System.out.print((char)(a)); } else if(a<65) { System.out.print((char)(a+26)); } else { System.out.print(ch); } } } }

Variable description
Variable Data Type Description

i a ch

int int char

To run the loop To calculate range To calculate the last index of the string

s d

String int

To enter the string To decode the string

Output In the method call of decode.main( ) enter the string(s) and the decoding value. Suppose entered string is COMPUTER and the decoding value is -5. Hence output, XPNQVUFS Suppose entered string is COMPUTER and the decoding value is 5. Hence output, Hence output, HZX[`_P]

16)Wap to display the entered string in alphabetical order


import java.io.*; class Alpha { String str; int l; char c[] = new char[100]; public Alpha()//Alpha() constructor { str = ""; l =0; } public void readword() throws IOException//function to read input string { System.out.println("enter word - "); BufferedReader br =new BufferedReader(new InputStreamReader(System.in)); str = br.readLine(); l = str.length(); } public void arrange()//function to arrange string in ascending order { int i,j; char temp; for(i=0;i<l;i++) { c[i]= str.charAt(i); } for(i=0;i<l-1;i++)//loops for swapping of characters { 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; } }

} } public void display()//function to display the rearranged string { System.out.println(); for(int i=0;i<l;i++) { System.out.print(c[i]); } } public static void main(String args[]) throws IOException//main function { Alpha obj = new Alpha(); obj.readword(); obj.arrange(); obj.display(); } }

Variable description
Variable str l c i j temp Data Type String int char[ ] int int char Description Inputted string To calculate string length Character array To run the loop To run the loop Temporary storage

Output enter word basketball Output:aabbekllst

17)Wap to create a string and count number of words


import java.io.*; class countNoOfWords { public static void main(String args[])throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter Sentence"); String a=br.readLine();//accepting 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));//displaying result } }

Variable description

Variable a z y x

Data Type String int int int

Description To enter a sentence To calculate the length of the entered string To run the loop Incrementing the value

Output

Enter any Number(less than 99) 85 Eighty Five

18)Wap to create a string and replace all vowels with *


import java.io.*; class vowel_star { public static void main(String args[])throws IOException//main function { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a String"); StringBuffer a=new StringBuffer(br.readLine());//accepting a string 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.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);//displaying the result } }

Variable description
Variable a z Data Type String Buffer int Description To store the string To run the loop and to replace all the vowels with *.

Output Enter a String you did not gain a percentage

Original String -you did not gain a percentage New String -y** d*d n*t g**n * p*rc*nt*g*

19)Wap to create a DDA with 4*4 subscripts


import java.io.*; public class dda_4x4 { public static void main(String args[])throws IOException { BufferedReader ob=new BufferedReader(new InputStreamReader(System.in)); int i,j; int m[][]=new int[4][4]; System.out.println("Enter the numbers one by one"); for(i=0; i<4; i++) { for(j=0; j<4; j++) { m[i][j]=Integer.parseInt(ob.readLine()); } } System.out.println("The elements in the cells are:"); for(i=0; i<4; i++) { for(j=0; j<4; j++) { System.out.print(m[i][j]+" "); } System.out.println(); } } }

Variable description
Variable Data Type Description

i j m[ ]

int int int

To run the loop To run the loop To store the elements of the cells and display it

Output Enter the numbers one by one 20 25 26 21 24 23 27 32 14 56 89 90 23 21 74 25 The elements in the cells are: 20 25 26 21 24 23 27 32 14 56 89 90 23 21 74 25

20)Wap to generate sum of all elements of a DDA of 5*5 subscripts


import java.io.*; class MatrixSum { public static void main(String args[])throws IOException//main function { 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());//accepting array element a[x][y]=z; } } System.out.println("Array:"); for(x=0;x<5;x++) { for(y=0;y<5;y++) { System.out.print(a[x][y]+" "); } System.out.print("\n"); } for(x=0;x<5;x++) { for(y=0;y<5;y++) { sum=sum+a[x][y]; } } System.out.println("Sum of array elements="+ sum); // displaying sum } }

Variable description
Variable Data Type Description

a x y z sum

int int int int int

Input array Run the loop Run the loop Input element Sum of all elements

Output
Enter the array 46 46 234 67 84 52 12 45 68 74 13 65 74 96 100 23 74 56 64 33 34 10 23 69 98 Array: 46 46 234 67 84 52 12 45 68 74 13 65 74 96 100 23 74 56 64 33 34 10 23 69 98 Sum of array elements=1560

3)

4)

21)Wap to generate the product of two arrays of 5 subscripts as a 3rd array

import java.io.*; public class arr_product { public static void main(String args[])throws IOException { BufferedReader ob=new BufferedReader (new InputStreamReader(System.in)); int i; int a[]=new int[5]; int b[]=new int[5]; int c[]=0; for(i=0; i<5; i++) { a[i]= Integer.parseInt(ob.readLine()); b[i]= Integer.parseInt(ob.readLine()); } for(i=0; i<5; i++) { c[i]=a[i]*b[i]; } for(i=0; i<5; i++) System.out.println(c[i]); } }

Variable description
Variable Data Type Description

a[ ] b[ ] c[ ] i

int int int int

To store 1st array To store 2nd array To calculate the product of two arrays To run the loop

22)WAP to find sum of each column of a DDA


import java.io.*; class ColoumnSum { public static void main(String args[])throws IOException//main function { 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-"); 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 column sum=0; }}}

Variable description
Variable Data Type Description

a x y z sum

int int int int int

Input array Run the loop Run the loop Input element Sum of all elements

Output
Enter the array 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Array10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 sum of column1 is 64 sum of column2 is 68 sum of column3 is 72 sum of column4 is 76

23)Wap to print the series:- 2 , 9 , 28 , 65 , 126.


public class series { public static void main(int n) { int i; for(i=1; i<=n; i++) System.out.print((int)Math.pow(i,3)+1+","); } }

Variable description
Variable n i Data Type int int Description Entered range To run the loop

Output In the method call box enter the value of n. Suppose entered value of n is 7. Then, output:2, 9, 28, 65, 126, 217, 344, 513,

24)Wap to find the grade of steel samples considering the following conditions:I. Tensile strength>=700 kgf/cm^2 II. Rockwell hardness>=200 III. Carbon content <=6% When conditions I), II), III) are satisfied then the grade is A When conditions I) and II) are satisfied then the grade is B When conditions I) and III) are satisfied then the grade is C When conditions II) and III) are satisfied then the grade is D When conditions I) or II) or III) are satisfied then the grade is E, otherwise grade F
import java.io.*; public class steel_sample { public static void main(String args[])throws IOException { int rh,ts,cc; BufferedReader ob=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the tensile strength in kgf/cm^2"); ts=Integer.parseInt(ob.readLine()); System.out.println("Enter the rockwell hardness"); rh=Integer.parseInt(ob.readLine()); System.out.println("Enter the carbon content in percentage"); cc=Integer.parseInt(ob.readLine()); if(ts>=700 && rh>=200 && cc<=6) System.out.println(" GRADE A"); else if(ts>=700 && rh>=200) System.out.println(" GRADE B"); else if(ts>=700 && cc<=6) System.out.println(" GRADE C"); else if(rh>=200 && cc<=6) System.out.println(" GRADE D"); else if(ts>=700 || rh>=200 || cc<=6) System.out.println(" GRADE E"); else System.out.println(" GRADE F"); }}

Variable description
Variable Data Type Description

rh ts cc

int int int

To store the rockwell hardness To store the tensile strength To store the carbon content in percentage

Output
Enter the tensile strength in kgf/cm^2 700 Enter the rockwell hardness 200 Enter the carbon content in percentage 6 Output:GRADE A

25) Wap to create pascals triangle

import java.io.*; class Pascal { public void pascalw()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a no."); int n=Integer.parseInt(br.readLine()); 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( ); for (int j=i+1; j>0; j--) pas[j]=pas[j]+pas[j-1]; } } }

Variable description
Variable Data Type Description

n pas[] i j

int int int int

Entered no Pass value Run the loop Run the loop

Output
Enter a no. Input:- 5 Output:1 11 121 1331 14641

Você também pode gostar