Skip to content

Commit 2219125

Browse files
authored
Add files via upload
Java Programs second set
1 parent e916fdd commit 2219125

29 files changed

Lines changed: 738 additions & 0 deletions
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package javaPrograms;
2+
3+
import java.util.Scanner;
4+
5+
public class addTwoIntegers {
6+
7+
// add two integers entered by the user
8+
9+
public static void main(String[] args) {
10+
11+
Scanner reader = new Scanner(System.in);
12+
System.out.println("Please enter two numbers");
13+
System.out.println("Please enter the first number");
14+
int input = reader.nextInt();
15+
System.out.println("Please enter the next number");
16+
int inputtwo = reader.nextInt();
17+
int output = input+inputtwo;
18+
System.out.println("Result after adding given two numbers is "+output);
19+
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package javaPrograms;
2+
3+
public class armstrongNumber {
4+
5+
public static void main(String[] args) {
6+
// Armstrong number
7+
// 153 ==> 1*1*1 + 5*5*5 + 3*3*3 = 153
8+
9+
int num =371;
10+
double result = 0;
11+
12+
int actualNum =num;
13+
14+
while(num != 0) {
15+
16+
int n = num% 10;
17+
result = result + Math.pow(n, 3);
18+
num = num/10;
19+
}
20+
21+
if(result == actualNum) {
22+
System.out.println("Armstrong Number it is!");
23+
}else {
24+
System.out.println("Not an armstrong number");
25+
}
26+
27+
}
28+
29+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package javaPrograms;
2+
3+
public class armstrongNumberBetweenIntervals {
4+
5+
public static void main(String[] args) {
6+
// Armstrong number between two numbers
7+
8+
int low = 101;
9+
int high = 400;
10+
11+
for (int i = low; i<high; i++){
12+
int check, rem, sum = 0;
13+
check = i;
14+
while(check != 0) {
15+
rem = check % 10;
16+
sum = sum + (rem * rem * rem);
17+
check = check / 10;
18+
}
19+
if(sum == i){
20+
System.out.println(""+i+" is an Armstrong number.");
21+
}
22+
}
23+
}
24+
25+
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package javaPrograms;
2+
3+
public class checkAlplhabetOrNot {
4+
5+
public static void main(String[] args) {
6+
// Check alphabet
7+
8+
char c='F';
9+
if(c>='a'&& c<='z') {
10+
System.out.println("It is an alphabet");
11+
}else if(c>='A'&& c<='Z'){
12+
System.out.println("It is an alphabet");
13+
}
14+
else {
15+
System.out.println("Not an alphabet");
16+
}
17+
18+
19+
}
20+
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package javaPrograms;
2+
3+
public class checkLeapYear {
4+
5+
public static void main(String[] args) {
6+
// check leap year
7+
8+
//divisible by 4 for all the century years ending with 00
9+
//century is a leap year when it is divisble by 400
10+
//1900 is not a leap year
11+
//2012 is a leap year
12+
13+
int year = 2012;
14+
boolean leap=false;
15+
16+
if(year %4 == 0) {
17+
if (year%100==0) {
18+
if(year%400==0) {
19+
leap=true;
20+
}
21+
else {
22+
leap = false;
23+
}
24+
}else {
25+
leap =true;
26+
}
27+
28+
}
29+
else {
30+
leap =false;
31+
}
32+
33+
if(leap) {
34+
System.out.println(year +" is a leap year");
35+
}else {
36+
System.out.println(year+" is not a leap year");
37+
}
38+
39+
40+
}
41+
42+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package javaPrograms;
2+
3+
public class computeQuotientRemainder {
4+
5+
public static void main(String[] args) {
6+
// compute Quotient and Remainder
7+
8+
int a =20;
9+
int b=10;
10+
11+
int quotient = a/b;
12+
System.out.println("Quotient is "+ quotient);
13+
14+
int remainder =a%b;
15+
16+
System.out.println("Remainder is "+remainder);
17+
18+
19+
}
20+
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package javaPrograms;
2+
3+
public class countNumberOfDigitsInteger {
4+
5+
public static void main(String[] args) {
6+
// count the number of digits in an integer
7+
8+
int num = 1234567;
9+
String number = Integer.toString(num);
10+
11+
int numberOfDigits = number.length();
12+
System.out.println("Number of the digits in the given number is "+numberOfDigits);
13+
14+
}
15+
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package javaPrograms;
2+
3+
public class displayCharactersFromAtoZ {
4+
5+
public static void main(String[] args) {
6+
// display characters from a to z
7+
8+
/* for(int i =0;i<=10;i++) {
9+
System.out.println(i);
10+
}
11+
*/
12+
13+
for(char ch='a';ch<='z';ch++) {
14+
System.out.print(ch + " ");
15+
16+
}
17+
18+
}
19+
20+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package javaPrograms;
2+
3+
public class factorial {
4+
5+
public static void main(String[] args) {
6+
// factorial n*n-1
7+
8+
factorialLogic(10);
9+
10+
11+
12+
}
13+
14+
public static void factorialLogic(int num) {
15+
16+
int factorial = 1;
17+
18+
for (int i = 1; i <= num; i++) {
19+
factorial = factorial * i;
20+
System.out.println(factorial);
21+
}
22+
23+
System.out.println("Final number " + factorial);
24+
}
25+
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package javaPrograms;
2+
3+
public class factorialUsingRecursion {
4+
5+
public static void main(String[] args) {
6+
//
7+
8+
System.out.println(factorial(5));
9+
10+
}
11+
12+
public static double factorial(double num) {
13+
if(num>=1) {
14+
return num*factorial(num-1);
15+
}
16+
else {
17+
return 1;
18+
}
19+
}
20+
21+
}

0 commit comments

Comments
 (0)