Creating plsql function for Factorial program












-1















When I call the function using select statement
[select fact(5) from dual;]
I get output as 1.Can you guys please help me from the below code



create or replace function fact(num in number)

return number

Is

res_fact number:=1;

begin

for i in 1..5 loop

res_fact:=res_fact*i;

dbms_output.put_line(res_fact);

-- dbms_output.put_line('Factorial of '||num||' = '||res_fact);

return res_fact;

end loop;

dbms_output.put_line(res_fact);

end;

res_fact=res_fact*i;
as i call function i used to get the factorial of that input number
res_fact=5*4*3*2*1;
res_fact=120









share|improve this question

























  • This seems like homework, so I won't give a direct answer but some hints: What's the argument num supposed to do? What do you want to return and when?

    – sticky bit
    Jan 2 at 5:14


















-1















When I call the function using select statement
[select fact(5) from dual;]
I get output as 1.Can you guys please help me from the below code



create or replace function fact(num in number)

return number

Is

res_fact number:=1;

begin

for i in 1..5 loop

res_fact:=res_fact*i;

dbms_output.put_line(res_fact);

-- dbms_output.put_line('Factorial of '||num||' = '||res_fact);

return res_fact;

end loop;

dbms_output.put_line(res_fact);

end;

res_fact=res_fact*i;
as i call function i used to get the factorial of that input number
res_fact=5*4*3*2*1;
res_fact=120









share|improve this question

























  • This seems like homework, so I won't give a direct answer but some hints: What's the argument num supposed to do? What do you want to return and when?

    – sticky bit
    Jan 2 at 5:14
















-1












-1








-1


2






When I call the function using select statement
[select fact(5) from dual;]
I get output as 1.Can you guys please help me from the below code



create or replace function fact(num in number)

return number

Is

res_fact number:=1;

begin

for i in 1..5 loop

res_fact:=res_fact*i;

dbms_output.put_line(res_fact);

-- dbms_output.put_line('Factorial of '||num||' = '||res_fact);

return res_fact;

end loop;

dbms_output.put_line(res_fact);

end;

res_fact=res_fact*i;
as i call function i used to get the factorial of that input number
res_fact=5*4*3*2*1;
res_fact=120









share|improve this question
















When I call the function using select statement
[select fact(5) from dual;]
I get output as 1.Can you guys please help me from the below code



create or replace function fact(num in number)

return number

Is

res_fact number:=1;

begin

for i in 1..5 loop

res_fact:=res_fact*i;

dbms_output.put_line(res_fact);

-- dbms_output.put_line('Factorial of '||num||' = '||res_fact);

return res_fact;

end loop;

dbms_output.put_line(res_fact);

end;

res_fact=res_fact*i;
as i call function i used to get the factorial of that input number
res_fact=5*4*3*2*1;
res_fact=120






plsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 8:38









Kandy

348414




348414










asked Jan 2 at 5:02









zeal collegezeal college

31




31













  • This seems like homework, so I won't give a direct answer but some hints: What's the argument num supposed to do? What do you want to return and when?

    – sticky bit
    Jan 2 at 5:14





















  • This seems like homework, so I won't give a direct answer but some hints: What's the argument num supposed to do? What do you want to return and when?

    – sticky bit
    Jan 2 at 5:14



















This seems like homework, so I won't give a direct answer but some hints: What's the argument num supposed to do? What do you want to return and when?

– sticky bit
Jan 2 at 5:14







This seems like homework, so I won't give a direct answer but some hints: What's the argument num supposed to do? What do you want to return and when?

– sticky bit
Jan 2 at 5:14














2 Answers
2






active

oldest

votes


















0














Make this code "return res_fact;" out of loop you code will run, But above code work for only 5.






share|improve this answer
























  • I wants to execute function using plsql statements declare v_fact number; begin v_fact:=fact(8); dbms_output.put_line(v_fact); end;

    – zeal college
    Jan 2 at 10:08













  • @zealcollege you have created a function wherever you want use even procedure or select statement

    – Kandy
    Jan 2 at 11:32











  • Can u gave me ur gmail id plz

    – zeal college
    Jan 3 at 9:26











  • @Zealcollege Is there any more issue your are facing..kkr13590@gmail.com

    – Kandy
    Jan 4 at 7:04













  • need to talk to you on hangout

    – zeal college
    Jan 6 at 5:43



















0














Two issues....
Firstly you need to move your return statement outside of your loop. As you now have it it will return on the first pass through the loop.



