• No results found

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

N/A
N/A
Protected

Academic year: 2021

Share "Assignment No.3. /*-- Program for addition of two numbers using C++ --*/"

Copied!
14
0
0

Loading.... (view fulltext now)

Full text

(1)

/*-- Program for addition of two numbers using C++ --*/

#include<iostream.h>

class add {

private:

int a,b,c;

public:

void getData() {

cout<<"\nEnter value of a:";

cin>>a;

cout<<"\nEnter value of b:";

cin>>b;

}

void addData() {

c=a+b;

}

void displayAll() {

cout<<"Values of all variables:";

cout<<"\n a="<<a;

cout<<"\n b="<<b;

cout<<"\n c="<<c;

} };

int main() {

add obj;

obj.getData();

obj.addData();

obj.displayAll();

return (0);

}

(2)

//---Output of CPP Program to add two integers ---

Enter value of a:30 Enter value of b:40

Values of all variables:

a=30 b=40 c=70

/*-- Program for addition of two numbers using JAVA --*/

import java.util.*;

public class AddNumber {

public static void main(String[] args) {

int a,b,result;

System.out.println("Enter two numbers");

Scanner add =new Scanner(System.in);

a=add.nextInt();

b=add.nextInt();

result=a+b;

System.out.println("result ="+result);

add.close();

} }

//---Output of JAVA Program to add two integers ---

Enter two numbers 10 20

result =30

(3)

/*-- Program for finding palindrome using java class --*/

import java.util.Scanner;

public class palindrome1 {

public static void main(String[] args) {

// input user string

System.out.println("Enter string:");

Scanner value=new Scanner(System.in);

String str=value.next();

//create array of char and reverse array char [] arr=str.toCharArray();

int size=str.length();

char [] rev=new char[size];

for(int i=0;i<size;i++) {

System.out.println(arr [i]);

rev[size-1-i]=arr[i];

}

//compare arrays to check palindrome System.out.println(new String(rev));

boolean palindrome=true;

for(int i=0;i<size;i++) {

if(arr[i]!=rev[i]) {

palindrome=false;

break;

} }

(4)

//display result

if(palindrome==true) {

System.out.println("palindrome");

} else {

System.out.println("not palindrome");

}

value.close();

} }

//--- Output --- Key found at location: 4

Enter string: RSCOE RSCOE EOCSR

not palindrome Enter string: DAD

DAD DAD

palindrome

(5)

/*---- Program for Binary Search using java class ---*/

//--- BinarySearch.java ---

public class BinarySearch {

public void search (int a[],int low, int high,int key) {

int mid;

if (low>high) {

System.out.println("key not found ");

return;

}

mid=(low+high)/2;

if (key==a[mid]) {

System.out.println("key found at location;"+(mid+1));

}

else if(key<a[mid])

search (a,mid-1,high,key);

else if(key>a[mid])

search(a,mid+1,high,key);

} }

(6)

//--- Mainclass.java --- public class Mainclass

{

public static void main(String[] args) {

int a[]={10,20,30,40,50,60,70,80,90,100};

int key=20;

int low=0;

int high=9;

BinarySearch obj=new BinarySearch();

obj.search( a,low,high,key);

} }

//--- Output --- Key found at location: 2

(7)

/*Program for showing the current TIME and DATE using java class */

import java.util.*;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

