Results 1 to 4 of 4

Thread: 3 basic program

  1. #1
    jasskulainen is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default 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. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    jasskulainen is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    my question is

    did I write it correctly ?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by jasskulainen View Post
    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?

Similar Threads

  1. Basic high score program
    By Implode in forum New To Java
    Replies: 5
    Last Post: 09-03-2009, 05:21 PM
  2. Basic Help
    By robjames in forum New To Java
    Replies: 3
    Last Post: 02-09-2009, 02:58 AM
  3. Need some really basic help
    By Mayur in forum New To Java
    Replies: 6
    Last Post: 01-24-2009, 06:00 AM
  4. Basic Program Please Help!!
    By VinceGuad in forum New To Java
    Replies: 3
    Last Post: 02-01-2008, 03:35 PM
  5. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 05:45 PM

Posting Permissions

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