Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

PowerCalculator2

import java.util.Scanner;
import java.math.BigInteger;
/**
 * Performs unbounded integer exponetiation with a user-specified base
 * and exponent.
 * 
 * @author Jackson Sottile
 */
public class PowerCalculator2 {
    
    public static void main(String[]args) {
        //prompt user and read input
        System.out.print("Enter two integers (base and exponent): ");
        Scanner sc = new Scanner(System.in);
        String baseString = sc.next();
        int exponent = sc.nextInt();
        
        //calculate result
        BigInteger base = new BigInteger(baseString);
        BigInteger result = base.pow(exponent);
        //format and display output
        String output = baseString + "^" + exponent + " = " + result;
        System.out.println(output);
    }
     
}

PowerCalculator

/**
 * Performs integer exponentiation with a user-specified base
 * and exponent.
 * 
 * @author Xavier Char-Amato
 */
import java.util.Scanner;

public class PowerCalculator{

     public static void main(String []args){
        System.out.print("Enter two integers (base and exponent): ");
        Scanner keyboard = new Scanner(System.in);
        float num1 = keyboard.nextFloat();
        float num2 = keyboard.nextFloat();
        if (num1 == (int)num1){
            System.out.print((int)num1);
        }
        else{
            System.out.print(num1);
        }
        System.out.print("^");
        
        if (num2 == (int)num2){
            System.out.print((int)num2);
        }
        else{
            System.out.print(num2);}
        System.out.print(" = ");
        
        if(Math.pow(num1,num2) == ((int)Math.pow(num1,num2))){
            System.out.println((int)Math.pow(num1,num2));}
            
        else{
            System.out.println(Math.pow(num1,num2));
        }
        
        
     }
}

swap without temp

public class MySwapingTwoNumbers 
{
 
    public static void main(String a[])
    {
        int x = 10;
        int y = 20;
        System.out.println("Before swap:");
        System.out.println("x value: "+x);
        System.out.println("y value: "+y);
        x = x+y;
        y=x-y;
        x=x-y;
        System.out.println("After swap:");
        System.out.println("x value: "+x);
        System.out.println("y value: "+y);
    }
}

sum1

import java.util.Scanner;
public class HelloWorld{

     public static void main(String []args){
        int sum=1;
        int n;
        Scanner reader=new Scanner(System.in);
        n=reader.nextInt();
        for(int i=0;i<n;i++)
        {
            sum=sum+5*i;
            System.out.println("Sum is "+sum+" and i is  "+i);
        }
     }
}

Remove Duplicates from Sorted Array

public class HelloWorld{

     public static void main(String []args){
          // int[] array = new int[]{1,1,5,5,6,7,8,8};

         test(new int[]{1,1,5,5}, new int[]{1,5});
         test(new int[]{1,1,5,5,6,7,8,8}, new int[]{1,5,6,7,8});
         test(new int[]{1,5}, new int[]{1,5});
         test(new int[]{1}, new int[]{1});
     }
     
     static int removeDups(int[] nums) {
         
          // int index = 1;
          int offSet = 1;
          int i = 0;
          for( i = 0; i < nums.length; i++) {
               // if(index != 0) {
               // int j = i;
               System.out.println("i " + j);
               while(nums[i] == nums[i + offSet]) {
                    System.out.println("offSet " + offSet);
                    if(i + offSet < nums.length - 1) {
                         nums[i + 1] = nums[i + offSet + 1];
                         offSet++;
                    }
                    else { 
                         break;
                    }
                    // System.out.println("inner " + j);
                    if(i + offSet >= nums.length) {
                         // System.out.println("break " + j);
                         break;
                    }
               }
                
                
               // }
             
          }
          return i;
     }
     
      static void test(int[] array, int[] expected) {
           int len = removeDups(array);
          System.out.println(len);
          
          System.out.print("actual   ");
          for(int i=0; i < len; i++) {
               System.out.print(array[i] + ", ");
          }
          System.out.println();
          System.out.print("expected ");
          for(int i=0; i < expected.length; i++) {
               System.out.print(expected[i] + ", ");
          }
          System.out.println();
     }

    
     static int removeDups2(int[] nums) {
         
          int index = 0;
          for( int i = 1; i < nums.length; i++) {
               // if(index != 0) {
               int j = i;
               // System.out.println("outer " + j);
               while(nums[index] == nums[j]) {
                    if(j < nums.length - 1) {
                         nums[j] = nums[j+1];
                    }
                    j++;
                    // System.out.println("inner " + j);
                    if(j >= nums.length) {
                         // System.out.println("break " + j);
                         break;
                    }
               }
               i = j;
               {
                    index++;
               }
                
                
               // }
             
          }
          return index;
     }
}

Swap with Temp var

public class JavaSwapExample
{

 public static void main(String[] args)
 {
  int x = 10;
  int y = 20;

  System.out.println("Before Swapping");
  System.out.println("Value of x is :" + x);
  System.out.println("Value of y is :" + y);

  //swap the value
  swap(x, y);
 }

 private static void swap(int x, int y) {
  int temp = x;
  x = y;
  y = temp;

  System.out.println("After Swapping");
  System.out.println("Value of x is :" + x);
  System.out.println("Value of y is :" + y);
 }
}

https://pt.stackoverflow.com/q/237073/101

import java.util.Scanner;
import java.util.Random;

public class HElloWorld {
    public static void main(String []args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Informe o numero de pessoas no sorteio: ");
        int n = sc.nextInt();
        String pessoa [] = new String [n];
        for (int i = 0; i < n; i++) {
            System.out.println("Informe a " + (i + 1) + "a pessoa: ");
            pessoa[i] = sc.nextLine();
            if (i == 0) sc.nextLine();
        }
        Random rd = new Random(n);
        System.out.println("O vencedor do sorteio e: " + pessoa[rd.nextInt(n)]);
    }
}

//https://pt.stackoverflow.com/q/237073/101

PowerCalculator

import java.util.Scanner;
/**
 * Performs integer exponentiation with a user-selected
 * base and exponent.
 * 
 * @author Connor Landis
 */
public class PowerCalculator {

	public static void main(String[] args) {
    // prompt the user and read input
    System.out.print("Enter two integers (base and exponent): ");
    Scanner in = new Scanner(System.in);
    int base = in.nextInt();
    int exponent = in.nextInt();
    
    // calculate result
    int result = (int) Math.pow(base, exponent);
    
    // format and display result
    String output = base + "^" + exponent + " = " + result;
    System.out.println(output);
	}
}

DiceRoller

import java.util.Random;



/**
 * Simulates rolling a pair of dice
 * 
 * @author Wil levonuk
 * 
 */
public class DiceRoller 
{ 
    
	public static void main(String[] args) 
	{
	    //declaration 
	    int d1,d2;
	    //declare Random object 
	    Random face = new Random();
	    //invokes nextInt method of the random class that will give us a random number between 1-6
	    d1 = face.nextInt(6) + 1; //.nextInt is a method invoking the nextInt method 
	    //6 indexes start at 0 so we get: 0-5. the +1 adds one so we shift from 1-6.
	    d2 = face.nextInt(6) + 1;
	    

	    //Results
	    System.out.println("First Dies Face: " + d1);
	    System.out.println("Second Dies Face: " + d2);
	    System.out.println("Sum Of Both Die: " + (d1 + d2));

	    //system class alreadys has object called out, that is why we do System.out
	}

}

Power Calculator

/**
 * Perforsm integer exponentiation with a user-specified
 * base and exponent.
 * 
 * @author Justin Yoder
 */
import java.util.Scanner;

public class PowerCalculator {
 
     public static void main(String[] args) {       
         
         // prompt the user and read input
         System.out.print("Enter two integers (base and exponent): ");
         Scanner scan = new Scanner(System.in);
         int base = scan.nextInt();
         int exponent = scan.nextInt();
 
         // calculate result 
         int result = (int) Math.pow(base,exponent);
 
         // format and display result
         String output = base + "^" + exponent + " = " + result;
         System.out.println(output);
    }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.