Download as pdf
Download as pdf
You are on page 1of 18
VUE YUU SULLY UUUUUUUUUU UE LUCE EEE E EY SECTIONS 1. CONDITIONAL PROGRAMMING 1. Write a program to input two numbers and print the greater one. Public class greater { public void find_great(int x,inty) _// method with input { if (oy) 1 condition System.out.print(“greater= “4x); else ‘System.out.print(“greater= “+y); } /Lend of class 2, Write a program to input three numbers and print the greatest one. public class greatest { public void find_great(int x, intyy, int 2)//method with input int g; e=%; if (ey) [1 condition By; if(g<2) 1 condition 2; System,out.print(“greatest number= “+g); } } Mend of class 3. Write a program to input an integer and print the corresponding day of the week. public class days { public void weekday(int x) 7/ method with input { switch(x) // condition { case: System.out print("MONDAY"); break; case 2: System.out.print("TUESDAY”); ————————————— ‘TBH, Department of ICT, The Assam Valley School- 2019 Page 31 break; case3: System.out print("WEDNESDAY”)}; break; case4: System.out print( “THURSDAY"}; break; case: ‘System.out print{"FRIDAY"); break; case 6: System.out print(“SATURDAY"); break; case7: ‘System.out.print{“SUNDAY"); break; default: ‘System.out print(“Wrong choice!!”); } Mend of switch : //end of class a 7 The Assam Valley School- TBH, Department of IC’ Page 32 4. Write a program to print the first 10 integers using a while loop. public class integers public void numbers() { int p; p=1) while(p<=10) //loop begins { ‘System.out.print(p); pH } //oop ends } ) //end of class 5. Write a program to print the first 10 integers using do..while loop. public class integers { public void numbers() { int p; p=4; do /oop begins { ‘System.out.print(p); pH; } while(p<=10) ; /floop ends } } //end of class 6. Write a program to print the first 10 integers using a for loop. public class integers { public void numbers(), { intp; (for p =1; p<=10; p++) /Moop System.out.printin(p); } } 7. Write a program to print the serles of even numbers up to 20. public class even { public void numbers() TBH, Department of ICT, The Assam Valley School- 2019. 2. ITERATIVE (LOOP-BASED) PROGRAMMING Page 33 2. ITERATIVE (LOOP-BASED) PROGRAMMING 4 Write a program to print the first 10 integers using a while loop. public class integers { public void numbers() { int p; peu while(p<=20) {loop begins { System.out.print(p); ptt; ) Mop ends } } //end of class S. Writea program to print the first 10 integers using do,.while loop. public class integers public void numbers() { //oop begins { System.out.print(p); pH } while(p<=10) ; Toop ends } } // end of class 6. Write a program to print the first 10 integers using a for loop. public class integers { public void numbers() { int p; (for p =1; p<=10; p++) Moop System.out printin(p); } } 7. Write a program to print the series of even numbers up to 20. public class even { public void numbers() ‘TBH, Department of ICT, The Assam Valley School: 2019 Page 33 int p; for (p=2; paw20; p+=2) Moop System.out printin(p); 2. write a program to print the series of odd numbers up to 19, public class odd { public void numbers() { int p; for (p =1; p<=19; p+=2) hoop System.out.printin(p); } } 9. Write a program to print the first 10 multiples of 5. public class multiples { public void numbers() { int p, int mn; (for p =1; p<=10; p++) //loop { ma5*p; System.out.printin(m); ) } } Hfend of class 10. Write a program to print the first 10 multiples of 5 in the reverse order. public class rev_multiples { public void numbers() { int p, int m; (for p =10; p>=1; p-) {loop { m5"; System.out.printin(m); : J J Hend of class Re TBH, Department of ICT, The Assam Valley School- 2019 Page 34 11. Write a program to print the first n multiples of x. Public class multiples Public void numbers(int x, intn) _// method with input { int p, int m; (for { m=x*p; System.out.printin(m); pt) //oop } } } /lend of class 12. Writea program to print the factorial of n. public class factorial { public void numbers(int n) // method with input { intp, int; f=; p<=n; p++) Joop aye f System.out.print(“Factorial of “+n +” = "+f); } } /[end of class 13. Write a program to print the Fibonacci series upto n. public class fibonacci { public void numbers(int n) // method with input { inta, b, ¢; fi whilele < 0) /1 \oop for fibonacci { c=a+b; System.out.print(c); a=b; b=q } } } /fend of class 14, Write a program to print the first 20 elements of the Fibonacci series. public class fibonacci { public vold numbers { inta, b, , f; while(c < 20) // oop for fibonacci { feat; System.out.print(f); cH; } //end of class 15. Write a program to check if a number is an Armstrong Number. Info: If the sum of the cube of the digits of a number is equal to the number itself, then the number is said to be an Armstrong number. public class armstrong public void number( int n) //method with input { int d, c, num, sum; num =n; I/ copy of n sum=0; while (n>0) { d=n% 10; // splitting digits c=d*d*d; // cube of digit sum = sum +c; // sum of cubes n=n/10; // reducing value } if (sum==num) System.out.print(“The number entered is Armstrong”); else System.out.print("The number entered is not Armstrong”); } } //end of class 16. Write a program to check ifa number is prime or not. public class prime { public void check_prime(int n) //method with input { int flag; flay for{ i i< n; i++) { if (n%6i==0) //\f divisible flag =1; } if (flag==1) ‘System.out.print(“The number entered is not Prime”); else System.out.print("The number entered is Prime”); } // end of class 17. Write a program to split the digits of a number and print the sum of the digits. public class digits { public void number(int n) { intsum int digit; while (n>0) { digit = n%10; // extracting digits sum= sumtdigit; // calculating sum n= n/10; // new value } (SUR SURED // adding the last digit System.out:print("sum of the digits: "+sum); } } 18, Write a program to print all prime numbers between 1 and 100, public class prime { public void get_prime() { TBH, Department of ICT, The Assam Valley School 2019 : Page 37. int{,}, flag; System.out,printin(2); for (j = 3; j<=100; j++) // for numbers upto 100 { flag = 0; fort I = 2; I<}; i++) // for checking { if (jpse=o) //\f divisible flag =1; ) if(flag==0) System.out.printin(}); } } ) //end of class 19) Write a program to generate the following pattern through loops: - 1 12 123 1234 (The number of linesis to be input by the user) // Pattern of numbers public class Challenge1 { \ public void main(int n) // input parameter { inti,j; // control variables for (i=1; i» /{ control variables iI-) // outer loop forlj = 4; j<=i; j++) //inner loop System.out.print(j); System.out.printin(); // next line 21) Write a program to print the following triangle: - 1 121 12321 1234321 123454321 public class series1 { public void triangle() { int i, j, k; System.out.printin(1); //first line for(i = 2; i<=5; i++) { for() = 1; j<=i; J++) { System.out, print(j); } for(k = 1-1;k>0; k--) { 40P(K) y Sop TBH, Department of ICT, The Assam Valley School“2019 Page 39 23) Write a program to generate the following pattern through loops: - eens eee oe . (The number of lines is to be input by the user) // Pattern of numbers public class Challenged { Public vold main(intn) _// input parameter { inti, j,k; // control variables for (i=; i>=1;i-) // number of lines { for(j = ‘a; j++) // Spaces on the line System.out.print(' '); } for(k = 4; k<=i; k++) //Values on the line { System.out.print('*'); ) System.out.printin(); // next line TBH, Department of ICT, The Assam Valley School- 2019 Page 41 es ne cee cent Se Chine kanal uname are oO , (The number oflines is to be input by the user) // Pattern of numbers Public class Challenges { Public void main(int n) _// input parameter { inti, j, kj // control variables inta=n; // duplicate copy int b= 0; for (i= 3; { i++) // number of lines for{j = 1; j<=a; j++) // Spaces on the line { System.out.print(’ '); } for(k = 1; k<=i; k++) // Values on the line { System.out.print("* "); } System.out.printin(); // next line } for (i=n-1; i>=1;i--) _// number of lines { Bia TBH, Department of ICT, The Assam Valley School- 2019 rae @ forlj =1;}; { ‘System.outprint(’ '); 'b; j++) //' Spaces on the line } for{k = 1; ke=i;k++) //Values on the line { ‘System.out.print("* "); } System.outprintin(); // next line 25) Write a program to generate the following pattern through loops: - 1 121 12321 1234321 12321 121 Fy // Pattern of numbers public class Challenge10 { public void main(int n) —_// input parameter { inti, j, ks // control variables inta=n; ‘// duplicate copy int b=0; for (i= 4; i<=n; i++) // number of lines { TBH, Department of ICT, The Assam Valley School- 2019 Page 43, forlj = 1; j<=a; j++) // Spaces on the jin { System.outprint(’ '); } fork = 1; keal;k++) // ascending values on the line { System.out.print(k); } for{k = 1-1; k>#1; k--) // ascending values on the line { System.out.print(k); } System.out.printin(); // next line } for (i =n-1; i>=1;i--) // number of lines { bet; forlj = 1; j<=b; j++) // Spaces on the line { System.out print '); } for{k = 1; k<=i;k++) // ascending values on the line { System.out.print(k); } for{k = i-1; k>=1; k~-) // descending values on the line { System.out.print(k); } System.out.printin(); // next line J ‘TBH, Department of ICT, The Assam Valley School- 2019 : eos ho Rea 26) Write a program to find and display the prime numbers between x and y. // Prime Numbers ina given range public class Challenge6 { public void main(int x, int y) { int count; // tracker inti,j; // for loops for(i = x;i0) { K%610;—_// extracting digit ligit* digit* digit; sum+= cube; k=/10; } if (sum==i) // reducing value System.out printin(i); } } [end of main() V/end of class 28) Write a program to check if a number: input (n) isa special number or not. (A special number is one which is equal to the sum of the factorial of its: 145° = 1144145) =1+244120 = 145 Hence, 145 is a special number. // Special Number Public class Challenges { public void main(int n) { int digit; int fact; // necessary variables int sum = Oj; inti; /[ or looping digits. E.g. 145) Page 46 intk =n; 1/ duplicate while(n>0) { digit fact= 4) /initialization 19610; // extracting digit digit; i++) n=n/10; // reducing value } if(sum= ) System.out.printin(k+ "is a special number’); else System.out printin(k+ "is not a special number”); } //end of main() ¥/ end of class 29) Write a program to enter xand y and calculate x"and y*. I raising powers through loops public class Challenge9 { public void main(int x, inty) // input parameters { inti; /[ necessary variables Jong xpowy =1; // initialization Jong ypowx = 1; for (i=4; //-xto the power y xpowy*= x; for (i= 1; i

You might also like