# How do you write a program that calculate factorial?

include #include void main() { clrscr(); int i=1,fact=1,n; printf("enter a no."); scanf("%d"n); while(i…

# How to write a C program to find factorial of given integer using recursive function?

// Note: You may need a larger data type, factorials become very big very quickly and may cause an overflow long factorial(int x) { if(x == 1) return 1; return((long)… factorial(x-1) * x); }

# What is a program in c to calculate factorial of number?

First of all we will define what factorial is and how to it is calculated. Factional is non negative integer. Notation would be n! It is calculated by multiplying all intege…rs from 1 to n; For example: 5! = 1 x 2 x 3 x 4 x 5 = 120. Note: 0! = 1 Small C program that illustrates how factorial might be counted: #include int factorial(int num); int main() { int num; printf("Enter number: "); scanf("%d", &num); printf("Factorial: %d\\n", factorial(num)); return 0; } int factorial(int num) { if (num == 0) { return 1; } return num * factorial(num - 1); } Testing: Enter number: 5 Factorial: 120 Enter number: 0 Factorial: 1

# How do you write a C program that calculates parking charges for a given day?

#include minfee = 2; int main() { int car1,car2,car3,hour,hour2,hour3,charge1,charge2,charge3, totalhour,totalcharge,minfee; … printf("Enter car number\\n"); scanf("%d", &car1); printf("How many hour did the car park\\n"); scanf("%d", &hour); if (hour>=3) { charge1 = (minfee+hour*0.50); } else { charge1 = 2.00; } printf("Enter car number\\n"); scanf("%d", &car2); printf("How many hour did the car park\\n"); scanf("%d", &hour2); if (hour>=3) { charge2 = (minfee+hour*.50); } else { charge2 = 2.00; } printf("Enter car number\\n"); scanf("%d", &car3); printf("How many hour did the car park\\n"); scanf("%d", &hour3); if (hour>=3) { charge3 = (minfee+hour*0.50); } else { charge3 = 2.00; } totalhour=(hour+hour2+hour3); printf("%d", totalhour); totalcharge=(charge1+charge2+charge3); printf("%d", totalcharge); return 0; }

# Write a C program to calculate factorial of given positive integer no?

