Tuesday 17 March 2009

Binary Searh+sort+Seq sort

#include
#define size 10

void sort ( int[]);
void seqsearch (int[], int);
void binarysearch (int [], int);

void main ()
{
int list[] = {10,19,18,17,16,15,14,13,12,11};
int target;
int i;

printf("\nEnter the value to search : ");
scanf("%d",&target);

seqsearch(list,target);
sort (list);
binarysearch (list,target);

printf("\n\t\tSORTED LIST\n");
for (i=0; i printf("\nList[%d]= %d\n",i,list[i]);

}

void seqsearch(int list[], int t)
{
int i=0;

printf("\n\t\tSequential Search Technique\n");

while(i ++i;

if( i printf("\nTarget was found in location %d\n",i);
else
printf("\nTarget not in the list\n");

}

void sort(int list[])
{
int i,j,temp;

for(i=0; i for( j=i+1; j if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}

void binarysearch(int list[],int t)
{
printf("\n\t\tBinary Search Technique\n");

int mid,first=0, last=size-1;

while(first {
mid=(first+last)/2;

if(t>list[mid])
first=mid+1;
else if(t last=mid-1;
else
break;
}

if(t==list[mid])

printf("\nTarget was found in location %d \n",mid);
else
printf("\nTarget not in the list\n");
}

Combo Seq Sort

#include
#define size 10

void sort ( int[]);
void seqsearch (int[], int);
void binarysearch (int [], int);

void main ()
{
int list[] = {10,19,18,17,15,14,13,12,11};
int target;
int i;

printf("\nEnter the value to search : ");
scanf("%d",&target);

seqsearch(list,target);

sort (list);

printf("\n\t\tSORTED LIST");
for (i=0; i printf("\nList[%d]= %d\n",i,list[i]);

}

void seqsearch(int list[], int t)
{
int i=0;

while(i ++i;

if( i printf("\nTarget was found in location %d\n",i);
else
printf("\nTarget not in the list\n");

}

void sort(int list[])
{
int i,j,temp;

for(i=0; i for( j=i+1; j if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}

Seq Search

#include
#define size 10

void sort ( int[]);
void seqsearch (int[], int);
void binarysearch (int [], int);

void main ()
{
int list[] = {10,19,18,17,15,14,13,12,11};
int target;

printf("\nEnter the value to search : ");
scanf("%d",&target);

seqsearch(list,target);

}

void seqsearch(int list[], int t)
{
int i=0;

while(i ++i;

if( i printf("\nTarget was found in location %d\n",i);
else
printf("\nTarget not in the list\n");

}

Calculate Cost X

import java.util.Scanner;
class calculateCost {
public static void main (String args[]) {
Scanner s = new Scanner(System.in);


int weight=0;
String method=null;
double cost=0.0;

System.out.println("Choose the selected shipping method");
System.out.println("A=Air");
System.out.println("T=Truck");
System.out.println("M=Mail");
String choice=s.next();

if(choice.equals("A"))
{
method="Air";
System.out.println("Enter weight :");
weight=s.nextInt();

if (weight <=8)
cost=2.00;
else if (weight<=16)
cost=3.00;
else
cost=4.50;
}
else if(choice.equals("T"))
{
method="Truck";
System.out.println("Enter weight :");
weight=s.nextInt();

if (weight <=8)
cost=1.50;
else if (weight<=16)
cost=2.35;
else
cost=3.25;
}

else if(choice.equals("M"))
{
method="Mail";
System.out.println("Enter weight :");
weight=s.nextInt();

if (weight <=8)
cost=0.50;
else if (weight<=16)
cost=1.50;
else
cost=2.15;
}
else
System.out.println("Thank You");

System.out.println("Method\tWeight\tCost");
System.out.println("=====================");
Package p = new Package(weight,method,cost);
System.out.println( p.display());




}
}

String Compare strcmp

//Program to key in the password 3 times

#include
#include
#define mypassword "abc123"

void main()
{

char password [20] ;
int i ;

for( i=0; i <3; ++i)
{

printf("\nEnter security password : ");
gets(password);




if (strcmp(password,mypassword)==0)
{
printf("\nCorrect\n");
break;
}
}
if (i==3)
printf("\nYou have key in the password 3 time\n");
}

Monday 16 March 2009

C+ Super Array !!!

#include
#include
#define myfruit "grapes"

void main()
{
char name [20];
char string [20] = "Hello";
char string2 [20] = "How are you";
char dummy[20];
char fruit [20];

printf("\nEnter name : ");
scanf ("%s",&name);

gets(dummy); // to read the enter key and store in the string dummy

printf("\nYour name is %s",name);

printf("\nEnter your name again : ");
gets(name);

printf("\nYour nane is %s\n",name);
strcat(string,name);

printf("\nNow in string is %s",string);

printf("\nThe number of characters in name is %d",strlen(name));

strcpy(string,string2);
printf("\nNow in string is %s",string);

printf("\nThe value return is %d",strcmp("Apple","Papaya"));

printf("\nThe value return is %d",strcmp("Papaya","Apple"));

printf("\nThe value return is %d",strcmp("Apple","Apple"));

printf("\nGuess my favourite fruit ?");

gets(fruit);

if (strcmp(fruit,myfruit)==0)
printf("\nWell done! You guess it right!\n");
else
printf("\nSorry! %s is not my favourite fruit\n",fruit);
}

What is life ?

Thursday 5 March 2009

Inheritance

public class Event {

int NumGuests;

public Event() {
setNumGuests(0);
}

public Event(int g){
setNumGuests (g);
}

private void setNumGuests(int g){
NumGuests = g;
}

public int getNumGuests(){
return NumGuests;
}

public String displayNumGuests () {
return "Number of Guests: "+NumGuests;
}

}

public class DinnerEvent extends Event {

int dinnerChoice;

public DinnerEvent() {
super (0);
setDinnerChoice(' ');
}

public DinnerEvent(int g ,int c) {
super (g);
setDinnerChoice(c);
}


private void setDinnerChoice(int c) {
dinnerChoice=c;
}

public String getDinnerChoice() {
String msg=null;
switch(dinnerChoice) {
case 1 : msg = "Dinner choice is Beef"; break;
case 2 : msg = "Dinner choice is Chicken"; break;
case 3 : msg = "Dinner choice is Fish"; break;
}
return msg;
}

public String displayDinnerEvent() {
return displayNumGuests() +"\n"+getDinnerChoice();
}
}


import java.util.Scanner;
class testevent{
public static void main(String agrs[]) {
Scanner s = new Scanner (System.in);
System.out.println ("Enter the number of quests for the event :");
int nofGuest=s.nextInt();

System.out.println("Dinner Choice: \n1=Beef\n2=Chicken\n3=Fish\nEnter Option: ");
int choice=s.nextInt();
DinnerEvent de= new DinnerEvent(nofGuest,choice);
System.out.println(de.displayDinnerEvent());
System.out.println("\nCalling a method in Event Class--->\n"+de.displayNumGuests());
}
}

N-e-r-D for life XD

Monday 2 March 2009

Recursive Function

#include
int sub (int,int);

void main()
{
int a = 9;
int b = 3;

printf("The answer is %d \n" ,sub(a,b));
}

int sub (int a,int b)
{
if (b==1)
return a;
else
return sub(a,b-1)*9;
}

Thursday 19 February 2009

Method 2 : Test Person

import java.util.Scanner;
class TestPerson {
public static void main (String args[]) {
Scanner s = new Scanner(System.in);
String name;
int age,g;
boolean gender;
double salary;
//Person p;

System.out.println("Name\tAge\tGender\tSalary\tNet Pay");
System.out.println("-----------------------------------------");
for (int i=1; i < 3; i++) {
System.out.print("Enter name: ");
name = s.next();
System.out.print("Enter age: ");
age = s.nextInt();
System.out.print("Enter gender: (1-Male, 2-Female): ");
g = s.nextInt();
if (g==1)
gender = true;
else
gender = false;
System.out.print("Enter salary: ");
salary = s.nextDouble();
Person p = new Person(name,age,gender,salary);
System.out.println( p.display() );
} // for
} // main()
} //class

Method 1: Person

public class Person {
private String name;
private int age;
private boolean gender;
private double salary;

public Person() { // default constructor
setPerson(null,0,false,0.0); //intialize the data for this object
}
public Person(String n, int a, boolean g, double s) { //proper constructor
setPerson(n,a,g,s);
}
private void setPerson(String n, int a, boolean g, double s) { //set method
name = n; age = a; gender = g; salary = s;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender ? "Male" : "Female";
}
public double getSalary() {
return salary;
}
public double getNetSalary() {
return getSalary() - (getSalary()*0.1);
}
public String display() {
return getName()+"\t"+age+"\t"+getGender()+"\t"+salary+"\t"+getNetSalary();
}
}

Tuesday 17 February 2009

LAB

// This is program is to calculate the total mark of exam and assignment

#include

void main ()
{
int mark1 ,mark2 ;
float total1,total2,sum;

printf ("Enter the total mark of the assignment \n");
scanf ("%d", &mark1);

printf ("Enter the total exam of the assignment \n");
scanf ("%d", &mark2);

total1=(mark1*40)/100;

if (total1 <16)
printf ("You have to RETAKE the paper \n");
else
printf ("You have pass the paper \n");

total2=(mark2*60)/100;

if (total1 <24)
printf ("You have to RESIT the paper \n");
else
printf ("You have pass the exam \n");

sum=total1=total2;


if (sum >= 70)
printf ("Grade A %f \n",sum);
else if (sum >= 60 && sum <=69)
printf ("Grade B %f \n",sum);
else
printf ("Grade C %f \n",sum);
}

Monday 16 February 2009

Counting Grade while repeating

// This program is to display the average in grade

#include
void main ()
{
int x;
int mark=0;
int sum=0;
float average=0;
char ans;

do {
for ( x = 0; x <5 ; x++)
{
printf("Enter 5 mark \n");
scanf ( "%d",&mark);
sum=sum+mark;
average = sum/5;
}




if (average>=70)
printf ("Grade A \n");

else if (average>=60 && average <=69)
printf ("Grade B \n");

else if (average>=50 && average <=59)
printf ("Grade C \n");

else if (average>=40 && average <=49)
printf ("Grade D \n");

else if (average<=40)
printf ("Grade E \n");

mark=0;
sum=0;
average=0;

printf ("Do you wan to continue :D \n");
getchar();
scanf ("%c",&ans);

}while ( ans =='y' || ans == 'Y');

}

Monday 9 February 2009

C program While loop

#include
void main ()
{
int x;
int sum = 0;
printf ("Enter a number \n");
scanf ("%d",&x);
while (x != 0)
{
sum=sum+x;
printf ("Enter a number \n");
scanf ("%d",&x);

}
printf ("%d",sum);
}

Friday 6 February 2009

String

import java.util.Scanner;
public class operator {
public static void main (String args[])
{
Scanner s = new Scanner (System.in);
System.out.println (" Enter two number:");

int sum,minus, product, multiple;
float divide;

int x =s.nextInt();
int y =s.nextInt();

System.out.println (" Enter the desire operator | + | - | / | * | % |:");
String operator = s.next();

if (operator.equals( "+"))
{sum = x+y;
System.out.println (" The sum of the 2 number is "+sum);}

else if (operator.equals ("-"))
{minus = x-y;
System.out.println (" The minus of the 2 number is "+minus);}

else if (operator.equals ("/"))
{divide = x/y;
System.out.println (" The divide of the 2 number is "+divide);}

else if (operator.equals ("*"))
{product = x*y;
System.out.println (" The product of the 2 number is "+product);}


else if (operator.equals ("%"))
{multiple = x%y;
System.out.println (" The product of the 2 number is "+multiple);}






}
}

Just for the sake of boring ness

import java.util.Scanner;
public class SwitchCase {
public static void main (String args[])
{
Scanner s = new Scanner (System.in);
System.out.println (" Select 1 - 12 to view the month");

int x =s.nextInt();

switch (x){
case 1:
System.out.println ("January");
break;

case 2:
System.out.println ("Febuary");
break;

case 3:
System.out.println ("March");
break;

case 4:
System.out.println ("April");
break;

case 5:
System.out.println ("May");
break;

case 6:
System.out.println ("June");
break;

case 7:
System.out.println ("July");
break;

case 8:
System.out.println ("August");
break;

case 9:
System.out.println ("September");
break;

case 10:
System.out.println ("October");
break;

case 11:
System.out.println ("November");
break;

case 12:
System.out.println ("December");
break;

default:
System.out.println ("-------------------------");
System.out.println ("|**** | ");
System.out.println ("|**** ( )");
System.out.println ("|**** | ");
System.out.println ("|**** / | \\ ");
System.out.println ("|**** / | \\ ");
System.out.println ("|**** | ");
System.out.println ("|**** / \\ ");
System.out.println ("|**** / \\ ");
System.out.println ("|****");
System.out.println ("-------------------------------------------------------------");
System.out.println ("U have select the wrong number");
break;

}

}
}

Tuesday 3 February 2009

Switch !!

//*Calculate walt

#include
void main ()
{
int x;
printf ("Please enter the light bulb's wattage in the following list.\n");
printf ("|15|25|40|60|75|100|\n");
scanf ("%d",&x);

switch (x){
case 15:

printf ( "The brightness is 125. \n");
break;

case 25:

printf ( "The brightness is 215. \n");
break;

case 40:

printf ( "The brightness is 500. \n");
break;

case 60:

printf ( "The brightness is 880. \n");
break;

case 75:

printf ( "The brightness is 1000. \n");
break;

case 100:

printf ( "The brightness is 1675. \n");
break;
default:

printf ( "The brightness is -1. \n");
break;
}
}

C programming

#include
void main ()
{
float n;
printf ("Please enter the scale of the earthquake.\n");
scanf ("%f",&n);

if (n<5.0)
printf ("Little or no damage. \n");
else if (n >=5.0 && n < 5.5)
printf ( "Some damage. \n");
else if (n >= 5.5 && n < 6.5)
printf ( "Serious damage: walls may crack or fall. \n");
else if (n>=6.5 && n < 7.5)
printf ("Disaster: house and building may collapse. \n");
else if (n>= 7,5)
printf ("Catastrophe: Most building destroyed.\n");
printf ("GG.COM\n");
printf (" * \n");
printf (" ***** \n");
printf (" ***** \n");
printf (" ******* \n");
printf (" ********* \n");
printf (" ************* \n");
printf (" *************** \n");
printf (" ***************** \n");


}

Coding q3 *JAVA*

To calculate sum,product,average,smallest,largest

import java.util.Scanner;
public class Application {
public static void main (String args[])
{
Scanner s = new Scanner (System.in);
System.out.println (" Enter your number:");
int sum, product,smallest,largest;
float average;
int x =s.nextInt();
int y =s.nextInt();
int z =s.nextInt();
sum = x+y+z;
average = (x+y+z)/3;
product = x*y*z;
System.out.println ("The sum of the 3 integer is " +sum);
System.out.println ("The average of the 3 integer is " +average);
System.out.println ("The product of the 3 integer is " +product);
if (x>y && x>z)
System.out.println ("The largest integer is " +x);
else if (y>x && y>z)
System.out.println ("The largest integer is " +y);
else
System.out.println ("The largest integer is " +z);
if (xSystem.out.println ("The smallest integer is " +x);
else if (ySystem.out.println ("The smallest integer is " +y);
else
System.out.println ("The smallest integer is " +z);
}
}

WTF CODE

import java.util.Scanner;
public class q4
{
public static void main (String args[])
{
Scanner s = new Scanner (System.in);
System.out.println (" Enter 5 number:");
int pos = 0,neg = 0,zero = 0,x;
int num1 =s.nextInt();
int num2 =s.nextInt();
int num3 =s.nextInt();
int num4 =s.nextInt();
int num5 =s.nextInt();
if (num1 >0)
pos++;
else if (num1<0)
neg++;
else
zero++;
if (num2 >0)
pos++;
else if (num2<0)
neg++;
else
zero++;
if (num3 >0)
pos++;
else if (num3<0)
neg++;
else
zero++;
if (num4 >0)
pos++;
else if (num4<0)
neg++;
else
zero++;
if (num5 >0)
pos++;
else if (num5<0)
neg++;
else
zero++;
System.out.println ( " The number of posittive numbers are " +pos);
System.out.println ( " The number of negative numbers are " +neg);
System.out.println ( " The number of zero numbers are " +zero);
}
}

Sunday 4 January 2009

Back in K L again

Back in Inti Nilai again !!
Fully recharge and ready to hit the books.... *mebi la*
But to kickstart my 3rd sem in Inti i m gonna visit my favourite spot....the cyber cafe :D jajaja