Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:23:05+00:00 2026-06-07T17:23:05+00:00

hi guys am kinda new to the language i made this simple program about

  • 0

hi guys am kinda new to the language i made this simple program about Fibonacci
and the program works fine but there is strange behavior when i try to get the average number

public class fibonacci {

/**
 * @param args
 */
public static int fibonaccifun(int number)

{
    int firstvar=1;
    int secondvar=0,total=0,sum=0;
    for(int i=0;i<number;i++)
    {
        total =firstvar+secondvar;
        System.out.println(total);
        firstvar=secondvar;
        secondvar=total;
        sum+=total;

    }

return sum;
}

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



     float aver= (float) ( fibonacci.fibonaccifun(5)/5);
    System.out.println(aver);

………………………………

when i try

float aver= (float) ( fibonacci.fibonaccifun(5))/5; the result is 2.4 which is the correct value ,however when i do this

float aver= ((float) fibonacci.fibonaccifun(5)/5); the average =2.0;

i dont know why it do this so can anyone helps me explaining this ,thx guys.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:23:07+00:00Added an answer on June 7, 2026 at 5:23 pm

    because in the second case

    fibonacci.fibonaccifun(5)/5

    goes to 2, as both the result of fibonaccifun and 5 are ints, THEN you cast to float. (If you divide 2 ints, the result is an int, and ints obviously can’t have decimals)

    In the first case

    (float) ( fibonacci.fibonaccifun(5))

    makes the result of fibonaccifun a float, THEN you do the division.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It may seem to be very easy for you guys but I'm kinda new
I'm kinda new here and I've got a programming/coding related question you guys maybe
I'm kinda new to WPF and .Net programming in general. I have downloaded this
Hey guys, sorry I'm kind of new to programming, but I had a small
Hay guys. I'm kinda new to OOP in PHP. I've learnt how to write
HI Guys, Im kinda new to php and xml so pls bear with me.
I'm new to Qt. This is simply great. I was wondering whether you guys
im kinda new to javascript i mean i know the syntax but not so
I'm kinda new to Jquery but doing my best to be better. I have
guys i have arrays in which i have to match this kind of text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.