Hello Guys,
Now I am presenting java program to calculate Arithmetic and Geometric Progression :-
public class ArithmeticAndGeometricProgression {
public static void ArithmeticProgression(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Arithmetic Progression series for Ex: 1 2 3 (Note:- only enter upto 3 entries)");
String gp = sc.nextLine();
String gpArr[] = gp.split(" ");
int num1 = Integer.parseInt(gpArr[0]);
int num2 = Integer.parseInt(gpArr[1]);
int num3 = Integer.parseInt(gpArr[2]);
int r = num2-num1;
int temp = 2*r+num1;
if(temp == num3){
System.out.println("please enter the term");
int term = sc.nextInt();
System.out.println("press 1 if you want to calculate the addition upto given term");
System.out.println("press 2 if you want to find the number on given term");
int choice = sc.nextInt();
if(choice == 1){
float sum1 = term/2 ;
float sum = (2*num1+(term-1)*r)*sum1;
System.out.println("Addition upto "+term+" is "+sum);
}else{
float ans = num1+(term-1)*r;
System.out.println("The "+term+" position in series is "+ans);
}
}else{
System.out.println("it's not a Arithmetic Progression series");
}
}
public static void GeometricProgression(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Geometric Progression series for Ex: 1 3 9 (Note:- only enter upto 3 entries)");
String gp = sc.nextLine();
String gpArr[] = gp.split(" ");
int num1 = Integer.parseInt(gpArr[0]);
int num2 = Integer.parseInt(gpArr[1]);
int num3 = Integer.parseInt(gpArr[2]);
int r = num2/num1;
if(r!=1){
int temp = r*r*num1;
if(temp == num3){
System.out.println("please enter the term");
int term = sc.nextInt();
System.out.println("press 1 if you want to calculate the addition upto given term");
System.out.println("press 2 if you want to find the number on given term");
int choice = sc.nextInt();
if(choice == 1){
float sum1 = num1*(1-(float)Math.pow(r, term));
float sum = sum1/((float)1-r);
System.out.println("Addition upto "+term+" is "+sum);
}else{
int d = term-1;
float ans = num1*(float)Math.pow(r, d);
System.out.println("The "+term+" position in series is "+ans);
}
}else{
System.out.println("it's not a Geometric Progression series");
}
}else{
System.out.println("it's not a Geometric Progression series");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Press 1 for Arithmatic Progression series.");
System.out.println("Press 2 for Geometric Progression series.");
int choice = sc.nextInt();
if(choice == 1){
ArithmeticProgression();
}else{
GeometricProgression();
}
}
}
Now I am presenting java program to calculate Arithmetic and Geometric Progression :-
public class ArithmeticAndGeometricProgression {
public static void ArithmeticProgression(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Arithmetic Progression series for Ex: 1 2 3 (Note:- only enter upto 3 entries)");
String gp = sc.nextLine();
String gpArr[] = gp.split(" ");
int num1 = Integer.parseInt(gpArr[0]);
int num2 = Integer.parseInt(gpArr[1]);
int num3 = Integer.parseInt(gpArr[2]);
int r = num2-num1;
int temp = 2*r+num1;
if(temp == num3){
System.out.println("please enter the term");
int term = sc.nextInt();
System.out.println("press 1 if you want to calculate the addition upto given term");
System.out.println("press 2 if you want to find the number on given term");
int choice = sc.nextInt();
if(choice == 1){
float sum1 = term/2 ;
float sum = (2*num1+(term-1)*r)*sum1;
System.out.println("Addition upto "+term+" is "+sum);
}else{
float ans = num1+(term-1)*r;
System.out.println("The "+term+" position in series is "+ans);
}
}else{
System.out.println("it's not a Arithmetic Progression series");
}
}
public static void GeometricProgression(){
Scanner sc = new Scanner(System.in);
System.out.println("Enter Geometric Progression series for Ex: 1 3 9 (Note:- only enter upto 3 entries)");
String gp = sc.nextLine();
String gpArr[] = gp.split(" ");
int num1 = Integer.parseInt(gpArr[0]);
int num2 = Integer.parseInt(gpArr[1]);
int num3 = Integer.parseInt(gpArr[2]);
int r = num2/num1;
if(r!=1){
int temp = r*r*num1;
if(temp == num3){
System.out.println("please enter the term");
int term = sc.nextInt();
System.out.println("press 1 if you want to calculate the addition upto given term");
System.out.println("press 2 if you want to find the number on given term");
int choice = sc.nextInt();
if(choice == 1){
float sum1 = num1*(1-(float)Math.pow(r, term));
float sum = sum1/((float)1-r);
System.out.println("Addition upto "+term+" is "+sum);
}else{
int d = term-1;
float ans = num1*(float)Math.pow(r, d);
System.out.println("The "+term+" position in series is "+ans);
}
}else{
System.out.println("it's not a Geometric Progression series");
}
}else{
System.out.println("it's not a Geometric Progression series");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Press 1 for Arithmatic Progression series.");
System.out.println("Press 2 for Geometric Progression series.");
int choice = sc.nextInt();
if(choice == 1){
ArithmeticProgression();
}else{
GeometricProgression();
}
}
}
Comments
Post a Comment