Advertisement
heysoul_sisypus

swap/fibonacci/factorial

Mar 1st, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package javaapplication85;
  8.  
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author Student
  14.  */
  15. public class JavaApplication85 {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) {
  21.         // TODO code application logic here
  22.         Scanner scan =new Scanner(System.in);
  23.        
  24.         System.out.println("Menu");
  25.         System.out.println("[1] Swap and Set");
  26.         System.out.println("[2] Fibonacci");
  27.         System.out.println("[3] Factorial");
  28.         System.out.print("Choose: ");
  29.         int a=scan.nextInt();
  30.        
  31.         if (a==1) {
  32.         System.out.println("Swap and Sort");
  33.         System.out.println(" ");
  34.         int x,y,temp;
  35.         System.out.print("Num 1: ");
  36.         x=scan.nextInt();
  37.         System.out.print("Num 2: ");
  38.         y=scan.nextInt();
  39.         //swapping
  40.         temp=x;
  41.         x=y;
  42.         y=temp;
  43.         //output
  44.         System.out.println("Swapped numbers:");
  45.         System.out.println("Num 1: "+x);
  46.         System.out.println("Num 2: "+y);
  47.         }
  48.        
  49.         else if (a==2) {
  50.            
  51.         System.out.println("Fibonacci");    
  52.         System.out.print("Num 1: ");
  53.         int n1=scan.nextInt();
  54.         System.out.print("Num 2: ");
  55.         int n2=scan.nextInt();
  56.         int temp =0;
  57.        
  58.         for (int i=1;i<=10;i++){
  59.            
  60.         temp=n1+n2;
  61.         n1=n2;
  62.         n2=temp;
  63.            
  64.         System.out.println(n2);}
  65.         }
  66.        
  67.         else if (a==3) {
  68.          int n,c,fact=1;
  69.          System.out.println("Factorial");
  70.          System.out.print("Enter number: ");
  71.          n=scan.nextInt();
  72.          if (n<0)
  73.          System.out.println("Number should be non-negative!");
  74.           else
  75.           {
  76.           for (c=1;c<=n;c++)    
  77.           fact=fact*c;
  78.           System.out.println("Factorial: "+fact);
  79.           }
  80.         }
  81.        
  82.         else {
  83.         System.out.println("Please choose the right value.");}
  84.            
  85.        
  86.        
  87.        
  88.        
  89.        
  90.     }
  91.    
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement