Java vs Python: The Ultimate Programming Showdown #programming #python #java #javascript #shorts

preview_player
Показать описание
Java vs Python: The Ultimate Programming Showdown #programming #python #java #javascript #shorts
#webdevelopment #webdesign #webdeveloper #digitalmarketing #website #html #seo #websitedesign #coding #programming #javascript #css #webdesigner #developer #wordpress #marketing #programmer #web #ecommerce #business #websitedevelopment #branding #graphicdesign #socialmediamarketing #software #python #technology #design #softwaredeveloper #coder
Рекомендации по теме
Комментарии
Автор

Actually I can remove 3 more lines in Java code

Edit: Here's the code

Case - 1: If no print statements are there
import java.util.Scanner;

public class SumOfNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
for (int n = sc.nextInt(), sum = 0; n-- > 0; sum += sc.nextInt())
if (n == 0) System.out.println(sum);
}
}


Case - 2: Print statements are there:
import java.util.Scanner;

public class SumOfNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter no. of elements: ");
int n = sc.nextInt(), sum = 0;
System.out.println("Enter the elements: ");
for (int i = 0; i < n; sum += sc.nextInt(), i++);
System.out.println("Sum of numbers: " + sum);
}
}

Edit - 2: This is the shortest thing I can do.
import java.util.Scanner;
import java.util.stream.IntStream;

public class SumOfNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Sum: " + IntStream.range(0, sc.nextInt()).map(i -> sc.nextInt()).sum());
}
}


Sorry for this type of comment. Because I like java

hacker_boy_
join shbcf.ru