Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. package com.company;
  2. import java.util.Scanner;
  3. public class Main {
  4. public static void main(String[] args) {
  5. Scanner read = new Scanner(System.in);
  6. System.out.print("n = ");
  7. int n = read.nextInt();
  8. int s = 0;
  9.  
  10. for(int i = 1; i <= n; i++){ //ciclul for, care pe rand parcurge toate valorile de la 1 la n
  11. if(i % 2 != 0) s = s + i; //aflu daca i este impar. daca da, o adun la suma finala
  12. }
  13.  
  14. System.out.println("Suma numerelor impare de la 1 la " + n + " este " + s);
  15.  
  16. }
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement