Monday, November 4, 2013

C program how to find the cube of a number using recursion!!


One of my cousin asked this question. So thought of sharing this.  The tricky thing here is, if we use recursion, we dont get power of 3, we will get power of 2,4,6 ... etc. To get the cube, we need to call the recursive function two times, so that we will get the number to the power of 4, then devide that result by given number, so that you will get the given number cube.

Given number cube = number to the power of 4 / number

Below is the C program for finding the cube using recursion.

#include<stdio.h>

int iterator = 2;
int givenValue=0;
int cubeOfNumber(int number)
{
  if(!iterator)
  {
    printf("cube of %d is %d\n",givenValue,number/givenValue);
    return;
  }
  else
  {
    iterator--;
    cubeOfNumber(number*number);
  }
}

int main()
{
  int n=0;
  printf("enter the number to find the cube\n");
  scanf("%d",&givenValue);
  cubeOfNumber(givenValue); 
}

OutPut:

./a.out
enter the number to find the cube
3
cube of 3 is 27
$ ./a.out
enter the number to find the cube
25
cube of 25 is 15625

7 comments:

Mythili rathinam said...

This code is not working.

Rakesh said...

Thanks for the well-written post and I will follow your updates regularly.
Machine Learning course in Chennai
Machine Learning Training in Chennai
RPA Training in Chennai
RPA course in Chennai
Blue Prism Training in Chennai
Blue Prism Training Chennai
Automation Anywhere Training in Chennai
UiPath Training in Chennai
Machine Learning Institute in Chennai

Shanejo said...

It's very useful blog post with inforamtive and insightful content and i had good experience with this information. We, at the CRS info solutions ,help candidates in acquiring certificates, master interview questions, and prepare brilliant resumes.Find top Salesforce admin interview questions in 2020.
These Salesforce developer interview questions are highly helpful in 2020. You can read these Salesforce lightning interview questions and Salesforce integration interview questions which are prepared by industry experts.

thomas said...

MAJOR168 is open for football betting today. There are many big camps together BTi SBOBET IBCBET CMD365 if you are looking for a football betting website. Do not miss this site, there is football, there are all sports in the world. คาสิโนออนไลน์. Betting is available 24 hours a day with the best odds per pair in Thailand. Guaranteed automatic deposit and withdrawal system 10 seconds.

Live sports betting Online football betting Good price with every football match open for today online football betting SAGAME88 There are many big camps together, SBOBET IBCBET BTi CMD365, the only website complete in online football betting คาสิโนออนไลน์. There is every sport on the planet in here. With the automatic deposit and withdrawal system for 10 seconds, we have a live football system to watch every night.


We offer a wide variety of services. Called him the only player to finish with everything else does not have to go to the web preview ufabet as online. Online casinos Baccarat online Online betting games, Slot online, and with new technology, you can play ufabet via mobile phone today. Mobile Baccarat, play online via the website


Ufabet1688 of us again the way we are websites directly, not through a General Services, where customers will know it absolutely was extremely really no cheating possible on-site gambling online, it is ufabet1688 of us will hit prices.

Maradona Jons said...

Along with it also has a live casino 24 hours a day. สมัคร ufa Bet online gambling website is available to play with all casinos, baccarat, slots, roulette, dice and many other card games. We have gathered it here. With many promotions Apply for UEFA Bet now, we are happy to serve you all. With a professional team ready to solve problems And serve all of you There is a team to support the service. And answer your questions 24 hours a day.

Michael L Catudal said...

สล็อตออนไลน์ (Slot Online) คือการนำเครื่องเล่นพนัน ตู้สล็อตแมชชีน ที่กล่าวมาข้างต้นนี้ นำมาทำเป็นเกมอิเล็กทรอนิกส์ที่เรียกกันว่า สล็อตออนไลน์ เนื่องด้วยยุคสมัยพัฒนาคนหันมานิยมเล่นพนันกันผ่านคอมพิวเตอร์ จะได้นำเกมสล็อตมาทำเป็นเกมพนันออนไลน์ ผ่านระบบเครื่อข่ายอินเตอร์เน็ต ซึ่งผู้เล่นสามารถเล่นแบบผ่าน โปรแกรมสล็อต ก็ได้หรือจะเป็นการเข้าเล่น Slots ผ่านหน้าเว็บไซต์ผู้ให้บริการ ซึ่งบริการเกม สล็อตออนไลน์ นั้นก็มีรูปแบบของกติกาการเล่น คล้ายกันกับการเล่นบนตู้สล็อตแมชชีน ทั้งภาพและเสียงสมจริงเร้าใจไม่แพ้กันกับไปนั่งเล่นในคาสิโนเลยทีเดียว.

Michael L Catudal said...

If you are looking for a good website, ufa, UEFA BET casino site. Which can be played as a complete solution, able to answer Both in terms of quality and performance that are quite diverse It is best to have a lot of these things.

Popular Posts