java basics exercises with solution !! 07-11-2014, 10:20 PM
#1
i'll be sharing here some really simple java exercises with solution to help you practice while learning java (only for beginners)
i'll start today by sharing the first two exercises !!
1.Write a program called Sum10 that calculates the sum of the first ten
positive integers 1 + 2 + β¦ + 10.
2.Write a program named calculation that reads on the keyboard two inserted integers, calculates their sum, their difference and their product and
displays the results.
i'll start today by sharing the first two exercises !!
1.Write a program called Sum10 that calculates the sum of the first ten
positive integers 1 + 2 + β¦ + 10.
Spoiler:
Code:
public class Sum10{
public static void main(String[]args){
int sum=0;
for(int i=1;i<=10;i++){
sum+=i;
}
System.out.println("the sum of the first ten positive integers : " +sum);
}
}2.Write a program named calculation that reads on the keyboard two inserted integers, calculates their sum, their difference and their product and
displays the results.
Spoiler:
Code:
import java.util.Scanner;
public class Calculation{
public static void main(String[]args){
Scanner in=new Scanner(System.in);
int sum,difference,product;
System.out.println("insert first integer :");
int nbr1=in.nextInt();
System.out.println("insert second integer :");
int nbr2=in.nextInt();
sum=nbr1+nbr2;
product=nbr1*nbr2;
difference=nbr1-nbr2;
System.out.println("the sum of the integers is : " +sum);
System.out.println("the difference of the integers is : " +difference);
System.out.println("the product of the integers is : " +product);
}
}
(This post was last modified: 07-16-2014, 01:19 AM by LordPankake.)



![[+]](https://sinister.li/images/modern/collapse_collapsed.png)

![[Image: wvBFmA5.png]](http://i.imgur.com/wvBFmA5.png)
![[Image: T4OUWZ1.png]](http://i.imgur.com/T4OUWZ1.png)