Secondly you are ignoring your input parameter and always going to get factorial of 5 rather than what is passed in (once you move your return statement.



See code below



create or replace function fact(num in number)
return number
Is
res_fact number:=1;
begin
for i in 1..num loop
res_fact:=res_fact*i;
end loop;
dbms_output.put_line('Factorial of '||num||' = '||res_fact);
return res_fact;
end;





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001441%2fcreating-plsql-function-for-factorial-program%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Make this code "return res_fact;" out of loop you code will run, But above code work for only 5.






    share|improve this answer
























    • I wants to execute function using plsql statements declare v_fact number; begin v_fact:=fact(8); dbms_output.put_line(v_fact); end;

      – zeal college
      Jan 2 at 10:08













    • @zealcollege you have created a function wherever you want use even procedure or select statement

      – Kandy
      Jan 2 at 11:32











    • Can u gave me ur gmail id plz

      – zeal college
      Jan 3 at 9:26











    • @Zealcollege Is there any more issue your are facing..kkr13590@gmail.com

      – Kandy
      Jan 4 at 7:04













    • need to talk to you on hangout

      – zeal college
      Jan 6 at 5:43
















    0














    Make this code "return res_fact;" out of loop you code will run, But above code work for only 5.






    share|improve this answer
























    • I wants to execute function using plsql statements declare v_fact number; begin v_fact:=fact(8); dbms_output.put_line(v_fact); end;

      – zeal college
      Jan 2 at 10:08













    • @zealcollege you have created a function wherever you want use even procedure or select statement

      – Kandy
      Jan 2 at 11:32











    • Can u gave me ur gmail id plz

      – zeal college
      Jan 3 at 9:26











    • @Zealcollege Is there any more issue your are facing..kkr13590@gmail.com

      – Kandy
      Jan 4 at 7:04













    • need to talk to you on hangout

      – zeal college
      Jan 6 at 5:43














    0












    0








    0







    Make this code "return res_fact;" out of loop you code will run, But above code work for only 5.






    share|improve this answer













    Make this code "return res_fact;" out of loop you code will run, But above code work for only 5.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 5:22









    KandyKandy

    348414




    348414













    • I wants to execute function using plsql statements declare v_fact number; begin v_fact:=fact(8); dbms_output.put_line(v_fact); end;

      – zeal college
      Jan 2 at 10:08













    • @zealcollege you have created a function wherever you want use even procedure or select statement

      – Kandy
      Jan 2 at 11:32











    • Can u gave me ur gmail id plz

      – zeal college
      Jan 3 at 9:26











    • @Zealcollege Is there any more issue your are facing..kkr13590@gmail.com

      – Kandy
      Jan 4 at 7:04













    • need to talk to you on hangout

      – zeal college
      Jan 6 at 5:43



















    • I wants to execute function using plsql statements declare v_fact number; begin v_fact:=fact(8); dbms_output.put_line(v_fact); end;

      – zeal college
      Jan 2 at 10:08













    • @zealcollege you have created a function wherever you want use even procedure or select statement

      – Kandy
      Jan 2 at 11:32











    • Can u gave me ur gmail id plz

      – zeal college
      Jan 3 at 9:26











    • @Zealcollege Is there any more issue your are facing..kkr13590@gmail.com

      – Kandy
      Jan 4 at 7:04













    • need to talk to you on hangout

      – zeal college
      Jan 6 at 5:43

















    I wants to execute function using plsql statements declare v_fact number; begin v_fact:=fact(8); dbms_output.put_line(v_fact); end;

    – zeal college
    Jan 2 at 10:08







    I wants to execute function using plsql statements declare v_fact number; begin v_fact:=fact(8); dbms_output.put_line(v_fact); end;

    – zeal college
    Jan 2 at 10:08















    @zealcollege you have created a function wherever you want use even procedure or select statement

    – Kandy
    Jan 2 at 11:32





    @zealcollege you have created a function wherever you want use even procedure or select statement

    – Kandy
    Jan 2 at 11:32













    Can u gave me ur gmail id plz

    – zeal college
    Jan 3 at 9:26





    Can u gave me ur gmail id plz

    – zeal college
    Jan 3 at 9:26













    @Zealcollege Is there any more issue your are facing..kkr13590@gmail.com

    – Kandy
    Jan 4 at 7:04







    @Zealcollege Is there any more issue your are facing..kkr13590@gmail.com

    – Kandy
    Jan 4 at 7:04















    need to talk to you on hangout

    – zeal college
    Jan 6 at 5:43





    need to talk to you on hangout

    – zeal college
    Jan 6 at 5:43













    0














    Two issues....
    Firstly you need to move your return statement outside of your loop. As you now have it it will return on the first pass through the loop.



    Secondly you are ignoring your input parameter and always going to get factorial of 5 rather than what is passed in (once you move your return statement.



    See code below



    create or replace function fact(num in number)
    return number
    Is
    res_fact number:=1;
    begin
    for i in 1..num loop
    res_fact:=res_fact*i;
    end loop;
    dbms_output.put_line('Factorial of '||num||' = '||res_fact);
    return res_fact;
    end;





    share|improve this answer




























      0














      Two issues....
      Firstly you need to move your return statement outside of your loop. As you now have it it will return on the first pass through the loop.



      Secondly you are ignoring your input parameter and always going to get factorial of 5 rather than what is passed in (once you move your return statement.



      See code below



      create or replace function fact(num in number)
      return number
      Is
      res_fact number:=1;
      begin
      for i in 1..num loop
      res_fact:=res_fact*i;
      end loop;
      dbms_output.put_line('Factorial of '||num||' = '||res_fact);
      return res_fact;
      end;





      share|improve this answer


























        0












        0








        0







        Two issues....
        Firstly you need to move your return statement outside of your loop. As you now have it it will return on the first pass through the loop.



        Secondly you are ignoring your input parameter and always going to get factorial of 5 rather than what is passed in (once you move your return statement.



        See code below



        create or replace function fact(num in number)
        return number
        Is
        res_fact number:=1;
        begin
        for i in 1..num loop
        res_fact:=res_fact*i;
        end loop;
        dbms_output.put_line('Factorial of '||num||' = '||res_fact);
        return res_fact;
        end;





        share|improve this answer













        Two issues....
        Firstly you need to move your return statement outside of your loop. As you now have it it will return on the first pass through the loop.



        Secondly you are ignoring your input parameter and always going to get factorial of 5 rather than what is passed in (once you move your return statement.



        See code below



        create or replace function fact(num in number)
        return number
        Is
        res_fact number:=1;
        begin
        for i in 1..num loop
        res_fact:=res_fact*i;
        end loop;
        dbms_output.put_line('Factorial of '||num||' = '||res_fact);
        return res_fact;
        end;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 5:24









        Shaun PetersonShaun Peterson

        1,1231118




        1,1231118






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001441%2fcreating-plsql-function-for-factorial-program%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith