Tutorialspoint

Programming Examples

PwrCalc 3.0

/**
 * It's a calculator. Big numbers! Dialog boxes! What's not to love?
 * 
 * @author Fenton Krupp
 * @version 3.0
 */

import java.util.Scanner;
import java.math.BigInteger;
import javax.swing.JOptionPane;

public class PwrCalc3 {
   
    public static void main(String []args) {
        // prompt that user and read input. It's visual.
        String prompt  = "Give me two integers and nobody gets hurt: \n";
        String input = JOptionPane.showInputDialog(prompt);
        
        // extract base and expontent 
        Scanner in = new Scanner(input);
        BigInteger base = in.nextBigInteger();
        int exp = in.nextInt();
        BigInteger result = base.pow(exp);

        // format and display (AKA EZ MONEY)
        String output = base + " to the power of " + exp + " is " + result + ".";
        JOptionPane.showMessageDialog(null, output);
    }
}

Compile and Execute C Online

c

#include <stdio.h>
#include <string.h>

char first[] = "Justin";
char last[] = "Hall";
char full[50] = "";

int main(void)
{

    strcat(strcat(strcpy(full, last)," "), first);
    printf("Hello %s.\n\n", full);

    printf("First name is %d characters long.\n", strlen(first));
    printf("Last name is %d characters long.\n", strlen(last));
    printf("Full name with the space is %d characters long. \n\n", strlen(full));
    
    printf("Your initials are %c.%c.\n", first[0], last[0]);
    printf("Your ASCII decimal initials are %d.%d.\n\n", first[0], last[0]);
    
    printf("Program complete. Good bye.\n");

}

Execute SML/NJ Online

(* Jenny Zhong, Lab 2, 09/22/2017 *)

(* Problem 1 *)

(* Example 1 *)
(* r = 3*(3*3)*2 = 54 *)
val m : int = 2
val r : int =
let
    val m : int = 3
    val n : int = m * m
in
    m * n
end * m

(* Example 2 *)
(* test = (3.0+2.0)*(1.0+3.0)*(-1.0+2.0) = 20.0 *)
val x : real = 1.0
val y : real = 2.0
fun f(x : real) : real = x + y
fun g(y : real) : real = x + y
fun h(x : real, y : real, f : real -> real, g : real -> real) : real =
let 
    val x : real = 3.0 
in 
    f(x)
end * g(y) * f(x)
val test:real = h(~1.0,3.0,f,g)

(* Example 3 *)
(* x=2; y=2.0; z="2"; r=2.0 *)
val triple : int*real*string = (2, 2.0, "2")
val pair_of_pairs : (int*int) * (real*real) = ((2, 3), (2.0, 3.0))
val (x : int, y : real, z : string) = triple
val ((_,_), (r : real,_)) = pair_of_pairs

(* Problem 2 *)
(* Error 1: andalso is an operator for combining two boolean variables;
            and is a logical operator for evaluating two expressions. *)
(* Error 2: val needs to be declared *)
(* Error 3: ~ is used for negative *)
(* Error 4: 3 is not of type real, 3.0 is *)
(* Error 5: a pair of pairs is not the same as a quadruple *)
local
type vec = real*real and mat = (real*real) * (real*real)
in
val x : vec = (1.0, 2.0) and y : vec = (~2.0, 3.0) and a : mat = ((1.0, 2.0), (2.0, 1.0))
end

(* Problem 3 *)
local
    type vec = real*real and mat = (real*real) * (real*real)
    
    fun help1 (x:vec, c:real) : vec = 
    let
        val (a : real, b : real) = x
    in
        (c*a, c*b)
    end
    
    fun help2 (x:vec, y:vec) : vec = 
    let
       val (m : real, n : real) = x
       val (p : real, q : real) = y
    in
        (m+p, n+q)
    end
in
    fun mat_vec_mul (a:mat, x:vec) : vec = 
    let
        val ((a11:real, a21:real), (a12:real, a22:real)) = a
        val (x1 : real, x2 : real) = x
        val col1 : vec = (a11, a21)
        val col2 : vec = (a12, a22)
    in
        help2 (help1(col1,x1), help1(col2,x2))
    end
    val test : vec = mat_vec_mul (((1.0,3.0), (2.0,4.0)), (5.0,6.0))

    
    fun rot (x:vec, t:real) : vec = 
    let 
        val rotation_matrix : mat = ((Math.cos(t), Math.sin(t)), (~(Math.sin(t)), Math.cos(t)))
    in
        mat_vec_mul(rotation_matrix, x)
    end
    
