0% found this document useful (0 votes)
12 views13 pages

TUTORIAL - 4 App

The code defines a class called CountDigitsAndLetters that contains a main method. The main method prompts the user to enter a string, then iterates through each character to count the number of digits and letters. It prints out the digit and letter counts after analyzing the entire input string.

Uploaded by

legendken77777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views13 pages

TUTORIAL - 4 App

The code defines a class called CountDigitsAndLetters that contains a main method. The main method prompts the user to enter a string, then iterates through each character to count the number of digits and letters. It prints out the digit and letter counts after analyzing the entire input string.

Uploaded by

legendken77777
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1- public class DivisibleBy8AndMultiple0f5 { STDIN

2 - public static void main(String[] args) {


3 System.out.print ln ("Numbers divisible by 8 and multiple of ! Input for the program (Optional )
4
5 - for (int i = 1000; i <= 2000; i++) {
if (i % 8 == 0 && i %5 == 0) {
7 System.out. println(i);
8 Numbers divisible by 8 and multiple
1000
10
1040
11 }
12 1080
1120
1160
1200
FEEDBACK
1240
1280
1320
1360
1400
1440
1480
1520
rogramiz Java Course
Online Java Compiler

Main.java Output

1 import java.util.Scanner ;
2 import java.util.Random;
3

4 public class NumberGuessingGame {


5 public static void main(String[]
args) {
6 Scanner scanner = new Scanner
(System. in);
Random random = new Random( );
8

int targetNumber = random


.nextInt (9) + 1;
10 int userGuess;
11
12 System.out .println( "Welcome to
the Number Guessing Game!"
):
13
14 do {
15 System.out.print("Guess a
number between 1 and 9:
"):
16 userGuess = Scanner.nextInt

):
17
18 if (userGuess ==
targetNumber) {
19 System.out.prin Run
Programiz Java Course
Online Java Compiler

Main.java Output
.nextnt(9) t |;
10 int userGuess;
11
12 System. out . println("Welcome to
the Number Guessing Game!"

13
14 do {
15 System.out.print("Guess a
number between 1 and 9:
"):
16 userGuess = Scanner.neXtInt
():
17
18 if (userGuess ==
targetNumber) {
19 System. out .println
("Well guessed!");
20 } else {
21 System. out.println("Try
again! ");
22 }
23 } while (userGuess !=
targetNumber);
24
25 Scanner.close();
26
27 } Run
Java Course
Online Java Compiler

Main.java Output

java -cp /tmp/yeSVHUynTg Number GuessingGame


Welcome to the Number Guessing Game !
Guess a number between 1 and 9: 1
Try again!
Guess a number between 1 and 9: 3
Try again!
Guess a number between 1 and 9: 4
Try again!
Guess a number between 1 and 9: 7
Well guessed!
PatternExample.java + 3zhmu5dmg NEW JAVA RUN L

1- public class PatternExample { STDIN


2 public static void main(String[] args) {
3 int rows = 5;
Input for the program (Optional)
5
6 for (int i = 1; i <= rows; i++) {
for (int j = 1; j<= i; j++) {
8 System. out . print ("* "): Output:
10 System.out.println();
11
* *
12
13 * * *

14 for (int i = r ows - 1; i >= 1; i--) { * *


15 for (int j = 1; j <= i; j++) { * * * * *
16 System.out .print("* "): * * * *
17
18 System.out.println(); * *

19
20
21 }
Main.java Run Output
1- import java.util.Scanner; java -cp /tmp/clBCjqw1 s4 Rever
2 Enter a word: hello
3- public class ReverseWord {
4 public static void main(String[] args) { Reversed word: olleh
5 Scanner scanner = new Scanner (System. in

6 System.out .print ("Enter a word: ");


String inputWord = scanner.nextLine();
8 Scanner.close():

10 String reversedWord = reverse(inputWord


):
11 System.out.println("Reversed word: " +
reversedWord);
12 }
13
14 - public static String reverse(String word) {
15 char [] charArray = word. toCharArray():
16 int start = 0;
17 int end = charArray. length - 1;
18
19 while (start < end) {
20 1/ Swap characters
21 char temp = charArray[start] ;
22 charArray[start] = charArray [end] ;
23 charArray[end] = temp;
24
25 start++;
26 end-
27
28
29 return new String(charArray);
30
31
rogramiz Large screen TVs,
Java
Certific
Online Java
Compiler
EMI Starting at Z11 with 0% interest BP

Main.java Run Output Clear

import java.util.Scanner; java -cp /tmp/clBCjqw1 s4 CountDigitsAndLetters


Enter a string: java is High level progr amming
3 public class CountDigitsAndLetters { language
4 - public static void main(Stringl] args) { Number of digits: ONumber of letters: 34
5 Scanner scanner = new Scanner (System. in

6 System.out.print("Enter a string: "):


String input = scanner.nextLine():
Scanner.close(0:

10 int digitCount
11 int letter Count = 0;
12
13 for (int i = 0; i input.length(); i
++) {
14 char ch = input.charAt(i);
15 if (Character .isDigit(ch)) {
16 digitCount ++;
17 } else if (Character.isLetter(ch))

18 letterCount ++;
hp 19 }
20
21
22 System.out.println( "Number of digits : "
digitCount ):
23 System.out . println( "Number of letters:
+ letterCount );
24
25 }
26
n.java Run Output

import java.util.Scanner; java -cp /tmp/c1BCj qw1 s4 CountDigitsAn


Enter a string: john birthday 23 may
public class CountDigitsAndLetters {
public static void main(String[] args) { Number of digits: 2
Scanner scanner = new Scanner(System.in Number of letters: 15

System.out .print("Enter a string: "):


String input Scanner.nextLine();
scanner.close();

int digitCount =0;


int letterCount 0;

for (int i =0; i < input.length(); i


++) {
char ch =input.charAt(i);
if (Character.isDigit (ch)) {
digitCount++ ;
} else if (Character.isLetter (ch))
{
letterCount t+;

System.out.printIn ("\nNumber of digits:


+ digitCount );
System.out.println("Number of letters:
+ letterCount);
1 import java.util.Scanner ; java -cp /tmp/clBCj qw1s4 PasswordValidation
2 Input your password: KINIatg123@
3- public class PasswordValidation { Valid Password
public static void main(Stringl] args) {
5 Scanner scanner = new Scanner (System. in
):
6 System.out .print(" Input your password:
"):
7 String password = scanner. nextLine(0:
8 scanner.close():
9
10 if (isValidPas Sword(password)) {
1 System.out.println("Valid Password"
).
12 } else {
13 System. out .println("Not a Valid
Password");
14 }
15
16
17 public static boolean isValidPassword
(String password) {
18 if (pas sWord. length() <6 | | password
.length() > 16) {
19 return false;
20 }
21
22 boolean hasLowerCase = false;
23 boolean hasUpperCase = false;
24 boolean hasNumber = false;
25 boolean hasSpecialChar = false;
26 String specialCharacters = "$#@";
27
28 for (char ch : password. toCharArray())

29 if (Character. isLower Case(ch)) {


30 hasLowerCase = true;
31 - } else if (Character. isUpperCase(ch
)) {
32 hasUpperCase = true;
33 - } else if (Character . isDigit(ch) ) {
34 hasNumber = true;
35 } else if (special Characters
.contains(String.value0f(ch) ))
36 hasspecialChar = true;
37
38 }
39
40 return hasLowerCase &8& hasUpperCase &&
hasNumber && hasSpecialChar ;:
41
1Va Run Output Clear

olic class EvenDigitNumbers { java -cp /tmp/clBCj qw1 s4 EvenDigitNumbers


public static void main(String[] args) { Numbers with all even digits between 100 and 400:
System.out. println("Numbers with all 200, 202, 204, 206, 208, 220, 222, 224, 226, 228, 240
even digits between 100 and 400: "); ,242, 244, 246, 248, 260, 262, 264, 266, 268,
boolean firstNumberPrinted = false; 280, 282, 284, 286, 288, 400

for (int num = 100; num <= 400; num++)


{
if (hasAllEvenDigits (num) ) {
if (firstNumberPrinted) {
System.out .print(", "):

System.out.print(num) ;
firstNumber Printed = true;

System. out. println();

public static boolean hasAllEvenDigits (int


number ) {
while (number > 0) {
int digit = number % 10;
if (digit % 2 != 0) {
return false;

number /= 10;

return true;
Trogramiz Katalon Java
LEARN MORE > Certification
Online Java
Compiler
A Manual QA's Favorite IDE

Main.java Run Output Clear

1- import java.util.Scanner; java -cp /tmp/6tbarPLdVU MonthToDays Converter


2 Enter the name of a month: may
3- public class MonthToDaysConverter {
4 - public static void main(String[] args) { may has 31 days.
Scanner scanner = new Scanner (System.in
):
5
System.out.print("Enter the name of a
month:
7 String monthName = scanner.nextLine();
8 Scanner.close():

10 int days getDays I nMonth(monthName);


11 if (days != -1) {
12 System.out.println(monthName
has "+ days +" days. "):
13 } else {
14 System.out. println(" Invalid month
JS name. ");
15 }
GO 16
17

ptip 18 public static int getDaysInMonth(String


monthName) {
19 monthName = monthName. toLowerCase():
20 Switch (monthName ) {
21 case "january":
22 case "march":
23 case "may" :
24 case "july":
25 case "august":
2¬6 case "october":
27 case "december":
28 return 31;
29 case "april":
30 case "june":
31 case "september":
32 case "november":
33 return 30;
34 case "february":
35 return 28; // As suming non-leap
year
36 default:
37 return -1:
38 }
39
40 }
Main.java Run Output
1 import java.util. Scanner; java -cp /tmp/DZaATE5Bwo SumwithRange
2 Enter the first integer: 60
3 public class SumWithRangeCheck { Enter the second integer: 60
4 public static void main(String[J args) { Sum: 200
5 Scanner scanner = new Scanner (System. in
):
6 System. out . print ("Enter the first
integer: "):
7 int num1 = scanner.nextInt0;
8 System.out. print ("Enter the second
integer : ");
9 int num2 = scanner.nextInt();
10 Scanner.close();
11
12 int sum = num1 + num2;

13
14 - if (sum >= 105 && Sum <= 200) {
15 Sum = 200:

16 }
17
18 System.out . println("Sum: " + sum);
19 }
20 }
21
rogram1z PPFAS Mutual Fund
nline Java
Dmpiler
Parag Parikh Tax Saver Fund
Main.java Run Output
1 public class Number Pattern { java -cp /tmp/DZ
2 public static void main(String[] args) { 999999999
3 int rowS = 9; 88888888
4 7777777
5 for (int i = rows; i >= 1; i--) { 666666
6 for (int j = 1; j <= i; j++) { 55555

System.out.print(i); 4444
333
9 System.out.println(): 22
10 1
11
12 }
13

You might also like