View Full Version : CSC105 Assignment
massiah
01-11-2004, 10:17 AM
I've finish CSC105 assignment 1.
Here is the source code that I write.
You should study it rather than copy all of my code.
Good Luck!!!
P.S. Any copy or cheating on the describe situation will mean an instance ZERO marks on the assignment AND ALL ASSIGNMENT THEREAFTER "F" on the subject, and withdrawal of all subjects in the semeter.
massiah
01-11-2004, 10:19 AM
public class Fact_Recursive
{
public static long factorial(int n)
{
if (n<=1)
return 1;
else
return n*factorial(n-1);
}
public static void main(String [] args)
{
int num = 5;
long end = 0;
long start = System.currentTimeMillis();
System.out.println("Start time :"+ start);
System.out.println(num+" ! = "+Fact_Recursive.factorial(num));
end = System.currentTimeMillis();
System.out.println("End time :"+ end);
System.out.println("Duration :"+ (end-start)+" milliseconds");
}
}
massiah
01-11-2004, 10:20 AM
public class Fibo_Recursive
{
public static int fibonacci (int n)
{
if (n==1||n==2)
return 1;
else
return fibonacci(n-1)+fibonacci(n-2);
}
public static void main(String[] args)
{
long end = 0;
int num = 8;
long start = System.currentTimeMillis();
System.out.println("Start time :"+ start);
System.out.println("fibo "+num+" is : "+Fibo_Recursive.fibonacci(num));
end = System.currentTimeMillis();
System.out.println("End time :"+ end);
System.out.println("Duration :"+ (end-start)+" milliseconds");
}
}
massiah
01-11-2004, 10:23 AM
public class Fact_Iterative
{
public static int fact(int n)
{
int num=1;
for (int i=1;i<=n;i++)
{
num = num*i;
}
return num;
}
public static void main(String args[])
{
int num = 5;
long end = 0;
long start = System.currentTimeMillis();
System.out.println("Start time :"+ start);
System.out.println(num+" ! = "+fact(num));
end = System.currentTimeMillis();
System.out.println("End time :"+ end);
System.out.println("Duration :"+ (end-start)+" milliseconds");
}
}
massiah
01-11-2004, 10:24 AM
public class Fibo_iterative
{
public static int fibonacci(int n)
{
int a[] =new int [n+1];
int c=0;
a[0]=1;
for (int i=0;i<n;i++)
{
a[i+1]=a[i]+c;
c=a[i];
a[i]=a[i+1];
}
return a[n-2];
}
public static void main(String[] args)
{
int n = 8;
long end = 0;
long start = System.currentTimeMillis();
System.out.println("Start :"+ start);
System.out.println(Fibo_iterative.fibonacci(n));
end = System.currentTimeMillis();
System.out.println("End :"+ end);
System.out.println("Duration :"+ (end-start)+"milliseconds");
}
}
massiah
01-11-2004, 10:25 AM
class TestTime
{
public static void main(String[] args)
{
long end = 0;
long start = System.currentTimeMillis();
System.out.println("Start :"+ start);
for (long i =0;i<1000000 ;i++ );
end = System.currentTimeMillis();
System.out.println("End :"+ end);
System.out.println("Duration :"+ (end-start)+"milliseconds");
}
}
// this is method to test time of processing.
massiah
01-11-2004, 10:28 AM
public class Binary
{
public static void Convert(int n)
{
if (n<=1)
System.out.print(n);
else
{
Convert(n/2);
System.out.print(n%2);
}
}
public static void main(String[] args)
{
int num = 13;
System.out.print("=> You input "+num+" in decimal and in binary is :");
new Binary().Convert(num);
System.out.println("");
}
}
//this is home work#1 this is the source code of program which convert decimal number to binary number.
massiah
01-11-2004, 10:30 AM
from code above, I hope that you don't copy all of them and sent to teacher.
If you have any idea or better code you can post code here.
Thank you very much!!!
massiah
01-11-2004, 04:42 PM
//I'm sorry for code above.
//Some of code is missing.
//Here is the right code.
public class Fibo_iterative
{
public static int fibonacci(int n)
{
int a[] =new int [n+1];
int c=0;
a[0]=1;
for (int i=0;i<n;i++)
{
a[i+1]=a[i]+c;
c=a[i];
a[i]=a[i+1];
}
return a[n-2];
}
public static void main(String[] args)
{ long start = System.currentTimeMillis();
System.out.println("Start :"+ start);
System.out.println(Fibo_iterative.fibonacci(40));
long end = System.currentTimeMillis();
System.out.println("End :"+ end);
System.out.println("Duration :"+ (end-start)+"milliseconds");
}
}
massiah
01-11-2004, 04:44 PM
public class Fibo_iterative
{
public static int fibonacci(int n)
{
int a[] =new int [n+1];
int c=0;
a[0]=1;
for (int i=0;i < n ; i + + )
{
a[i+1]=a[i]+c;
c=a[i];
a[i]=a[i+1];
}
return a[n-2];
}
public static void main(String[] args)
{ long start = System.currentTimeMillis();
System.out.println("Start :"+ start);
System.out.println(Fibo_iterative.fibonacci(40));
long end = System.currentTimeMillis();
System.out.println("End :"+ end);
System.out.println("Duration :"+ (end-start)+"milliseconds");
}
}
asylu3
01-11-2004, 04:58 PM
เวลา post โค้ดให้ใช่แทคนี้นะครับจะได้ดูง่ายๆ
..your code ..
massiah
02-11-2004, 12:02 PM
Here is the code that transform number in decimal to binary and also count number of 1's.
This program use recursive method.
public class Binary
{
public static void Convert(int n)
{
if (n<=1)
System.out.print(n);
else
{
Convert(n/2);
System.out.print(n%2);
}
}
public static int count(int num)
{
if (num<=1)
return num;
if (num%2==1)
return count(num/2)+1;
return count(num/2);
}
public static void main(String[] args)
{
int num = 13;
System.out.print("=> You input "+num+" in decimal and in binary is :");
new Binary().Convert(num);
System.out.println("");
System.out.println("It has "+ count(num)+" of 1's");
}
}
labyrinth
03-11-2004, 09:54 AM
[code]
public static void Convert(int n)
{
Powered by vBulletin® Version 4.2.5 Copyright © 2026 vBulletin Solutions Inc. All rights reserved.