end

RangeOfThree

import java.util.Scanner;
import java.util.Random;
import java.lang.*;
/**
 * Write a program that generates three random numbers between 10 and 99 inclusive,
 * and outputs these numbers on one line, then outputs the range (difference between minimum and maximum) on the next line.
 * 
 * author@ Wulff Brinton-Wilson
 */
public class RangeOfThree {

     public static void main(String []args) {
         //Chooses three random numbers and prints them
         Random rand = new Random();
         int number1 = 10 + rand.nextInt(89);
         int number2 = 10 + rand.nextInt(89);
         int number3 = 10 + rand.nextInt(89);
         System.out.println("Random Numbers: "+number1+", "+number2+", "+number3);
         
         //finding the max/min of the three numbers
         int high1 = Math.max(number1,number2);
         int high2 = Math.max(high1,number3);
         
         int low1 = Math.min(number1,number2);
         int low2 = Math.min(low1,number3);
         
         //Print the Range
         System.out.print("Range: "+(high2-low2));
         
        
     }
}

PwrCalc 1.1

/**
 * It's a calculator. Provides error messages for overflow errors.
 * 
 * @author Fenton Krupp
 * @version 1.1
 */

import java.util.Scanner;

public class PwrCalc {
   
    public static void main(String []args) {
        //prompt that user
        System.out.println("Give me two integers and nobody gets hurt: \n");
        Scanner in = new Scanner(System.in);
        int base = in.nextInt();
        int exp = in.nextInt();

        
        //calc this right up
        

        int result = (int) Math.pow(base, exp);
        if (result == 2147483647) {
            System.out.println("Try again kiddo (numbers are too big).");
        }
        else {
            //format and display (AKA EZ MONEY)
            String output = base + " to the power of " + exp + " is " + result + ".";
            System.out.print(output); 
        }


    }
}

Chapter 3 Exercise 1

cpp

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello World" << endl; 
   
   return 0;
}

HalfSwitcher

import java.util.Scanner;
/**
 * Write a program that prompts the user to enter a word and outputs the same word with the second half preceding the first half.
 * 
 * author@ Wulff Brinton-Wilson
 */
public class HalfSwitcher {

     public static void main(String []args) {
        //Takes a word as input from the user
        Scanner in = new Scanner(System.in);
        String word = in.nextLine();
        
        //creates strings for the second and first half of the word
        String first = new String(word.substring(0,word.length()/2));
        String second = new String(word.substring((word.length()/2)+1),word.length());
        
        //Print the solution
        System.out.print(first+second);
     }
}

Compile and Execute C# Sharp Online

using System.IO;
using System;

class Program
{
    
    static string input = "";
    static string description = "";
    
    static void Main()
    {
        input = Console.ReadLine();
        
        string[] splitInput = input.Split('>');
        description = splitInput[0];
        
        Console.WriteLine("Description: " + description);
        
        for(int i = 1; i < splitInput.Length; i++)
        {
            understand(splitInput[i].ToLower());
        }
        
    }
    
    static void understand(string line)
    {
        if(line.StartsWith("implying"))
        {
            string[] splitLine = line.Split(' ');
            if(splitLine[1] == "i" &&  splitLine[2] == "won't")
            {
                if()
            }
        }
        else
            Console.WriteLine("ERR");
    }
    
    public struct variable
    {
        string value;
        public variable(string type)
        {
            value = type + ":";
        }
    }
    
}

wut is that shiet

Module VBModule
 
    Sub Main()
        Dim a As Integer  = 3.5
        Console.WriteLine(a)
        Dim b As Integer  = 2.5
        Console.WriteLine(b)
        Console.WriteLine(int(a/b))
    End Sub
  
End Module

lulzulz

Module VBModule
 
    Sub Main()
        Dim a As Integer  = 3.5
        Dim b As Integer  = 2.5
        Console.WriteLine(int(a/b))
    End Sub
  
End Module

Advertisements
Loading...

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