MR_Whitehat

Arithmetic Progression

Mar 30th, 2020
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.05 KB | None | 0 0
  1. package com.akshat.number_programs;
  2.  
  3. //PROGRAM DEVELOPED BY AKSHAT DODHIYA
  4.  
  5. import java.io.*;
  6. import java.lang.*;
  7.  
  8. public class Arithmetic_Progression {
  9.     public static void main(String[] args)throws IOException {
  10.        
  11.         InputStreamReader isr = new InputStreamReader(System.in);
  12.         BufferedReader br = new BufferedReader(isr);
  13.         BufferedReader br2 = new BufferedReader(isr);//br2 is used because while executing the program, error of "empty string" is shown by the java compiler
  14.  
  15.         System.out.println("To Calculate Difference terms of Arithmetic Progression you choices are :\n First term = a \n Last term = l \n Number of terms = n \n Common Difference = d");
  16.         //Showing the choices to the user
  17.         System.out.println("\nEnter your choice");
  18.         char ch = (char) br2.read();
  19.  
  20.         double ans = 0.0;
  21.         int flag = 0;
  22.  
  23.         if (ch=='a'||ch=='A') {
  24.             System.out.println("Enter the common difference");
  25.             double d = Double.parseDouble(br.readLine());
  26.  
  27.             System.out.println("Enter total number of terms");
  28.             int n = Integer.parseInt(br.readLine());
  29.  
  30.             System.out.println("Enter the last term");
  31.             double l = Double.parseDouble(br.readLine());
  32.  
  33.             ans = l - (n-1)*d;//formula to find first term of AP
  34.         }
  35.         else if (ch=='l'|| ch=='L') {
  36.             System.out.println("Enter the first term");
  37.             double a = Double.parseDouble(br.readLine());
  38.  
  39.             System.out.println("Enter the common difference");
  40.             double d = Double.parseDouble(br.readLine());
  41.  
  42.             System.out.println("Enter total number of terms");
  43.             int n = Integer.parseInt(br.readLine());
  44.  
  45.             ans = a + (n-1)*d;//formula to find last term of AP
  46.         }
  47.         else if (ch=='d'||ch=='D') {
  48.             System.out.println("Enter the first term");
  49.             double a = Double.parseDouble(br.readLine());
  50.  
  51.             System.out.println("Enter total number of terms");
  52.             int n = Integer.parseInt(br.readLine());
  53.  
  54.             System.out.println("Enter the last term");
  55.             double l = Double.parseDouble(br.readLine());
  56.  
  57.             ans  = (l - a)/ (n-1.0);//formula to find common difference of an AP
  58.         }
  59.         else if (ch=='n'||ch=='N') {
  60.             System.out.println("Enter the first term");
  61.             double a = Double.parseDouble(br.readLine());
  62.  
  63.             System.out.println("Enter the common difference");
  64.             double d = Double.parseDouble(br.readLine());
  65.  
  66.             System.out.println("Enter the last term");
  67.             double l = Double.parseDouble(br.readLine());
  68.  
  69.             ans = ((l - a)/d) + 1;//formula to find number of terms in an AP
  70.         }
  71.         else {
  72.             System.out.println("Invalid Choice : Please Enter a valid choice");
  73.             flag = 1;//to prevent double output flag is used
  74.         }
  75.         if (flag==0) {
  76.             System.out.println("Your Answer is "+(int)ans);//showing output in int data type
  77.         }
  78.  
  79.     }
  80. }
Add Comment
Please, Sign In to add comment