public class DateTime {

public static void main(String[] args) {

DateFormat d2=new SimpleDateFormat("HH:mm:ss:SS:K ");

DateFormat d1=new SimpleDateFormat("yyyy/MM/dd

HH:mm:ss");

Date d=new Date();

System.out.println("The current Date and Time is "

+d1.format(d));

System.out.println("To Display

Hours:mins:sec:millisec:Hour In AM/PM:"+d2.format(d));

} }

//--- Output ---

The current Date and Time is :2013/03/19 11:00:19 To Display Hours:mins:sec:millisec:Hour In AM/PM :11:00:19:265:11

(8)

Assignment No.7

/*--- Program for Simple Calculator using C++ ---*/

#include<iostream.h>

#include<conio.h>

class Calci {

private:

int a,b,result;

float c,d,res;

public:

void sum() {

cout<<"Enter two numbers:\n";

cin>>a>>b;

result=a+b;

cout<<"Addition="<<result;

}

void sub() {

cout<<"Enter two numbers:\n";

cin>>a>>b;

result=a-b;

cout<<"Subtraction="<<result;

}

void mult() {

cout<<"Enter two numbers:\n";

cin>>a>>b;

result=a*b;

cout<<"Multiplication="<<result;

}

void div() {

cout<<"Enter two numbers:\n";

cin>>c>>d;

res=c/d;

cout<<"Division="<<res;

}

(9)

void mod() {

cout<<"Enter two numbers:\n";

cin>>a>>b;

result=a%b;

cout<<"Remainder after Division="<<result;

} };

void main () {

clrscr();

int choice;

cout<<"Simple Calculator";

cout<<"\n1.Addition\n2.Subtraction\n3.Multiplication\n4.D ivision\n5.Modulus”;

cout<<“\nPlease Enter Your Choice:";

cin>>choice;

Calci c;

switch(choice) {

case 1: c.sum();

break;

case 2: c. sub();

break;

case 3:c.mult();

break;

case 4:c.div();

break;

case 5:c.mod();

break;

default:

cout<<"Invalid choice";

}

getch();

}

(10)

/*---OUTPUT--- Simple Calculator

1. Addition 2. Subtraction 3. Multiplication 4. Division

5. Modulus

Please Enter Your Choice: 4 Enter two numbers:

15 10

Division= 1.5

Simple Calculator 1. Addition

2. Subtraction 3. Multiplication 4. Division

5. Modulus

Please Enter Your Choice: 1 Enter two numbers:

1 100

Addition= 101

(11)

/*--- Program for Simple Calculator using Applet ---*/

import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="Cal" width=300 height=300>

</applet>

*/

public class Cal extends Applet implements ActionListener {

String msg=" ";

int v1,v2,result;

TextField t1;

Button b[]=new Button[10];

Button add,sub,mul,div,clear,mod,EQ;

char OP;

public void init() {

Color k=new Color(120,89,90);

setBackground(k);

t1=new TextField(10);

GridLayout gl=new GridLayout(4,5);

setLayout(gl);

for(int i=0;i<10;i++) {

b[i]=new Button(""+i);

}

add=new Button("add");

sub=new Button("sub");

mul=new Button("mul");

div=new Button("div");

(12)

mod=new Button("mod");

clear=new Button("clear");

EQ=new Button("EQ");

t1.addActionListener(this);

add(t1);

for(int i=0;i<10;i++) {

add(b[i]);

}

add(add);

add(sub);

add(mul);

add(div);

add(mod);

add(clear);

add(EQ);

for(int i=0;i<10;i++) {

b[i].addActionListener(this);

}

add.addActionListener(this);

sub.addActionListener(this);

mul.addActionListener(this);

div.addActionListener(this);

mod.addActionListener(this);

clear.addActionListener(this);

EQ.addActionListener(this);

}

public void actionPerformed(ActionEvent ae) {

String str=ae.getActionCommand();

char ch=str.charAt(0);

if( Character.isDigit(ch)) t1.setText(t1.getText()+str);

(13)

{

v1=Integer.parseInt(t1.getText());

OP='+';

t1.setText("");

}

else if(str.equals("sub")) {

v1=Integer.parseInt(t1.getText());

OP='-';

t1.setText("");

}

else if(str.equals("mul")) {

v1=Integer.parseInt(t1.getText());

OP='*';

t1.setText("");

}

else if(str.equals("div")) {

v1=Integer.parseInt(t1.getText());

OP='/';

t1.setText("");

}

else if(str.equals("mod")) {

v1=Integer.parseInt(t1.getText());

OP='%';

t1.setText("");

}

if(str.equals("EQ")) {

v2=Integer.parseInt(t1.getText());

if(OP=='+')

result=v1+v2;

else if(OP=='-') result=v1-v2;

(14)

else if(OP=='*')

result=v1*v2;

else if(OP=='/') result=v1/v2;

else if(OP=='%') result=v1%v2;

t1.setText(""+result);

}

if(str.equals("clear")) {

t1.setText("");

} }

} //--- Output ---

References

Related documents