this is the java code to enjoy
convert it to C or take the idea and go on.
why ?! because the syntax is the same !!
file name : Class1.java
------------------------
package mypackage1;
public class Class1
{
public long fact(int a)
{
int answer = a;
do
{
if (a==0)
{
return a;
}
answer = a*(--a);
} while(a<=0);
return answer;
}
public static void main(String [] arg)
{
Class1 c1 = new Class1();
System.out.println(c1.fact(4));
}
-------------------
note: the code will work will all java versions
enjoy the coding
//program to find the factorial value f any number using while loop #include<stdio.h> void main() { int i,n,fact=1; printf("Enter the number\n"); scanf("%d",&n); i=n; while (i>=1) { fact=fact*i; i--; } printf("The factorial value=%d",fact); } the above is a program for calculating tha factorial value of any number which is entered by the user
Actually, a for loop is more appropriate in this case. With while, it would be something like the following pseudocode - adapt to your favorite programming language:function factorial(n)result = 1factor = 1while factor
#include <iostream> using namespace std; int main() { int i, number=0, factorial=1; // User input must be an integer number between 1 and 10 while(number<1 number>10) { cout << "Enter integer number (1-10) = "; cin >> number; } // Calculate the factorial with a FOR loop for(i=1; i<=number; i++) { factorial = factorial*i; } // Output result cout << "Factorial = " << factorial << endl;
int factorial(int n) { int i; int f=1; for(i=2;i<=n;++i) f*=i; return f; }
by this program you can find the factorial: #include<iostream> using namespace std; main() { int n,x,f=1; cin>> n; x=0; while(x<n) { x++; f= f*x; } cout<<"factorial is"<<f<<"\n"; system("pause"); return 0; }
// returns n! int fact(final int n) { // keep track of factorial calculation in f // f starts at n, and we will multiply it by all integers less than n int f = n; // loop from n-1 down to 2 for(int i = (n - 1); i > 1; --i) { // increase our total product f *= i; } return f; }
#include using std::cout;using std::endl;int main(){int number(1);cout number;//using loop forint sum(0);for(int i(1); i
An example in Java, to compute 10!: int factorial = 1; for(int i = 1; i < 11; i++) { factorial *= i; }
#include<stdio.h> #include<conio.h> void main() { int i,n,fact=1; clrscr(); printf("enter the number"); scanf("%d",&n); for(i=1;i<=n;i++) printf("%d",n); fact=fact*i; { printf("the factorial is=%d",fact); } getch(); } By:-Abhishek Goyal(goyal.abhi40@yahoo.com)
#!/usr/bin/perl print factorial($ARGV[11]); sub factorial { my($num) = @_; if($num == 1) { return 1; # stop at 1, factorial doesn't multiply times zero } else { return $num * factorial($num - 1); # call factorial function recursively } }
#include #include void main(){int n,i;long int fact=1;clrscr();printf("Enter number = ");scanf("%d",&n);for(i=1;i
A Loop is a programming language construct that instructs the processor to repeat a sequence of operations a number of times until a specific condition is reached. There are different types of loops. They are: * for loop * while loop * do while loop