Skip to content

Instantly share code, notes, and snippets.

@alibehzadian
Created August 23, 2024 18:40
Show Gist options
  • Save alibehzadian/4d8882c33e6d4f2900dba4848caa5e9c to your computer and use it in GitHub Desktop.
Save alibehzadian/4d8882c33e6d4f2900dba4848caa5e9c to your computer and use it in GitHub Desktop.
Examples of episode 10 of Java complete course in Persian/Farsi
public class Main1 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = in.nextInt();
while(number > 10_000) {
System.out.print("Number too large!. Try numbers below 10000: ");
number = in.nextInt();
}
int divideCounter = 0;
while (number > 0) {
System.out.printf("%d / 2 = %d %n", number, number /= 2);
divideCounter++;
}
System.out.printf("After %d times dividing by 2, we reached 0! %n", divideCounter);
}
}
public class Main2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number;
do {
System.out.print("Enter a number below 10,000: ");
number = in.nextInt();
} while (number > 10_000);
int divideCounter = 0;
while (number > 0) {
System.out.printf("%d / 2 = %d %n", number, number /= 2);
divideCounter++;
}
System.out.printf("After %d times dividing by 2, we reached 0! %n", divideCounter);
}
}
public class Main3 {
public static void main(String[] args) {
for(int row = 1; row <= 10; row++) {
for(int col = 1; col <= 10; col++) {
System.out.printf("%-5d", (row * col));
}
System.out.println();
}
}
}
public class Main4 {
public static void main(String[] args) {
String[] fruits = {"apple", "banana", "orange", "grape"};
for (int i = 0; i < fruits.length; i++) {
System.out.println(fruits[i]);
}
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
public class Main5 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int[] numbers = {10,19,14,5,7,36,27,29,31,2,0,17};
int foundIndex = -1;
System.out.print("Enter a number to search for: ");
int searchKey = in.nextInt();
for(int i = 0; i < numbers.length; i++) {
if(numbers[i] == searchKey) {
foundIndex = i;
break;
}
}
System.out.println( foundIndex != -1 ? "Search key found at index: " + foundIndex : "Search key not found.");
}
}
public class Main6 {
public static void main(String[] args) {
int[] numbers = {10,19,14,5,7,36,27,29,31,2,0,17};
int evenNumbersCounter = 0;
for (int number : numbers) {
if (number % 2 != 0) {
continue;
}
System.out.println(number);
evenNumbersCounter++;
}
System.out.printf("There are %d even numbers in the array!", evenNumbersCounter);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment