Posts

Showing posts from January, 2021

20.factorial using for loop

 // factorial using for loop import java.util.*; class factorial { public static void main(String arg[]) { int f = 1, no, a; Scanner obj = new Scanner(System.in); System.out.print("Enter any number : "); no = obj.nextInt(); for(a = 1; a <= no; a++) { f = f * a; } System.out.println("Factorial : " +f); } }

19.for loop print 1 to 10 number

 //for loop print 1 to 10 number class forloop { public static void main(String arg[]) { int a; for(a = 1; a <= 10; a++) { System.out.println(a); } } }

18.prime number using do...while loop

 //prime number using do...while loop import java.util.*; class primedowhile { public static void main(String arg[]) { int no, a = 2; Scanner obj = new Scanner(System.in); System.out.print("Enter any number : "); no = obj.nextInt(); do { if(no % a == 0) { break; } a++; } while(a < no); if(a == no) System.out.println( no + " is Prime Number"); else System.out.println("Number is Not Prime"); } } 

17.reverse number using do...while loop

 // reverse number using do...while loop import java.util.*; class reversedowhile { public static void main(String arg[]) { int no, r, rev = 0; Scanner obj = new Scanner(System.in); System.out.print("Enter any number : "); no = obj.nextInt(); do { r = no % 10; rev = rev * 10 + r; no = no / 10; } while(no != 0); System.out.print("Reverse number is : " + rev); } }

16.do...while loop print 1 to 5 number

 // do...while loop 1 to 5 number class dowhile1 { public static void main(String arg[]) { int a = 1; do { System.out.println(a); a++; } while(a <= 5); } }

15.jumping statement - continue

 // jumping statement - continue class continue1 { public static void main(String arg[]) { int a = 1; while(a <= 10) { if(a < 5) { a++; continue; } System.out.println(a); a++; }  } }

14.jumping statement - break

 //jumping statement  - break class break1 { public static void main(String arg[]) { int a = 1; while(a <= 10) { if(a == 5) { break; } System.out.println(a); a++; } } }

13.while loop - prime number or not

 //while loop - prime number or not //prime number means number is divided by 1 and itself. import java.util.*; class primeornot { public static void main(String arg[]) { int a, n; Scanner obj = new Scanner(System.in); System.out.print("Enter any number : "); n = obj.nextInt(); a = 2; while(a < n) { if(n % a == 0) { break; } a++; } if(a == n) System.out.println(n + " is Prime number"); else System.out.println(" Not Prime "); } }

12.reverse number using while loop

 //reverse number using while loop import java.util.*; class reverse { public static void main(String arg[]) { int no, rev = 0, r; Scanner obj = new Scanner(System.in); System.out.print("Enter any number : "); no = obj.nextInt(); while(no != 0) { r = no % 10; rev = rev * 10 + r; no = no / 10; } System.out.println("Reverse Number : " + rev); } }

11.while loop print 1 to 10 number

 //while loop print 1 to 10 number class while1 { public static void main(String arg[]) { int a = 1; while(a <= 10) { System.out.println(+a); a++; } } }

10.switch case in java

 import java.util.*; class day { public static void main(String arg[]) { Scanner obj = new Scanner(System.in); int no; System.out.print("Enter 1 to 7 number : "); no = obj.nextInt(); switch(no) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednday"); break; case 4: System.out.println("Thersday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default : System.out.println("Invalid Number"); } } }

9.find a number is positive, negetive or zero number (multiple if)

 import java.util.*; class posneg { public static void main(String arg[]) { Scanner obj = new Scanner(System.in); int no; System.out.print("Enter any No. : "); no = obj.nextInt(); if(no > 0) System.out.println(no + " is Positive Number"); if(no < 0) System.out.println(no + " is Negetive Number"); if(no == 0) System.out.println("Number is Zero"); } }

8.odd and even number using if else.

 import java.util.*; class oddeven { public static void main(String arg[]) { Scanner obj = new Scanner(System.in); int no; System.out.print("Enter any no. : "); no = obj.nextInt(); if(no % 2 == 0) System.out.println(no + " is Even number"); else System.out.println(no + " is Odd number"); } }

