1. Member
Join Date
Sep 2009
Posts
2
Rep Power
0

## 3 basic program

Hello !
I was supposed to write 3 basic programs , and I did it, they work.
Im not an expert in java so every helping hand is welcome !

1. Fibonacci number
Java Code:
```import java.util.Scanner;
public class ciagfibonacciego {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner pobrana = new Scanner(System.in);
System.out.println("Podaj wyraz ciagu, ktory chcesz wyznaczyc: ");
int ciag,liczba1,liczba2,wynik,licznik;
ciag = pobrana.nextInt(); // wyraz ciagu ktory chcemy wyznaczyc
liczba1= 1;
liczba2= 2;
wynik=3;
licznik=4;

if (ciag < 0)
{
System.out.println("Podaj liczbe naturalna");
}
else if (ciag==0)
{
System.out.print("Wyrazy F0 ... F");
System.out.print(ciag);
System.out.println(" ciagu Fibonacciego to:");
System.out.println("F0 -> 0");
}
else if (ciag==1)
{
System.out.print("Wyrazy F0 ... F");
System.out.print(ciag);
System.out.println(" ciagu Fibonacciego to:");
System.out.println("F0 -> 0");
System.out.println("F1 -> 1");
}
else if (ciag==2)
{
System.out.print("Wyrazy F0 ... F");
System.out.print(ciag);
System.out.println(" ciagu Fibonacciego to:");
System.out.println("F0 -> 0");
System.out.println("F1 -> 1");
System.out.print("F2 -> ");
System.out.println(liczba1);
}
else if (ciag==3)
{
System.out.print("Wyrazy F0 ... F");
System.out.print(ciag);
System.out.println(" ciagu Fibonacciego to:");
System.out.println("F0 -> 0");
System.out.println("F1 -> 1");
System.out.print("F2 -> ");
System.out.println(liczba1);
System.out.print("F3 -> ");
System.out.println(liczba2);
}
else
{
System.out.print("Wyrazy F0 ... F");
System.out.print(ciag);
System.out.println(" ciagu Fibonacciego to:");
System.out.println("F0 -> 0");
System.out.println("F1 -> 1");
System.out.print("F2 -> ");
System.out.println(liczba1);
System.out.print("F3 -> ");
System.out.println(liczba2);
do {
wynik=liczba1+liczba2;
liczba1=liczba2;
liczba2=wynik;
System.out.print("F");
System.out.print(licznik);
System.out.print(" -> ");
System.out.println(wynik);
licznik++;
}
while (licznik != ciag+1);
}
}
}```
2.Arithmetic progression
Java Code:
```public class arytmetyczny {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

int n,suma;
n=1;
suma=0;
do
{
suma=suma+2*n+1;
System.out.print("2 * "+n);
System.out.print(" + 1 = ");
System.out.println(2*n+1);
n++;
}
while (n <= 10);
System.out.println();
System.out.println();
System.out.println();
System.out.print("Suma = "+suma);
}

}```
3.Factorial

Java Code:
```import java.util.Scanner;
public class eskimossilnia {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Silnia liczby naturalnej n, podaj n :");
Scanner pobrana = new Scanner(System.in);
long liczba;
long wynik=1;
liczba = pobrana.nextLong();

if (liczba<0)
{
System.out.println("Podaj liczbe naturalna ! ! !");
}
else if (liczba==1)
{
System.out.print(liczba);
System.out.print("! = ");
System.out.println("1");
}
else if (liczba==0)
{
System.out.print(liczba);
System.out.print("! = ");
System.out.println("1");
}
else
{
System.out.print(liczba);
System.out.print("! = ");
while ( liczba > 0 )
{
wynik=liczba*wynik;
liczba--;
}
System.out.println(wynik);
}
}
}```
Programs are in Polish if it's problem for you i can translate them !

take care
Last edited by jasskulainen; 09-27-2009 at 08:56 AM.

2. Member
Join Date
Sep 2009
Posts
2
Rep Power
0
my question is

did I write it correctly ?

3. Originally Posted by jasskulainen
my question is

did I write it correctly ?
Well, does it compile OK? Does it run correctly? Is the output as you would expect it should be?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•