What would you like to do?

# How do you write a C program to calculate the factorial of given no?

# Write a C program for simple interest calculation?

int main() { int p,n,count; float r,si; count=1; while(count…

# C program to calculate factorial of 100 using array?

hi friend hope u got the ans: int main() { int count=1,n,fact=1; printf("Enter the value which u need for factorial"); scanf("%d",&n); while(count=0;i--){ printf("%d",fac…t[i]); } getch(); } void factorial(int n) { int i; for(i=2;i

# Write a 'c' program to calculate the frequencies of different alphabets present in a given string the string of alphabets is to be taken as input from the keyboard?

/* For a short string, like "abaz" a Hashmap like (a:2, b:1, z:1) will be shorter, than a whole alphabet*/ #include #include main() { int count,i,j; char str[50]; printf("E…nter string : "); gets(str); for(i=0;i

# Write a c-program calculate lucky number?

include <stdio.h> main() { int a,b; clrscr(); printf("enter a,b values"); scanf("%d%d",&a,&b); a=b%10; printf("\\nlucky number %d",a); getch(); }

# Write a program in c to calculate age?

//program to find the age of given date of birth //program to find the age of given date of birth #include #include int main(void) { struct date d; int bd,bm,b…y,day,m,y,cd,cm,cy; char ch; clrscr(); do { getdate(&d); printf("\\t**********WELCOME TO AGE FINDER PLACE************\\n"); printf("\\t*************************************************\\n\\n"); cd=d.da_day; cm=d.da_mon; cy=d.da_year; printf("Enter the birthe date :(DD MM YYYY "); scanf("%d%d%d",&bd,&bm,&by); { if(cd

# Program for calculating the factorial of a number using recursion in c?

Quite a popular question, it is the 7th time I answer it;) int fact (int n) { if (n<=1) return 1; else return n*fact(n-1); }

# Write a program for calculating the area of circle in C programming?

#include #include #include using std::cout; using std::cin; using std::endl; using std::setw(); int main() { const double PI = 3.14153; double radius… = 0.0; cout > radius; unsigned short precision = 5; cout

# C program to calculate first symbol of given grammar?

include"stdio.h" #include #define max 10 #define MAX 15 char array[max][MAX],temp[max][MAX]; int c,n,t;void fun(int,int[]); int fun2(int i,int j,int p[],int key) … { int k; if(!key) { for(k=0;k='A'&&array[i][j]='A'&&array[p[0]][p[1]]

# 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 print factorial?

/*program for finding the factorial*/ void main() { int a,b=1; clrscr(); printf("Please enter any number to find its factorial\\n"); scanf("%d",&a); while(a>1) … { b=b*a; a--; } printf("factorial is %d",b); getch(); }

# How do you write a program to compute and print the factorial of given number using a while loop in c plus plus?

The problem with factorials is that small numbers have huge factorials. Even a 64-bit unsigned int is only capable of storing 20 factorial (denoted 20!). Therefore any fac…torial function that returns a 64-bit unsigned int must assert for values greater than 20. UINT64 factorial (UINT64 num) { assert (num

# 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

In Technology

# Write a c program to calculate employee payslip?

calculate pay of an employee.

Answered

In Technology

# How do you write a program on income tax calculator in C language?

include #include void tax caluclation() { char name[20]; double sal,tax; clrscr(); printf("enter the name of the person:\\n"); scanf("%s",name); printf("ente…r the name of the person:\\n"); scanf("%lf",sal); if(sal

Answered

# How do you write a program to find the factorial of a given number using the recursion or non-recursion?

The following program will find the factorial of any number using both recursion and non-recursion, using different precisions typically available, in this case to the Microso…ft Visual Studio platform. It includes an arbitrary precision decimal package that will calculate large factorials, showing the comparison of results between the different methods. Note that stack utilization is high for large numbers, so you may need to tell your linker/binder to increase the run-time stack. // Factorial.cpp : Defines the entry point for the console application. // #ifndef _WIN32_WINNT /* Microsoft stuff - possibly not needed */ #define _WIN32_WINNT 0x0600 /* Microsoft stuff - possibly not needed */ #endif /* Microsoft stuff - possibly not needed */ #include #include #include /* possibly not needed */ /* 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 lucky number?

int main (void) { puts ("Your lucky number is 12"); return 0;}