Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
Distributive Computing
LAB NO: 1
1. Write a program in java to show the working of calculator.
package lab1;
import java.io.*;
import java.util.Scanner;
class calc{
public void calc()
{
}
public void add(int a, int b)
{
int c;
c=a+b;
System.out.println("Answer:"+ c);
}
public void sub(int a, int b)
{
int c;
c=a-b;
System.out.println("Answer:"+ c);
}
public void mul(int a, int b)
{
int c;
c=a*b;
System.out.println("Answer:"+ c);
}
public void div(int a, int b)
{
int c;
c=a/b;
System.out.println("Answer:"+ c);
}
}
public class Lab1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Distributive Computing
int a,b = 0;
int choice;
System.out.println("Calculator");
Scanner input=new Scanner (System.in);
do
{
System.out.println("FOR ADD PRESS 1");
System.out.println("FOR SUBTRACTION PRESS 2");
System.out.println("FOR MULTIPLY PRESS 3");
System.out.println("FOR DIVIDE PRESS 4");
System.out.println("FOR Exit PRESS 5");
System.out.println("Enter choice");
choice=input.nextInt();
if(choice==1)
{
System.out.println("Enter a string");
a=input.nextInt();
System.out.println("Enter a string");
b=input.nextInt();
calc c1=new calc();
c1.add(a,b);
}
if(choice==2)
{
System.out.println("Enter a string");
a=input.nextInt();
System.out.println("Enter a string");
b=input.nextInt();
calc c1=new calc();
c1.sub(a,b);
}
if(choice==3)
{
System.out.println("Enter a string");
a=input.nextInt();
System.out.println("Enter a string");
b=input.nextInt();
calc c1=new calc();
c1.mul(a,b);
}
if(choice==4)
{
System.out.println("Enter a string");
a=input.nextInt();
System.out.println("Enter a string");
b=input.nextInt();
calc c1=new calc();
c1.div(a,b);
Distributive Computing
}
}while(true);
}
}
2. Write a program to display your name and address.
package lab1;
import java.io.*;
import java.util.Scanner;
public class Lab1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int choice;
Scanner input=new Scanner (System.in);
System.out.println("ENter NAME");
Distributive Computing
String a = input.nextLine();
System.out.println("ENter ADDRESS");
String b = input.nextLine();
System.out.println("NAME " + a + " ADDRESS "+ b);
}
}
3. Write a program to multiply a number by 2 without using multiplication.
package lab1;
import java.io.*;
import java.util.Scanner;
public class Lab1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner input=new Scanner (System.in);
int a = 2;
int c = 0;
c= (a << 2);
System.out.println("a << 2 = " + c );
}
}
Distributive Computing
4.
Fill up the missing information.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package lab1_5;
import java.io.*;
import java.lang.*;
import java.util.Scanner;
/**
*
* @author spring2014
*/
public class Lab1_5 {
static public boolean HelperConvertNumberToText(int num, String[] result)
{
String [] strones = {
"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen",
"Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen",
};
String [] strtens = {
"Ten", "Twenty", "Thirty", "Fourty", "Fifty", "Sixty",
"Seventy", "Eighty", "Ninety", "Hundred"
};
Distributive Computing
result[0] = "";
int single, tens, hundreds;
if(num > 1000)
return false;
hundreds = num / 100;
num = num - hundreds * 100;
if( num < 20)
{
tens = 0; // special case
single = num;
}
else
{
tens = num / 10;
num = num - tens * 10;
single = num;
}
if(hundreds > 0)
{
result[0] += strones[hundreds-1];
result[0] += " Hundred ";
Distributive Computing
}
if(tens > 0)
{
result[0] += strtens[tens - 1];
result[0] += " ";
}
if(single > 0)
{
result[0] += strones[single - 1];
result[0] += " ";
}
return true;
}
public static boolean ConvertNumberToText(int num, String[] result)
{
String tempString[] = new String[1];
tempString[0] = "";
int thousands;
int temp;
result[0] = "";
if(num < 0 || num > 100000)
{
System.out.println(num + " \tNot Supported");
Distributive Computing
return false;
}
if( num == 0)
{
System.out.println(num + " \tZero");
return false;
}
if(num < 1000)
{
HelperConvertNumberToText(num, tempString);
result[0] += tempString[0];
}
else
{
thousands = num / 1000;
temp = num - thousands * 1000;
HelperConvertNumberToText(thousands, tempString);
result[0] += tempString[0];
result[0] += "Thousand ";
HelperConvertNumberToText(temp, tempString);
result[0] += tempString[0];
}
return true;
}
/**
Distributive Computing
* @param the command line arguments
*/
public static void main(String[] args) {
Scanner inp=new Scanner(System.in);
char opt;
do
{
System.out.println("Enter Number");
int num=inp.nextInt();
System.out.println(" Number In words");
String [] result = new String[1];
result[0] = "";
int i, num2;
int [] arrNum ={num};
for (i = 0; i < arrNum.length; i++)
{
num2 = arrNum[i];
if( ConvertNumberToText(num2, result) == true)
System.out.println(num2 + "\t" + result[0]);
}
System.out.println("Number divided by 2");
System.out.println(num/2);
System.out.println("Number Multiplied by 2");
System.out.println(num*2);
System.out.println("Odd numbers less than the number");
int j=1;
while(j<num)
{
if(j%2!=0)
{
System.out.println(j);
}
j++;
}
System.out.println("again?");
opt=inp.next().charAt(0);
if(num>=100 && num<=500)
{
System.out.println("Number is in between 100 and 500");
}
}
while(opt=='y');
Distributive Computing
// TODO code application logic here
}
}
Distributive Computing