Advertisement
Spocoman

02. Sum Prime Non Prime

Sep 24th, 2024
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.85 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "strconv"
  6. )
  7.  
  8. func main() {
  9.     var input string
  10.     fmt.Scanln(&input)
  11.  
  12.     var primeSum = 0
  13.     var nonPrimeSum = 0
  14.    
  15.     for ; input != "stop"; {
  16.         var number, _ = strconv.Atoi(input)
  17.         if number >= 0 {
  18.             if number % 2 == 0 || number % 3 == 0 || number % 5 == 0 {
  19.                 if number == 2 || number == 3 || number == 5 || number == 7 {
  20.                     primeSum += number
  21.                 } else {
  22.                     nonPrimeSum += number
  23.                 }
  24.             } else {
  25.                 primeSum += number
  26.             }
  27.         } else {
  28.             fmt.Println("Number is negative.")
  29.         }
  30.         fmt.Scanln(&input)
  31.     }
  32.  
  33.     fmt.Printf("Sum of all prime numbers is: %d\n", primeSum)
  34.     fmt.Printf("Sum of all non prime numbers is: %d\n", nonPrimeSum)
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement