This question already has answers here:
Factorial using Addition
(4 answers)
Closed 3 years ago.
I'm trying to figure out a C program that calculates a factorial using nested while loops with no multiplication. Is there an easy way to do this with as little variables as possible?
I have an inner loop that does multiplication using addition, but I can't seem to find an outer loop that would then find the factorial.
#include <stdio.h>
#include <stdlib.h>
int main() {
unsigned int a = 4;
unsigned int b = 4;
unsigned int c = 0;
int i = 0;
while(i < b) {
c += a;
i++;
}
printf("Result: %u", c);
return 0;
}
This is one solution for that :
#include <stdio.h>
int mult (int n1, int n2);
int main () {
int number,result,count;
scanf("%d",&number);
result=number;
for (count=number-1;count>1;count--)
result=mult(result,count);
printf ("factorial is %d",result);
return 0;
}
int mult (int n1, int n2) {
int i,answer=0;
for (i=1;i<=n2;i++)
answer+=n1;
return answer;
}
I used my own variable names but that shouldn't be a problem.
Related
This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 4 months ago.
here's the code it is supposed to display the value of my function (1/1+(25xx)) in a [-1,1] interval. But when I run it I have 1 as a result!!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int i,n;
double y=0;
double t=0;
double delta=0;
scanf("%d",&n);
delta=1/n;
for (i=0;i<n;i++)
{
t=t+delta;
y= 1 / (1 + (25*t*t));
printf("%lf \n",y);
}
return 0;}
Your program is working with integer math. Be sure to use double literals and to cast integers to doubles where needed.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
int i, n;
double y = 0, t = 0, delta;
scanf("%d",&n);
delta = 1 / (double)n;
for (i = 0; i < n; i++) {
t = t + delta;
y = 1.0 / (1.0 + (25.0 * t * t));
printf("%lf \n", y);
}
return 0;
}
This question already has answers here:
What is the effect of trailing white space in a scanf() format string?
(4 answers)
Closed 8 months ago.
#include <stdio.h>
#include <stdlib.h>
void add(int *arr, int n);
int main() {
int arr[] = {};
add(arr, 4);
return 0;
}
void add(int *arr, int n){
for (int i = 0; i < n; i++){
printf("%d index is : ", i);
scanf("%d\n", &arr[i+1]);
}
}
the for loop doesn't work after the i == 1... the execution stops and then I have to press some alphabet and executes the whole for loop with no values...
This declaration in C and in C++
int arr[] = {};
is invalid. The initializer list shall not be empty.
Instead you could write in C
int arr[4] = { 0 };
or
int arr[] = { [3] = 0 };
And within the function instead of
scanf("%d\n", &arr[i+1]);
^^^
you have to write
scanf("%d", &arr[i]);
^^^
Otherwise there will be an access to memory outside the array.
If this has any error, kindly mention it. Because I may figure out some future consequences
#include<stdio.h>
int main()
{
int a[]={1,2,3,4,5,5,4,3,4,5},count[10]={0},i;
for(i=0;i<sizeof(a)/sizeof(a[0]);i++)
{
int x;
x=a[i];
count[x]=count[x]+1;
}
for(i=0;i<sizeof(count)/sizeof(count[0]);i++)
{
if(count[i]!=0)
{
printf("\n %d:%d",i,count[i]);
}
}
}
Generally? No. In this specific case? Maybe.
Instead of sizeof(a)/sizeof(a[0]), use a macro to get the array size.
Dont declare/initialize a loop variable and two arrays in one line.
You will get out of bounds issues as soon as a contains a number that is bigger than the length of count - 1 or smaller than 0.
I would do something like the following:
#include <stdio.h>
#include <string.h>
#define ARRAY_COUNT(array) (sizeof(array)/sizeof(array[0]))
void GetCountOfNumberInArray(int intArray[],
unsigned int intArraySize,
int numbersToCount[],
unsigned int numberCount[],
unsigned int numbersToCountSize){
memset(numberCount, 0, numbersToCountSize * sizeof(numbersToCountSize));
unsigned int count = 0;
for(unsigned int i = 0; i < intArraySize; i++){
for(int j = 0; j < numbersToCountSize; j++){
if(numbersToCount[j] == intArray[i]){
numberCount[j]++;
break;
}
}
}
}
int main(int argc, char *argv[]){
int intArray[] = {1,2,3,4,5,5,4,3,2,1};
int numbersToCount[] = {1,2,3,4,5,6,7,8,9,10};
unsigned int numberCount[ARRAY_COUNT(numbersToCount)];
GetCountOfNumberInArray(intArray,
ARRAY_COUNT(intArray),
numbersToCount,
numberCount,
ARRAY_COUNT(numbersToCount));
for(unsigned int i = 0; i < ARRAY_COUNT(numbersToCount); i++){
printf("number %i appears %u times in the array\n", numbersToCount[i], numberCount[i]);
}
}
That way you get a universal function to count a set of numbers in an array that still works similar to your original solution.
I created a factorial function and my output for integer b is incorrect when it is set at 5, any ideas as to why? b should be equal to the integer 120 and I am getting the number -95449088 after I compile and run it.
#include <stdio.h>
#include <stdlib.h>
int factorial(int x)
{
int i;
for(i=1; i < x; i++)
x *= i;
return x;
}
int main() {
int a=5, b;
b = factorial(a);
printf("%d\n", b);
}
Here you are using the same x value in the condition check of for-loop which is a mess up. Instead you can store the variable x to a variable temp and use this temp variable for checking the condition.
Please see below the corrected code
#include <stdio.h>
#include <stdlib.h>
int factorial(int x)
{
int i,temp=0;
temp = x; //store the x to temp
for(i=1; i < temp; i++){
x *= i;
}
return x;
}
int main() {
int a=5, b;
b = factorial(a);
printf("%d\n", b);
}
Other have explained that you should avoid mixing input and output variables. This is good advice, and as a beginner you should try to observe it.
But this is a special case, and here you can re-use the input value, provided you use a decreasing loop:
int factorial(int x)
{
int i;
for (i = x-1; i >1; i--)
x *= i;
return x;
}
It works because it implicitly initializes the return value with x and then multiplies it by all numbers below it, which is a possible definition for the factorial.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
#include <stdio.h>
int main() {
int i = 1;
int j = -1;
while(i)
i++;
while(-j)
j--;
i = i-1;
j = j+1;
printf("%d %d\n",i,j);
}
I want to know the range of int! this code can not get the right answer! But it can!
#include <stdio.h>
int main() {
int i = 1;
int j = -1;
while(i > 0)
i++;
while(j < 0)
j--;
i = i-1;
j = j+1;
printf("%d %d\n",i,j);
}
I don't know what are the differences between them!
Both of these code samples rely on undefined behavior to determine the maximum value of an int. Overflow is not defined for a signed number like int.
To find the maximum value of an int, you simply read the value INT_MAX.
If you truly want to calculate the value of INT_MAX in a portable way, then you should look into this answer: Is there a portable way to define INT_MAX?
Try limits.h from the standard C library. It contains the two constant definitions INT_MIN and INT_MAX, telling you the minimum and maximum values of an int respectively.
#include <limits.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
printf("int min: %d, int max: %d\n", INT_MIN, INT_MAX);
return 0;
}
You want to know the range of int?
#include <stdio.h>
#include <limits.h>
int main()
{
printf("%d, %d\n", INT_MIN, INT_MAX);
return 0;
}
Program output
-2147483648, 2147483647
And following the answer from #iharob, here is a similar solution that reaches the limit without UB.
#include <stdio.h>
int main()
{
int integer = -1, max = 0;
while (integer <<= 1)
max = (max << 1) | 1;
printf("%d\n", max);
return 0;
}
Lastly, a niftier solution (no loops) would be
printf("Max int %d\n", (unsigned)-1 >> 1);
If you actually want to compute the value, you can use this
int
main(int arcg, char **argv)
{
int integer;
int i;
integer = 0;
for (i = 0 ; i < 8 * sizeof (integer) - 1 ; i++)
{
integer |= (1 << i);
}
return 0;
}
and then the maximum will be integer and the minimum will be -integer