In: Computer Science
Program Name: Divisible.
Write a program that calculates the number of integers in a range from 1 to some user-specified upper bound that are divisible by 3, the sum of those integers, and the number and sum of those integers not divisible by 3. Your program must display meaningful output which includes: the selected upper bound number of integers divisible by 3 sum of integers divisible by 3 number of integers not divisible by 3 sum of integers not divisible by 3
IN JAVA
Source Code:
import java.util.Scanner;
class Main{
public static void main(String[] args) {
Scanner scnr=new
Scanner(System.in);
System.out.print("Enter an upper
bound value: ");
int
upper_bound=scnr.nextInt();
int
count_divisible=0,sum_divisible=0;
int
count_notDivisible=0,sum_notDivisible=0;
for(int
i=1;i<=upper_bound;i++){
if(i%3==0){
count_divisible=count_divisible+1;
sum_divisible=sum_divisible+i;
}
else{
count_notDivisible=count_notDivisible+1;
sum_notDivisible=sum_notDivisible+i;
}
}
System.out.println("No. of Integers
divisible by 3 from 1 to "+upper_bound+": "+count_divisible);
System.out.println("Sum of Integers
divisible by 3 from 1 to "+upper_bound+": "+sum_divisible);
System.out.println("No. of Integers
not divisible by 3 from 1 to "+upper_bound+":
"+count_notDivisible);
System.out.println("Sum of Integers
divisible by 3 from 1 to "+upper_bound+":
"+sum_notDivisible);
}
}