Skip to content

Commit 248ed1e

Browse files
authored
Basic java programs
0 parents  commit 248ed1e

File tree

9 files changed

+470
-0
lines changed

9 files changed

+470
-0
lines changed

SqSum.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//java program to accept 10 numbers in an array and compute the square of these numbers. And then print the sum of these numbers
2+
3+
import java.util.Scanner;
4+
5+
public class SqSum
6+
7+
{
8+
9+
public static void main(String args[])
10+
11+
{
12+
13+
Scanner scanner = new Scanner(System.in);
14+
15+
int n[]= new int[10];
16+
17+
int sq[]= new int[10];
18+
19+
int i,total=0;
20+
21+
System.out.println("Enter 10 numbers:");
22+
23+
for(i=0;i<10;i++)
24+
25+
{
26+
27+
n[i]=scanner.nextInt();
28+
29+
sq[i]=n[i]*n[i];
30+
31+
total=total+sq[i];
32+
33+
}
34+
35+
System.out.println("Sum of squares of 10 numbers= "+total);
36+
37+
}
38+
39+
}

add.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//program to add two numbers using command line arguments
2+
3+
class add
4+
5+
{
6+
7+
public static void main(String a[])
8+
9+
{
10+
11+
int x,y,s;
12+
13+
x=Integer.parseInt(a[0]);
14+
15+
y=Integer.parseInt(a[1]);
16+
17+
18+
s=x+y;
19+
20+
System.out.println("sum of " + x + " and " + y +" is " +s);
21+
22+
}
23+
24+
}

calculator.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//program to implement a command line calculator
2+
3+
import java.util.Scanner;
4+
5+
class calculator
6+
7+
{
8+
9+
public static void main(String a[])
10+
11+
{
12+
13+
int n,x,y,s;
14+
15+
System.out.println("Enter your choice number: \n 1. Add\n 2. Subtract\n 3. Multiply\n 4. Divide\n 5. Mod\n");
16+
17+
n=Integer.parseInt(a[0]);
18+
19+
x=Integer.parseInt(a[1]);
20+
21+
y=Integer.parseInt(a[2]);
22+
23+
switch(n)
24+
25+
{
26+
27+
case 1:
28+
29+
s=x+y;
30+
31+
System.out.println("sum of " + x + " and " + y +" is " +s);
32+
33+
break;
34+
35+
case 2:
36+
37+
s=x-y;
38+
39+
System.out.println("subtraction of " + x + " and " + y +" is " +s);
40+
41+
break;
42+
43+
case 3:
44+
45+
s=x*y;
46+
47+
System.out.println("multiplication of " + x + " and " + y +" is " +s);
48+
49+
break;
50+
51+
case 4:
52+
53+
s=x/y;
54+
55+
System.out.println("division of " + x + " and " + y +" is " +s);
56+
57+
break;
58+
59+
case 5:
60+
61+
s=x%y;
62+
63+
System.out.println("mod of " + x + " and " + y +" is " +s);
64+
65+
break;
66+
67+
default: break;
68+
69+
}
70+
71+
}
72+
73+
}

class_object.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//program using classes and objects in java
2+
3+
class operations
4+
5+
{
6+
7+
void add(int x,int y)
8+
9+
{
10+
11+
int z;
12+
13+
z=x+y;
14+
15+
System.out.println("Sum of " +x+" and "+y +" is: " +z);
16+
17+
}
18+
19+
void subtract(int a, int b)
20+
21+
{
22+
23+
int c;
24+
25+
c=a-b;
26+
27+
System.out.println("Subtraction of " +a+" and "+b +" is: " +c);
28+
29+
}
30+
31+
}
32+
33+
class class_object
34+
35+
{
36+
37+
public static void main(String args[])
38+
39+
{
40+
41+
operations obj = new operations();
42+
43+
obj.add(5,2);
44+
45+
obj.subtract(5,2);
46+
47+
}
48+
49+
}

fabonacci.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//program to print fabonacci series using loop
2+
3+
import java.util.Scanner;
4+
5+
class fabonacci
6+
7+
{
8+
9+
public static void main(String args[])
10+
11+
{
12+
13+
int i,n,a,b,c;
14+
15+
System.out.println("Enter the value of n: ");
16+
17+
Scanner scan = new Scanner(System.in);
18+
19+
n=scan.nextInt();
20+
21+
a=0;
22+
23+
b=1;
24+
25+
System.out.println(a);
26+
27+
System.out.println(b);
28+
29+
for(i=1;i<n-1;i++)
30+
31+
{
32+
33+
c=a+b;
34+
35+
System.out.println(c);
36+
37+
a=b;
38+
39+
b=c;
40+
41+
}
42+
43+
}
44+
45+
}
46+
47+

greatest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//program to find the greatest of two numbers
2+
3+
import java.util.Scanner;
4+
5+
public class greatest
6+
{
7+
8+
public static void main(String args[])
9+
{
10+
11+
int a,b,big;
12+
13+
Scanner scan=new Scanner(System.in);
14+
15+
System.out.println("Enter two numbers: ");
16+
17+
a=scan.nextInt();
18+
19+
b=scan.nextInt();
20+
21+
if(a>b)
22+
23+
{
24+
25+
big=a;
26+
27+
}
28+
29+
else
30+
31+
{
32+
33+
big=b;
34+
35+
}
36+
37+
System.out.println("Grestest of two numbers is: " +big);
38+
39+
}
40+
41+
}

month.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//program to input a number of month and print it’s equivalent name of the month
2+
3+
import java.util.Scanner;
4+
5+
public class month
6+
7+
{
8+
9+
public static void main(String args[])
10+
11+
{
12+
13+
Scanner scanner = new Scanner(System.in);
14+
15+
int n;
16+
17+
System.out.println("Enter the month number:");
18+
19+
n=scanner.nextInt();
20+
21+
switch(n)
22+
23+
{
24+
25+
case 1:System.out.println("January");
26+
27+
break;
28+
29+
case 2:System.out.println("February");
30+
31+
break;
32+
33+
case 3:System.out.println("March");
34+
35+
break;
36+
37+
case 4:System.out.println("April");
38+
39+
break;
40+
41+
case 5:System.out.println("May");
42+
43+
break;
44+
45+
case 6:System.out.println("June");
46+
47+
break;
48+
49+
case 7:System.out.println("July");
50+
51+
break;
52+
53+
case 8:System.out.println("August");
54+
55+
break;
56+
57+
case 9:System.out.println("September");
58+
59+
break;
60+
61+
case 10:System.out.println("October");
62+
63+
break;
64+
65+
case 11:System.out.println("November");
66+
67+
break;
68+
69+
case 12:System.out.println("December");
70+
71+
break;
72+
73+
}
74+
75+
}
76+
}

0 commit comments

Comments
 (0)