include long double fact (long double n) { return (n == 1) ? 1 : n * fact(n-1); } void main() { int n; printf("Enter a number:\\n>"); scanf("%d",&n); printf("%…d! = %Lf\\n", fact(n)); //long double can hold much bigger numbers than say, int or double. }

# Write a c program to print factorial of given number and using do while loop?

include #include void main() { int n,i,fact=1; clrscr(); printf("enter the value of n:"); Scanf("%d",&n); for(i=1;i…

# How can you write a program in c language to calculate factorial of 100 or above that?

The largest factorial in a 32 bit unsigned integer is 12!, which is 479,001,600. The largest factorical in a 64 bit unsigned integer is 20!, which is 2,432,902,008,176,6…40,000. Anything larger will overflow, and give wrong results. To go larger than that, you need to perform arithmetic digit by digit, or group by group, multiplying and carrying. Its the same as doing it on a piece of paper. You could create an array of digits and provide routines to multiply and carry. You could implement this as a linked list, making the length not preset. The elements do not have to be just digits - they could be larger, say 16 bit integers - so long as the largest possible operation would not result in an overflow.

# Write a C program to find the factorial of a given number?

This program will compute N Factorial for any N, and does not suffer from overflow, because it uses a bin technique and handles each digit individually. For large values of N,… you may need to increase your run-time stack size. /* Portable arbitrary length decimal iterative */ /* one node of a linked list of digits, the first node being low-order */ struct _decimal { int digit; struct _decimal *next; }; typedef struct _decimal decimal; /* Portable arbitrary length decimal iterative */ /* Initialize the list - necessary on second pass, if main recoded */ void decimal_initialize (decimal *d, int n) { decimal *next, *nextsave; d->digit = n; nextsave = d->next; d->next = NULL; next = nextsave; while (next != NULL) { nextsave = next->next; free (next); next = nextsave; } return; } /* Portable arbitrary length decimal iterative */ /* Append a digit at the high order position */ void decimal_add_digit (decimal *d, int n) { decimal *new_digit = (decimal*) malloc (sizeof (decimal)); while (d->next != NULL) d = d->next; new_digit->digit = n; new_digit->next = NULL; d->next = new_digit; return; } /* Portable arbitrary length decimal iterative */ /* Print the digits in reverse order - recursive */ void decimal_print_digits (decimal *d, int last_digit) { if (d->next != NULL) decimal_print_digits (d->next, false); printf ("%d", d->digit); if (last_digit) printf("\\n"); return; } /* Portable arbitrary length decimal iterative */ /* multiply the list by N */ void decimal_multiply (decimal *d, int N) { int carry = 0; while (d != NULL) { d->digit = d->digit * N + carry; carry = d->digit / 10; d->digit %= 10; if (carry != 0 && d->next NULL) decimal_add_digit (d, 0); d = d->next; } return; } /* Portable arbitrary length decimal iterative */ /* Primary interative algorithm */ void decimal_NFactIterative (decimal *d, int N) { if (N < 2) { decimal_initialize (d, 1); return; } if (N 2) { decimal_initialize (d, 2); return; } while (N > 2) { decimal_multiply (d, N); N--; } return; } /* Example main line */ /* Generates all variations to show differences in results */ int main (int argc, char *argv[]) { int N; decimal Decimal = {2, NULL}; if (argc < 2) { printf ("Enter N (or use command line) : "); scanf ("%d", &N); } else { N = atoi (argv[1]); } printf ("Arbitrary: %u! = ", N); decimal_NFactIterative (&Decimal, N); decimal_print_digits (&Decimal, true); return 0; }

# Write a C program to calculate a factorial?

include #include void main() { int a,b,c=1; clrscr(); { printf("enter the number b="); scanf("%d",&b); } for(a=b;a>…=1;a--) c=c*a; { printf("the factorial of the num is%d",c); } getch(); }

# How do you write a c program to calculate factorial using recursion?

unsigned long nfact(int n) if (n2) return n else return n*nfact(n-1); For 32-bit integers, this program will fail at N13, due to overflow. For 64-bit integers, it will fail at… N==21. A solution for this is an arbitrary decimal arithmetic library, perhaps based on linked lists.

# How do you write a C program to calculate the factorial within recursive function?

Note: This fails at N=20 due to truncation error #include #define FALSE (0) #define TRUE (1) } /* Microsoft 64-bit recursive */ unsigned long long NFactLongLongRecursive (…unsigned long long N) { if (N < 2) return 1; if (N == 2) return 2; return N * NFactLongLongRecursive (N - 1); /* Example main line */ int main (int argc, char *argv[]) { int N; if (argc < 2) { fprintf (stderr, "Usage: factorial N\\n"); return 1; } N = atoi (argv[1]); printf ("Recursive: %u! = %I64u\\n", N, NFactLongLongRecursive (N)); return 0; }

# How do you write a program that calculate factorial in javascript?

function factorial(n) { var x=1; while(n>1) x*=(n--); return x; }

Answered

# Write a C program to calculate the factorial of a given number using functions?

include int fact(int x); int main() { int x, total; printf("Enter a number: "); scanf("%d", &x); total = fact(x); printf("The factorial of %d is %d", x, total…); char wait; scanf("%s", wait); return 0; } int fact(int x) { if(x == 1) { return 1; } return fact(x - 1) * x; }

Answered

In Technology

# Write a program to calculate the factorial of a given number?

/*this is the program to find the factorial to n numbers*/ #include void main() { int n,f,factorial(int); printf("which number facrorial you want?"); scanf("%d",&n…); f=factorial(n); printf("The factorial upto %d is %d",n,f); } int factorial(int a) { if(a==0) return 1; else return (a)*factorial(a-1); }

Answered

# How do you write a C program to find factorial of given integer using function?

There are several methods, some recursive, and iterative. Most suffer from lack of precision, because factorials get large fast. Here is a routine that compares various method…s, including a method of arbitrary length decimals using linked lists. Note that, in order to compute vary large factorials, you will need to tell your linker/binder to increase the run-time stack size, because the linked list is created on the stack, and not in the heap. // Factorial.cpp : Defines the entry point for the console application. // #include #include /* Microsoft 32-bit iterative */ unsigned long NFactLongIterative (unsigned long N) { unsigned long result = N; if (N < 2) return 1; if (N 2) return 2; while (--N >= 2) result *= N; return result; } /* Microsoft 64-bit iterative */ unsigned long long NFactLongLongIterative (unsigned long long N) { unsigned long long result = N; if (N < 2) return 1; if (N 2) return 2; while (--N >= 2) result *= N; return result; } /* Microsoft 64-bit recursive */ unsigned long long NFactLongLongRecursive (unsigned long long N) { if (N < 2) return 1; if (N 2) return 2; return N * NFactLongLongRecursive (N - 1); } /* Portable double recursive */ double NFactDouble (double N) { if (N < 2) return 1; if (N 2) return 2; return N * NFactDouble (N - 1); } /* Portable arbitrary length decimal iterative */ /* one node of a linked list of digits, the first node being low-order */ struct _decimal { int digit; struct _decimal *next; }; typedef struct _decimal decimal; /* Portable arbitrary length decimal iterative */ /* Initialize the list - necessary on second pass, if main recoded */ void decimal_initialize (decimal *d, int n) { decimal *next, *nextsave; d->digit = n; nextsave = d->next; d->next = NULL; next = nextsave; while (next != NULL) { nextsave = next->next; free (next); next = nextsave; } return; } /* Portable arbitrary length decimal iterative */ /* Append a digit at the high order position */ void decimal_add_digit (decimal *d, int n) { decimal *new_digit = (decimal*) malloc (sizeof (decimal)); while (d->next != NULL) d = d->next; new_digit->digit = n; new_digit->next = NULL; d->next = new_digit; return; } /* Portable arbitrary length decimal iterative */ /* Print the digits in reverse order - recursive */ void decimal_print_digits (decimal *d, int last_digit) { if (d->next != NULL) decimal_print_digits (d->next, false); printf ("%d", d->digit); if (last_digit) printf("\\n"); return; } /* Portable arbitrary length decimal iterative */ /* multiply the list by N */ void decimal_multiply (decimal *d, int N) { int carry = 0; while (d != NULL) { d->digit = d->digit * N + carry; carry = d->digit / 10; d->digit %= 10; if (carry != 0 && d->next NULL) decimal_add_digit (d, 0); d = d->next; } return; } /* Portable arbitrary length decimal iterative */ /* Primary interative algorithm */ void decimal_NFactIterative (decimal *d, int N) { if (N < 2) { decimal_initialize (d, 1); return; } if (N 2) { decimal_initialize (d, 2); return; } while (N > 2) { decimal_multiply (d, N); N--; } return; } /* Example main line */ /* Generates all variations to show differences in results */ int main (int argc, char *argv[]) { int N; decimal Decimal = {2, NULL}; if (argc < 2) { printf ("Enter N (or use command line) : "); scanf_s ("%d", &N); } else { N = atoi (argv[1]); } printf ("Long: %u! = %u\\n", N, NFactLongIterative (N)); printf ("LongLong: %u! = %I64u\\n", N, NFactLongLongIterative (N)); printf ("Recursive: %u! = %I64u\\n", N, NFactLongLongRecursive (N)); printf ("Double: %u! = %.0f\\n", N, NFactDouble (N)); /* note: arbitrary is exact - if the others don't match, arithmetic overflow occurred */ printf ("Arbitrary: %u! = ", N); decimal_NFactIterative (&Decimal, N); decimal_print_digits (&Decimal, true); return 0; }

Answered

In Technology

# Write a program in c plus plus to compute the factorial of a given number?

The factorial of a real number, n, is the product of all positive real numbers less than or equal to n, denoted as n!. Thus, 5! = 5*4*3*2*1 = 120. Computing factorials is a n…aturally recursive operation. That is, for all values, n, where n>1, factorial(n) = factorial(n-1) * n. However, it is more efficient to implement the function iteratively because of the need to cater for the convention that 0! evaluates to 1 (thus 0! and 1! both evaluate to 1). unsigned factorial(unsigned n) { if (n==0) return 1; unsigned f = n--; while (n>1) f *= n--; return f; } However, the real problem with factorials is that the size of the result increases exponentially to the size of the value. Consider the following for all factorials from 0 to 12: 0! = 1 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 7! = 5,040 8! = 40,320 9! = 362,880 10! = 3,628,800 11! = 39,916,800 12! = 479,001,600 A 32-bit variable can store a maximum positive value of 232-1, which is 4,294,967,295. But 13! is 6,227,020,800. Therefore a 32-bit factorial function would need to be constrained to positive values less than or equal to 12. A 64-bit variable increases the maximum range up to to 18,446,744,073,709,551,615. But 21! is a whopping 51,090,942,171,709,440,000. Thus even a 64-bit factorial function would be limited to values less than or equal to 20. You can, of course, just keep adding bits to cater for larger and larger values, but the largest integral that C++ supports natively is 64-bits. There's no native support for a 128-bit or larger integrals. Floating point values will give a little more headroom through approximation, but there's still the high risk of overflowing even with relatively small values. Calculators that use 2-digit exponents are limited to just 69! because 69! < 10100 < 70!. When you consider that even the humble Windows 8 calculator can handle factorials up to 3248! before overflowing, even on a 32-bit system, the limitation seems not so much the computer but rather the programming language. However, the problem is not unique to C++. You will encounter the same or similar problem no matter which language you use. Therefore the only way to cater for factorials larger than 20! is to create your own dynamic datatype that can simply expand as required. You could use a fixed-length data type, such as 128-bit, but there's always the risk of overflowing. A dynamic data type can simply grow and shrink as required, and when it's at or below 64-bit, you can simply treat it just as you would any other integral. A good minimum size to start with is 32-bit or 64-bit, depending on the underlying architecture. There's not much point shrinking below that since the datatype is primarily intended to handle extremely large numbers that cannot be handled natively. Your datatype must also cater for all arithmetic operators so that you can use it just as you would an integral. The only problem is that you cannot assign integral values larger than 18,446,744,073,709,551,615. Therefore you must use strings to both assign and access the values within your datatype, which means you must convert those strings to and from your variable-length binary value. You could just store them as strings of course, but this consumes an 8-bit byte per digit. You could use 4-bit nybbles to double the capacity (3-bits would only cater for up to 8 unique symbols but the digits 0 to 9 require 10 symbols so you need at least 4-bits to represent them all). However, both techniques require that you perform decimal arithmetic upon the data which, although relatively simple for humans, is inefficient in a binary computer. The computer already knows how to perform binary arithmetic on integrals, so the most efficient method would be to convert your decimal strings to and from binary, and implement your own bit-adders. Bearing in mind that multiplication is just a series of additions (2*3 can be worked out as 2+2+2 but is more efficiently worked out as 3+3) you've already got enough to be able to calculate factorials. Take it a bit further and include a bit-subtractor and you can also cater for both integer division as well as modulo (integer remainders). A search of the internet will find many examples that can help you get started with this. Many can be found by searching for the terms "BIG_INT" or "HUGE_INT", but the majority make use of decimal arithmetic which is, as mentioned above, extremely inefficient and slow. However, decimal arithmetic is very easy to implement because it's something everyone is familiar with. Once you've played around with these examples, you simply convert that knowledge to binary and do exactly the same thing -- but a lot more efficiently. It won't be anywhere near as fast as integral arithmetic, but it'll be faster than decimal arithmetic by an order of magnitude because you only need to convert to decimal to show the result. Internally, you are operating at the bit-level and can therefore manipulate more bits (and therefore more decimal digits) in a single operation. With decimal arithmetic, you can only manipulate one digit at a time, converting to and from binary to perform the actual arithmetic.

Answered

In Technology

# How do you write a c program to calculate the SUM and COUNT of Primes from a given range of numbers?

To get complete tutorials of "c programming" Reference:programming-bd.com/c_page2.aspx# prime number