Increment in denominator in C [duplicate] - c

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 4 years ago.
I'm trying to make and print a series of the form 1/n; where n is a natural number.
int Number;
float NumberInverse, NumberInverseNext;
for ( Number = 1; Number < 1000; Number++)
if I try to print 'Number' after this, I get a series of natural numbers from 1-1000, as I'd expect. But if I do
NumberInverse = 1/Number;
and try to print NumberInverse, I get 0 as output. I'm not sure what I'm doing wrong and what I should be doing.
EDIT : This is not a duplicate of the question mentioned as even after changing
NumberInverse = 1/Number;
to
NumberInverse = 1/(float)Number;
I can't get the series of 1/n which was the original question

try casting, the answer in your case is an integer so you will get 0, try this:
int Number;
float NumberInverse, NumberInverseNext;
for ( Number = 1; Number < 1000; Number++) {
NumberInverse = 1/(double)Number;
printf("%f \n",NumberInverse);
}

Related

What causes a code to just randomly subtract or add small fractals to floating-point numbers? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 3 years ago.
Made a code that calculated the amount of negative numbers in an array and shows those numbers. The problem is when the program prints out the number, it changes the number a slight bit, adding or subtracting something like 0.003.
I have absolutely no clue as to what's wrong with it, tried asking my professor; she said she didn't know, so I am here.
float col[10];
...
for (int i = 0; i < 10; ++i)
{
scanf_s("%f", &col[i]);
}
...
printf("\n");
printf("There are %d negative numbers\n", ct);
for (int i = 0; i < 10; ++i)
{
if (col[i] < 0)
{
printf("[%d]=%f ", i, col[i]);
}
}
...
Put in -7786.88, command line printed out -7786.888184. It's fine on integers, just prints out a bunch of zeroes after the dot.

Implementing RSA Encryption - pow() with large outputs [duplicate]

This question already has answers here:
Calculating pow(a,b) mod n
(14 answers)
Power for big numbers modulo m in C
(4 answers)
Closed 4 years ago.
I have given a homework about RSA Encryption, and I have wanted to complete this homework with C Programming Language. By the way, I am trying to find an answer for a problem connected with my code, not only about pow(), so it should not be marked as duplicate question.
int *Encrypt(int* translation, int size){
int chunk;
const int E_RSA = 13;
double N_RSA = 2537;
// some codes...
//formulae = c = m^e mod n
encryptedMessage = fmod(pow(chunk,E_RSA) , N_RSA);
//some codes...
}
So here, when I calculate pow(chunk,E_RSA), because my E value is 13, and let's assume that the message is 1900, there will be an operation such as 1900^13, and it will give a huge output. When I calculate it from a calculator, it give a different result from my C code. This difference is being a problem for the encryption. How can I solve it so that the pow() function will return result as precise as a scientific calculator ? Thanks for the help.

Program in C to sum each digit in an integer [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
int main(){
int x;
int sum;
printf("Enter a positive integer: ");
scanf("%d", &x);
do{
sum += (x%10);
x=(x/10);
if((x/10)==0){
sum += x;
}
}
while((x/10)!=0);
printf("%d",sum);
}
Hey, I'm trying to get this to add up each digit within the entered integer, but the code I'm using keeps returning the wrong output. Would someone please help me fix my equation/code, because I'm not sure why the output is incorrect.
in your code
int sum;
is not initialized. use something like
int sum = 0;
Note: local variables are not automatically initialized [to 0 or anything], without explicit initialization their contents will be garbage. Thereby, using sum += (x%10); will lead to read before-write scenario, producing wrong result.
Here is a small math problem:
Somebody gave you ten apples, then someone else gave you two more. How many apples do you have?
The right answer is that this question is impossible to answer, because nobody told you how many apples you had at the beginning.
Your program suffers from the same problem: you failed to initialize sum before starting to add to it, so it has initial "garbage" value.
Changing the declaration to
int sum = 0;
will fix the problem.
Initialize your variable sum -
int sum = 0;
If you don't initialize your variable in C and some other language then the a garbage/random value is assigned to it when it is used in expression for the first time.
Late Answer
Since you've already found the problem with your code, I would like to provide a more concise alternative:
int x;
int sum = 0
int digit;
printf("Enter a positive integer: ");
scanf("%d", &x);
while (x > 0) {
digit = x % 10;
sum += digit;
x /= 10;
}
printf("%d", sum);
x % 10 extracts the last digit of the number, and then x /= 10 truncates the integer by removing the last digit.

Why does this code print greater? [duplicate]

This question already has answers here:
Floating point comparison [duplicate]
(5 answers)
Closed 8 years ago.
1)#include<stdio.h>
int main()
{
float x=0.5;
if(x>0.5)
printf("\ngreater");
else
printf("\nlesser ");
return 0;
}
output->lesser
2)#include<stdio.h>
int main()
{
float x=0.1;
if(x>0.1)
printf("\ngreater ");
else
printf("\nlesser ");
return 0;
}
output->greater
Why in the first case output is "lesser" while in the second one output is "greater"? What is the difference?
EDIT: I understood that 0.1 is not equal, but then why 0.5 is showing as equal?
I almost sure it's because you are comparing float and double.
There's an answer to why it is greater answered here : 0.1 float greater than double

Program doesn't show expected output [duplicate]

This question already has answers here:
Printing array elements
(6 answers)
Closed 9 years ago.
The expected output of the following C program is to print the elements in the array. But when actually run, it doesn't do so.
#include<stdio.h>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {23,34,12,17,204,99,16};
int main()
{
int d;
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);//printing the array
return 0;
}//looks simple but no result
What's going wrong? Why am I not getting any output?
In the comparison
d <= (TOTAL_ELEMENTS-2)
TOTAL_ELEMENTS has type size_t so d is converted to unsigned. For, say, sizeof(size_t)==4, this makes the test
0xffffffff < 5
which fails, causing the loop to exit.
If you really want to start your loop counter from -1
d <= (int)(TOTAL_ELEMENTS-2)
would work

Resources