[Solved] Java number sign and asterisk [closed]

You need to keep printing space in inner loop and once it come out from the inner loop print * and/or # accordingly. Scanner in = new Scanner(System.in); int x=0; System.out.println(“Enter number: “); x = in.nextInt(); for(int i=1;i<=x;i++){ for(int j=1;j<=i;j++){ System.out.print(” “); } if(i%2==1) { System.out.println(“*”); }else { System.out.println(“#”); } } >>>Demo Link<<< 0 solved … Read more

[Solved] Recursive function without loop

First thing you should consider when using recursion is the exit state; which means when the function will be exit public static int findMin(int[] numbers, int startIndex, int endIndex) { if(startIndex == endIndex){ return numbers[startIndex]; } int min = findMin(numbers, startIndex+1, endIndex); return numbers[startIndex] < min ? numbers[startIndex] : min ; } 0 solved Recursive … Read more

[Solved] Looping – amount of years from months

thanks to everyone who gave a answer 😀 much appreciated i got it working like this in the end int employmentInMonthsAmount = this.MaxEmploymentHistoryInMonths; var intcounter = 0; int years = 0; for (var i = 0; i < employmentInMonthsAmount; i++) { if (intcounter >= 12) { years++; intcounter = 0; } intcounter++; } var monthsleft … Read more

[Solved] C Looping a switch using Y/N options. Choosing N doesn’t close the program

change scanf(“%s”, &input); to scanf(” %c”, &input); and get rid of getchar()s at the end. It will work as you wanted. %c specifier reads a char character, but it does not omit whitespace characters. Putting space onto scanf’s format will deal with whitespaces and read first non-whitespace char. To be more clear, see what man … Read more

[Solved] C++ Loops forever [closed]

A do–while statement loops as long as the while expression is true. Your while expression is choice != ‘c’ || choice != ‘n’ In common English, that expression means choice is not ‘c’ OR choice is not ‘n’ That statement, logically, is always true. choice is always not one of those things. In both English … Read more

[Solved] % (examples in loop) [closed]

One particular use is to check even / odd: for($i=0; $i<10; $i++) { if($i%2==0) echo “even” else echo “odd” } This idea could be used to color rows (tr) of a table differently for better presentation. Another use is to close TR dynamically in a complex code (lets say, change tr after every 4 tds). … Read more

[Solved] Optimizing a while loop in Java

Your code is pretty fast (O(n)), but this problem can be solved in constant time (O(1)), since it is just based on the condition of the intersection of two lines being an integer. static String kangaroo(int k1, int v1, int k2, int v2) { float x = (k2 – k1)/(v1 – v2); if(x == (int) … Read more

[Solved] How can i shorten this python code [closed]

You can use a for loop. range(5) loops 5 times whatever is inside it. The _ is to indicate that you don’t want to use the value returned by the iterator (In this case range(5)) for _ in range(5): if (value0 == 100): send_rckey(rc_exit) else: send_rckey(rc_program_up) value0 = checkPicture(15) For better understanding about for loop … Read more