00:00
A04_a
/* 1 - Comparison of two number */ public class a_001 { public static void main(String[] args) { int a = 5; int b = 3; if (a > b) { System.out.println("A is Greater than B"); } else if (a < b) { System.out.println("B is Greater than A"); } else { System.out.println("C and b are equal"); } } }
/* 2 - Student grade if, 30 sm 40 = THIRD, 41 sm 50 = SECOND, 51 sm 60 = FIRST, sm>60 = TOPPER */ public class a_0002_student_marks_only_greaterthan { public static void main(String[] args) { int sm = 30; /* score-marks = sm */ int a = 30; int b = 40; int c = 41; int d = 50; int e = 51; int f = 60; int g = 61; if (sm > a && sm < b) { System.out.println(" Student's Grade = 'C' "); } else if (sm > c && sm < d) { System.out.println(" Student's Grade = 'B' "); }else if (sm > e && sm < f) { System.out.println(" Student's Grade = 'A' "); }else if (sm > g) { System.out.println(" Student's Grade = 'Topper' "); }else { System.out.println(" Student's Grade = 'F' "); } } } // will not work on all the defined literals
/* 3 - Student grade -this will works fine */ public class a_0003 { public static void main(String[] args) { /* Declare and initialize variables */ int sm = 30; /* Lets say score-marks = sm*/ int a = 30; int b = 40; int c = 41; int d = 50; int e = 51; int f = 60; int g = 61; if (a <= sm && sm <= b) { System.out.println(" Student's Grade = 'C' "); }else if (c <= sm && sm <= d) { System.out.println(" Student's Grade = 'B' "); }else if (e <= sm && sm <= f) { System.out.println(" Student's Grade = 'A' "); }else if (g <= sm) { System.out.println(" Student's Grade = 'Topper' "); }else { System.out.println(" Student's Grade = 'F' "); } } }
/* 4 - optimized a_0003 code using elseif */ public class a_0005 { public static void main(String[] args) { /* Declare and initialize variables */ int sm = 41; /* Let's say score-marks = sm */ /* Check the score range and print the corresponding grade */ if (30 <= sm && sm <= 40) { System.out.println("Student's Grade = 'C'"); } else if (31 <= sm && sm <= 50) { System.out.println("Student's Grade = 'B'"); } else if (51 <= sm && sm <= 60) { System.out.println("Student's Grade = 'A'"); } else { System.out.println("Student's Grade = " + ((60 < sm && sm <= 100) ? "'Topper'" : "'F'")); } } }
/* 5 - check number even odd status */ public class a_0006 { public static void main(String[] args){ int no = 10; if( no%2 == 0){ System.out.println("the number is even"); } else{ System.out.println("odd number") ; } } } // ma // if mod/reminder ==0,evenHome Paris Paris
/* 6 - check leap year */ // con-1 = divide by 4, accept if not fully div by 100 , accept if fully div by 400 public class a_0007_leap_year { public static void main (String [] args ){ int le = 2016; if(le/4 ==0){ if(le/100==0){ if(le/400==0){ System.out.println("is a leap year"); } else{ System.out.println("not a leap year"); } } else{ System.out.println("not a leap year"); } } else{ System.out.println("not a leap year"); } } }
/* 7 - check leap year using OR operator*/ public class a_0008_leap_year_OR_operator { public static void main(String[] args) { int le = 2016; if( (le%400==0) || (le%4 == 0 && le%100!=0 )){ System.out.println("leap year"); } else{ System.out.println("not a leap year"); } } }
/* 8 - swap two number using third variables*/ public class a_009_swap_two_number { public static void main(String[] args) { int a=10, b=20 ; int t=0 ; System.out.println("a=" +a); System.out.println("b=" +b); t=b; b=a; a=t; System.out.println("a=" +a); System.out.println("b=" +b); } } // ma code logic // t ->b ->a // a->t , cyclic orderHome Paris Paris
/* 9 - simple for loop explain */ public class a_0009_a { public static void main(String[] args) { for( int i =1;i<=10 ;i++){ System.out.println("increment loop "+i); } for(int j =10;j>=1;j--){ System.out.println("decrement loop"+j); } } } // ma // start loop for incremnt int ( i =1 ); // increment it and do the following task ( i++ ) // till i is less than 10 , or equals to 10 (i<=10) // for decrement // start loop form 10 int ( j =10 ); // decrement it and do the followign task ( j--) // till j is greater than 0, or equal to 0 ( j>=0) // लूप स्टार्ट करेंगे i=1 से और i में 1 बढ़ते जाएंगे और नीचे का ब्लॉक एग्जीक्यूट करते रहेंगे जब तक की i का मान बढ़ाते बढ़ाते 10 ना हो जाए l // MA for using lessthen or greater than // अगर हम i की वैल्यू को बढ़ा रहे हैं तो इसका मतलब i छोटा है | we will start i smaller than equals to // और अगर हम i की वैल्यू को घटा रहे हैं तो इसका मतलब i बड़ा है | we will start i greater than equals to
/* 10 - swap without uisng third variables*/ public class a_0010 { public static void main(String[] args) { int a=10, b=20 ; System.out.println("a=" +a); System.out.println("b=" +b); a = a+b; b = a-b; a = a-b; System.out.println("a=" +a); System.out.println("b=" +b); } } // formula a=a+b; // updaate b=a-b, then a ;Home Paris Paris
/* 11 - user input by using scanner class package-(java.util) */ // user input by using scanner class package-(java.util) // for that we will create scanner class object (to use its methods) import java.util.Scanner; // must import the scanner class for creating its method public class a_0011 { public static void main(String[] args) { Scanner s = new Scanner(System.in); /* must create object ,to access method */ System.out.println("enter your name"); String name = s.next(); /*next method is used for receiveing ip form the user*/ System.out.println("enter your age"); int age =s.nextInt(); /* nextInt = scanner class method*/ System.out.println("enter your gender"); char gender = s.next().charAt(0); System.out.println("enter your phoen number"); long phno = s.nextLong(); System.out.println("--------------------------"); System.out.println("name="+name); System.out.println("age="+age); System.out.println("gender="+gender); System.out.println("phone="+phno); } } // create object ( scanner class) // next, nextInt, nextCharAt(0), nextLong(),Home Paris Paris
/* 12 - switch case calculator using do while and or operator */ import java.util.Scanner; public class a_0012 { public static void main(String[] args) { String ans; int res; do{ Scanner sc = new Scanner(System.in); System.out.println("enter first number"); int no1 = sc.nextInt(); System.out.println("enter second number"); int no2 = sc.nextInt(); System.out.println("select symbol +, -"); String st = sc.next(); switch (st) { case "+": res = no1+no2; System.out.println("res=" +res); break; case "-": res = no1-no2; System.out.println("res=" +res); break; default: System.out.println("invalid symbol"); break; } System.out.println("do you want to execute"); Scanner tell = new Scanner(System.in); ans = tell.next(); } while((ans.equals("y")) || (ans.equals("Y") )); } }
/* 13 - looping program for creating table */ import java.util.Scanner; public class a_0013 { public static void main(String[] args) { System.out.println("enter the number"); Scanner ino = new Scanner(System.in); int no = ino.nextInt(); /* int no = 2; */ for(int i =1; i<=10; i++){ System.out.println(no+" * "+i+" = "+ no*i); } } } // increment upto 10 and in loop body * 2 , printHome Paris Paris
/* 14 - factorial of a number using decrement */ public class a_0014 { public static void main(String[] args) { int no = 5; int fact = 1; for(int i=no;i>=1;i--){ fact = fact*i; /* 1 x 2 x 3 x 4 x 1 x 5 */ } System.out.println("fact = "+fact); } }
/* 15 - factorial of a number using increment */ public class a_0014 { public static void main(String[] args) { int no = 5; int fact = 1; for(int i=1;i<=no;i++){ fact = fact*i; // 5 x 4 x 3 x 2 x 1 x 1 } System.out.println("fact = "+fact); } }
/* 16 - factorial using recursion */ // a_0015 factorial using recursion methods call itself, again and again // call itself with decrement by one again and again public class a_0015 { static int fact = 1; public static void main(String[] args) { int n = 6; a_0015 obj = new a_0015(); obj.calcFunc(n); System.out.println("the output of result is = "+fact); } void calcFunc(int n){ if ( n >=1){ fact =fact*n; calcFunc(n-1); } } } // ma // create object, call method with parameters // s-UP varaible=1 // self call > with parameters (n-1) // only execute the above code if (n>=1), +val // increment // self call with (n+1) // only execute the above code if (n<=6)
/* 17 - factorial using recursion with return type */ public class a_0016 { static int fact = 1; public static void main(String[] args) { int n = 5; a_0016 obj1 = new a_0016(); obj1.calcFunc1(n); fact = obj1.calcFunc1(n); System.out.println("the output of result is = "+fact); } int calcFunc1(int x) { if(x>=1) { return ( x * calcFunc1(x-1)); } return 1; } }
/* 18 - reverse a number */ public class a_0017 { public static void main(String[] args) { int no = 123456; int rem, rev = 0; while (no!=0){ rem=no%10; rev=rev * 10 + rem ; no = no/10; } System.out.println(rev); } } // ma // mod the number // varaible =0 *10+ mod, s-UP // div for update
/* 19 - reverse a string */ public class a_0018 { public static void main(String[] args) { String name = "Rajat"; int length = name.length(); String rev = ""; for(int i = (length-1) ; i>=0 ; i--){ rev = rev + name.charAt(i); } System.out.println(rev); } } // 1. find length // 2. decrement loop,(lenght-1) // 3. update variable using charAt Home Paris Paris
/* 20 - prime numbers */ public class a_0019 { public static void main(String[] args) { int no = 11, temp=0; for(int i=2;i<=(no-1);i++) { if(no%i == 0) { // System.out.println(no%i); // code check every number temp = temp+1; // System.out.println("not prime"); // againag and again print till the number } } if(temp == 0){ System.out.println("prime number"); } else { System.out.println("not prime"); } } } // for loop 2 to n-1 // if n % i==0 , not prime, set temp // check tempHome Paris Paris
/* 21 - print all prime number between 1 to 100 */ public class a_0020 { public static void main(String[] args) { for(int no=1;no<=100;no++){ int temp = 0; for(int i=2;i<=(no-1); i++){ if(no%i ==0){ temp=temp+1; } } if(temp==0){ System.out.println(no); } } } } // 2 for loops // 1 for # check (let say = n) // 2 for 2 to n-1 // if no%1==0, set temp // check temp and print n-1
/* 22 - ficonacci series till 10 */ public class a_0021 { public static void main(String[] args) { int a =0,b=1; System.out.println("first number are = " + a + " , " + b ); for(int i=1;i<=10;i++){ int c =a+b; System.out.print(c+" "); a=b; b=c; } } } // Let a = 0, b = 1 // Formula c = a+b. // print above // c-> b-> a and loop itHome Paris Paris
/* 23 - ficonacci series using recursion..*/ public class a_0022 { static int a = 0; static int b = 1; static int c; public static void main(String[] args) { a_0022 obj = new a_0022(); obj.abc(10); } void abc( int x){ if(x>=0){ c =a+b; System.out.println(c); a=b; b=c; abc(x-1); } } } // ma code logic // same as previous // at calling self, parameters =(x-1) // condition check, trueHome Paris Paris
/* 24 - check palindrome 121 reverse same */ public class a_0023 { public static void main(String[] args) { int no = 1221; int temp = no; int rev=0,rem; while(temp!=0){ rem=temp%10; rev = rev*10+rem; temp=temp/10; } if(no==rev){ System.out.println("palindrom number"); } else{ System.out.println("not a palindrom number"); } } } // ma code logic // make it reverse, use formula // check with if conditionHome Paris Paris
/* 25 - find the length of ann integer */ public class a_0024 { public static void main(String[] args) { int a = 111111; int lenght=0; while(a!=0){ lenght = lenght+1; a = a/10; /* Decrement the number and again update it */ } System.out.println("the length of string is = "+lenght); } } // MA Code Logic // mod the number // s-up th var to +1 // div the number for update
/* 26 - amstorng 123 = 1^3 + 2^3 + 3^3 ( both side equal) like 153 */ public class a_0025 { public static void main(String[] args) { int no = 153; int t1 = no; int t2=no; int arm = 0; int leng = 0; while(t1!=0) { /* Length of the number*/ leng= leng+1; t1=t1/10; } while (t2!=0) { /* for Reminder */ int rem; int mul=1; rem = t2%10; for(int i=1;i<=leng;i++) { /* Reminder multiply by lenght */ mul = mul * rem; } System.out.println(mul); arm = arm + mul; t2=t2/10; } if(arm==no) { System.out.println("number is arsmtrong"+arm); } else { System.out.println("not armstrong"+arm); } } } // MA code logic // find length for power,say=n // loop for mod and loop for * n // add/update the result and again loop for mod
00.png
Home PREVIOUS NEXT