7.get kilometer from user and convert in meter and centimeter after print it

 import java.util.*; class kmcal { public static void main(String arg[]) { Scanner obj = new Scanner(System.in); int km, m, cm; System.out.print("Enter Kilometer : "); km = obj.nextInt(); m = km * 1000; cm = m * 100; System.out.println("Meter = " + m); System.out.println("Centimeter = " + cm); } }

6.getdata from user and print

 //import java.util.Scanner; // java packege, util subpackeg , Scanner is class of util import java.util.*; // import all objects class getdata { public static void main(String arg[]) { Scanner obj = new Scanner(System.in); int age; String name; System.out.print("Enter your name : "); name = obj.next(); System.out.print("Enter your age : "); age = obj.nextInt(); System.out.println("Name : " + name); System.out.println("Age : " + age);   } }

5.calculate square and cube

 class square_cube { public static void main(String arg[]) { int a = Integer.parseInt(arg[0]); // convert string to integer int sq, cu; sq = a * a; cu = a * a * a; System.out.println("Square = " + sq); System.out.println("Cube = " + cu); } }

4.simple calculation in java

 class calculate { public static void main(String arg[]) { int a = 10, b = 5; System.out.println("A = " + a); System.out.println("B = " + b); int c = a + b; System.out.println(" Addition  = " + c); c = a - b; System.out.println(" Substraction = " + c); c = a * b; System.out.println(" Multiplication = " + c); c = a / b; System.out.println(" Division = " + c); } }

3.command line argument 2

 class commandline2 { public static void main(String arg[]) { System.out.println("Total Arguments = " + arg.length); // int i; for(i = 0; i < arg.length; i++) { System.out.println("\t" + arg[i] + "\t" + arg[i].length()); // length of string } } }

2.commandline argument 1

class commandline { public static void main(String arg[]) { System.out.println(arg[0]); }  }

1.first program in java

 class first { public static void main(String arg[]) { System.out.println("Welcome to JAVA Programmig"); } }

* pyramid

/*  *       * * *     * * * * *    * * * * * * */ #include <stdio.h> #include <conio.h> void main() { int i, j; clrscr(); for(i = 0; i < 6; i++) { for(j = 6; j > i; j--) printf("  "); for(j = 1; j <=i; j++) printf("   *"); printf("\n"); } getch(); }

pyramid

 /* 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1 */ #include <stdio.h> #include <conio.h> void main() { int i, j; clrscr(); for(i = 1; i <= 5; i++) { for(j = i; j > 0; j--) { printf("%d ", j); } printf("\n"); } getch(); }

Bubble Sort

#include <stdio.h> #include <conio.h> void main() { int a[5], i, j, temp; clrscr(); for (i = 0; i <= 4; i++) {        scanf ("%d",&a[i]); } for(i = 0; i <= 3; i++) { for (j = 0; j <= 3 - i; j++) { if(a[j] > a[j+1]) { temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } for(i = 0; i <= 4; i++) { printf("\n%d" , a[i]); } getch(); } 

Selection Sort

 #include <stdio.h> #include <conio.h> void main() { int i, j, a[5], temp; clrscr(); for(i = 0; i <= 4; i++) { scanf("%d", &a[i]); } for(i = 0; i <= 3; i++) { for(j = i + 1; j <= 4; j++) { if(a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } for(i = 0; i <= 4; i++) { printf("\n%d", a[i]); } getch(); }

stack

 #include <iostream.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> #define size 5 int stk[size], top = -1; void push(); void pop(); void peep(); void disp(); void main() { int ch; clrscr(); do { clrscr(); cout << "\n 1.push"; cout << "\n 2.pop"; cout << "\n 3.peep"; cout << "\n 4.disp"; cout << "\n 5.exit"; cout << "\nEnter your choice : "; cin >> ch; switch(ch) { case 1: push(); break; case 2: pop(); break; case 3: peep(); break; case 4: disp(); break; case 5: exit(0); break; default : cout << "Enter proper choice.."; } getch(); } while(ch != 5); getch(); } void push() { int no; if(top >= size-1) { cout << "stack is full"; } else { cout << "Enter any Number : "; cin >> no;