Factorial Of A Number Recursion Function

Write a program to find factorial of the given number.
Recursion: A function is called'recursive 'if a statement within the body of a function calls the same function. It
is also called'circular definition '. Recursion is thus a process of defining something in terms of itself.
Program: To calculate the factorial value using recursion.
#include

int fact(int n);
int main()
{
int x, i;

printf("En ter a value for x: \n");
scanf("%d" ,&x);

i = fact(x);

printf("\n Factorial of %d is %d", x, i);
return 0;
}
int fact(int n)
{
/* n=0 indicates a terminatin g condition */
if (n
return (1);
}
else
{
/* function calling itself */
return (n * fact(n - 1));
/*n*fact(n -1) is a recursive expression */
}
}
Output:
Enter a value for x:
4
Factorial of 4 is 24
Explanatio n:
fact(n) = n * fact(n-1)
If n=4
fact(4) = 4 * fact(3) there is a call to fact(3)
fact(3) = 3 * fact(2)
fact(2) = 2 * fact(1)
fact(1) = 1 * fact(0)
fact(0) = 1
fact(1) = 1 * 1 = 1
fact(2) = 2 * 1 = 2
fact(3) = 3 * 2 = 6
Thus fact(4) = 4 * 6 = 24
Terminatin g condition( n
infinite loop.

0 comments:

Simple Slider using JQuery Animation

Step 1 : Create a html file and add the following script tag in head section
             <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript">

Step 2 : add the following code to display the images
           
             <div id="wrapper">
<div id="slide">
<img src="7.JPG" width="200px" height="200px" />
<img src="8.JPG" width="200px" height="200px" />
<img src="9.JPG" width="200px" height="200px" />
<img src="11.JPG" width="200px" height="200px" />
<img src="12.JPG" width="200px" height="200px" />
</div>
   </div>

Step 3 : add the following code for next and previous button
            <div id="buttons">
<a href="javascript:void(0)" id="prev"><span style="text-decoration:none;"> < </span></a>
<a href="javascript:void(0)" id="next"><span style="text-decoration:none;"> > </span></a>
   </div>

Step 4: add the following css code
            *{
margin: 0 auto;
padding:0;
        }

#wrapper{
width:600px;
overflow:hidden;

}
#slide{
width:900px;
overflow:hidden;
height:210px;
position:relative;
}

#buttons{
position: relative;
width: 600px;
}


Step 5: add the following jquery to animate slide the images
            $(document).ready(function(){
               var timages = 5; <!-- timages = total number of images -->
var simages = 3; <1-- simages = number of images to show -->
var limages = timages-simages; <!-- limages = number of images remaining i.e. 5-3=2 -->
var cimages = 0; <!-- cimages = current images -->
$("#prev").click(function(){
cimages --;
if (cimages < 0)
{
cimages = 0
}
var posx = cimages* 200;
//showInputText();
$("#slide").animate({left:"-"+posx+"px"}, 500 )
});

$("#next").click(function(){
cimages ++;
if (cimages > limages)
{
cimages = limages
}
var posx = cimages* 200;
//showInputText();
$("#slide").animate({left:"-"+posx+"px"}, 500 )
})
 })



Your final code should look like this

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>

<title>Untitled Document</title>
<style>
*{
margin: 0 auto;
padding:0;
}

#wrapper{
width:600px;
overflow:hidden;

}
#slide{
width:900px;
overflow:hidden;
height:210px;
position:relative;
}

#buttons{
position: relative;
width: 600px;
}
</style>
<script>
$(document).ready(function(){
var timages = 5;
var simages = 3;
var limages = timages-simages;
var cimages = 0;
$("#prev").click(function(){
cimages --;
if (cimages < 0)
{
cimages = 0
}
var posx = cimages* 200;
//showInputText();
$("#slide").animate({left:"-"+posx+"px"}, 500 )
});

$("#next").click(function(){
cimages ++;
if (cimages > limages)
{
cimages = limages
}
var posx = cimages* 200;
//showInputText();
$("#slide").animate({left:"-"+posx+"px"}, 500 )
})
})

</script>
</head>

<body>
<div id="wrapper">
<div id="slide">
<img src="7.JPG" width="200px" height="200px" />
<img src="8.JPG" width="200px" height="200px" />
<img src="9.JPG" width="200px" height="200px" />
<img src="11.JPG" width="200px" height="200px" />
<img src="12.JPG" width="200px" height="200px" />
</div>
</div>
<div id="buttons">
<a href="javascript:void(0)" id="prev"><span style="text-decoration:none;"> < </span></a>
<a href="javascript:void(0)" id="next"><span style="text-decoration:none;"> > </span></a>
</div>
</body>
</html>


0 comments: