File tree Expand file tree Collapse file tree 10 files changed +275
-0
lines changed Expand file tree Collapse file tree 10 files changed +275
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Solution 9
2
+ class findFactorial {
3
+ public static void main (String args []){
4
+ factorial (args );
5
+ }
6
+
7
+ static void factorial (String args []){
8
+ if (args .length != 1 ) {
9
+ System .out .println ("Please provide exactly one number as command-line argument." );
10
+ return ;
11
+ }
12
+ int number = Integer .parseInt (args [0 ]);
13
+ int result = factorial (number );
14
+ System .out .println ("Factorial of " + number + " is: " + result );
15
+
16
+ }
17
+
18
+ public static int factorial (int num ) {
19
+ if (num < 0 ) {
20
+ System .out .println ("Factorial is not defined for negative numbers." );
21
+ return -1 ;
22
+ }
23
+
24
+ int result = 1 ;
25
+
26
+ for (int i = num ; i > 0 ; i --) {
27
+ result *= i ;
28
+ }
29
+ return result ;
30
+ }
31
+
32
+ }
Original file line number Diff line number Diff line change
1
+ // Solution 7
2
+ class greeting {
3
+ public static void main (String args []){
4
+ greetingUser (args ) ;
5
+ }
6
+
7
+ static void greetingUser (String args []){
8
+ if (args .length != 3 ) {
9
+ System .out .println ("Please provide only three strings as command line argument." );
10
+ return ;
11
+ }
12
+ greetUser (args [0 ], args [1 ], args [2 ]);
13
+ }
14
+
15
+ public static void greetUser (String str1 , String str2 , String str3 ) {
16
+
17
+ System .out .println ("Checking the lengths of the strings..." );
18
+
19
+ switch (getStringLength (str1 )) {
20
+ case 0 , 1 , 2 :
21
+ System .out .println ("First string is too short." );
22
+ break ;
23
+ default :
24
+ System .out .println ("First string is valid! Greeting..." );
25
+ break ;
26
+ }
27
+
28
+ switch (getStringLength (str2 )) {
29
+ case 0 , 1 , 2 :
30
+ System .out .println ("Second string is too short." );
31
+ break ;
32
+ default :
33
+ System .out .println ("Second string is valid! Greeting..." );
34
+ break ;
35
+ }
36
+
37
+ switch (getStringLength (str3 )) {
38
+ case 0 , 1 , 2 :
39
+ System .out .println ("Third string is too short." );
40
+ break ;
41
+ default :
42
+ System .out .println ("Third string is valid! Greeting..." );
43
+ break ;
44
+ }
45
+ }
46
+
47
+ public static int getStringLength (String str ) {
48
+ return str .length ();
49
+ }
50
+ }
Original file line number Diff line number Diff line change
1
+ // Solution 3
2
+ import java .lang .Math ;
3
+ class numberComparisons {
4
+ public static void main (String args []){
5
+ displaysmallest (args );
6
+ }
7
+
8
+ static void displaysmallest (String args []){
9
+ if (args .length != 3 ) {
10
+ System .out .println ("Please provide exactly three numbers." );
11
+ return ;
12
+ }
13
+ int number1 = Integer .parseInt (args [0 ]) ;
14
+ int number2 = Integer .parseInt (args [1 ]) ;
15
+ int number3 = Integer .parseInt (args [2 ]) ;
16
+ int largestno = Math .max (number1 ,number2 );
17
+ int sum = number1 + number2 ;
18
+ int smallest = Math .min (sum , number3 );
19
+ System .out .println ("The smallest between the sum of the first two numbers and the third number is: " + smallest );
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ // Solution 10
2
+ class primeNumberOddcheck {
3
+ public static void main (String args []){
4
+ int number = Integer .parseInt (args [0 ]);
5
+ boolean isPrime = isPrime (number );
6
+ if (isPrime ) {
7
+ System .out .println (number + " is a prime number." );
8
+ } else {
9
+ System .out .println (number + " is not a prime number." );
10
+ }
11
+
12
+ if (isOdd (number )) {
13
+ System .out .println (number + " is an odd number." );
14
+ } else {
15
+ System .out .println (number + " is an even number. Not an odd number. " );
16
+ }
17
+ }
18
+
19
+ static boolean isPrime (int num ) {
20
+ if (num <= 1 ) {
21
+ return false ; // 0 and 1 are not prime
22
+ }
23
+
24
+ for (int i = 2 ; i <= Math .sqrt (num ); i ++) {
25
+ if (num % i == 0 ) {
26
+ return false ; // number is divisible by i, so it's not prime
27
+ }
28
+ }
29
+ return true ; // number is prime
30
+ }
31
+
32
+ static boolean isOdd (int num ) {
33
+ return num % 2 != 0 ; // Returns true if num is odd, false if even
34
+ }
35
+ }
36
+
Original file line number Diff line number Diff line change
1
+ // Solution 8
2
+ class print1to10 {
3
+ public static void main (String args []){
4
+ formethod ();
5
+ whilemethod ();
6
+ dowhilemethod ();
7
+ }
8
+
9
+ static void formethod (){
10
+ System .out .println ("Printing from 1 to 10 using for loop only" );
11
+ for (int i =1 ;i <=10 ;i ++){
12
+ System .out .println (i );
13
+ }
14
+ }
15
+
16
+ static void whilemethod (){
17
+ int i =1 ;
18
+ System .out .println ("Printing from 1 to 10 using while loop only" );
19
+ while (i <=10 ) {
20
+ System .out .println (i );
21
+ i ++;
22
+ };
23
+ }
24
+
25
+ static void dowhilemethod (){
26
+ int i =1 ;
27
+ System .out .println ("Printing from 1 to 10 using do-while loop only" );
28
+ do {
29
+ System .out .println (i );
30
+ i ++;
31
+ }while (i <=10 );
32
+ }
33
+ }
Original file line number Diff line number Diff line change
1
+ // Solution 1
2
+ class printing1to7 {
3
+ public static void main (String args []){
4
+ display ();
5
+ }
6
+
7
+ static void display (){
8
+ for (int i =1 ;i <=7 ;i ++){
9
+ if (i ==3 ) System .out .println ("I am 3" );
10
+ else if (i ==5 ) System .out .println ("I am 5" );
11
+ else System .out .println (i );
12
+ }
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ // Solution 5
2
+ class sum {
3
+ public static void main (String args []){
4
+ displaysum ();
5
+ }
6
+
7
+ static void displaysum (){
8
+ int sum = 0 ;
9
+ for (int i =1 ;i <=20 ;i ++){
10
+ if (i %2 ==0 ){
11
+ sum +=i ;
12
+ }
13
+ else {
14
+ continue ;
15
+ }
16
+ }
17
+ System .out .println ("Sum of even numbers from 1 to 20 is : " +sum );
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ // Solution 4
2
+ class twentyTofour {
3
+ public static void main (String args []){
4
+ displaynumbers ();
5
+ }
6
+
7
+ static void displaynumbers (){
8
+ int i = 20 ;
9
+ while (i >3 ){
10
+ System .out .println (i );
11
+ i --;
12
+ }
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ // Solution 6
2
+ class userLogin {
3
+ public static void main (String args []){
4
+ loginCheck (args );
5
+ }
6
+
7
+ static void loginCheck (String args []){
8
+ if (args .length == 0 ) {
9
+ System .out .println ("Please provide your username and password as a command line argument." );
10
+ return ;
11
+ }
12
+ String username = args [0 ].toUpperCase ();
13
+ String password = args [1 ].toUpperCase ();
14
+ switch (username ) {
15
+ case "ANKAN" , "SOUPARNA" :
16
+ // Checking password based on username and now password
17
+ switch (password ) {
18
+ case "MADHU" , "TRISHA" :
19
+ System .out .println ("Login successful!" );
20
+ break ;
21
+ default :
22
+ System .out .println ("Invalid password." );
23
+ break ;
24
+ }
25
+ break ;
26
+ default :
27
+ System .out .println ("Invalid username." );
28
+ break ;
29
+ }
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ // Solution 2
2
+ class vowelsconsonantsCounter {
3
+ public static void main (String args []){
4
+ displaycounts (args );
5
+ }
6
+
7
+ static void displaycounts (String args []){
8
+ if (args .length == 0 ) {
9
+ System .out .println ("Please provide your name as a command line argument." );
10
+ return ;
11
+ }
12
+ String name =args [0 ].toUpperCase ();
13
+ System .out .println ("The name received is: " +name );
14
+ int vowels = 0 , consonants = 0 ;
15
+ for (int i = 0 ; i < name .length (); i ++) {
16
+ char ch = name .charAt (i );
17
+ if (ch =='A' || ch =='E' || ch =='I' || ch =='O' || ch =='U' ){
18
+ vowels ++ ;
19
+ }
20
+ else consonants ++ ;
21
+ }
22
+ System .out .println ("No. of vowels: " +vowels );
23
+ System .out .println ("No. of consonants: " +consonants );
24
+ }
25
+ }
You can’t perform that action at this time.
0 commit comments