Convert decimal number to binary in C [closed] - c

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
For an assignment I need to convert a 16-bit decimal number to a binary number. So for example the number 9 should print 0000000000001001. My professor started us with this code:
void printBinary(short n)
{
}
int main(int argc, char **argv)
{
short n;
printf("Enter number: ");
scanf("%hd", &n);
printBinary(n);
}
I am very confused as to where to go from here. I really would appreciate anyone helping me understand what to do, as I am very new to coding. Thanks in advance.

Try this:
int printBinary(int integer)
{
int remainder, counter = 0, my_binary = 0;
while (integer > 0)
{
remainder = integer % 2;
my_binary += remainder * pow(10, counter);
integer /= 2;
counter++;
}
printf ("Binary is %d\n", my_integer");
}
This has been taken from here.
NOTE: I have the assumption that you want to convert only positive decimals.

Related

convert string to double , the answer is correct ,but not very accurate [closed]

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 2 years ago.
Improve this question
I want to use atof to convert my string to a double,the answer is correct ,but not very accurate
ATTENTION: because of some other reasons, fscanf is not permitted
my code is :
#include <stdio.h>
#include <stdlib.h>
#define MAXCN 50
int main(void)
{ FILE* lstm_txt = NULL;
char lstm_weight[MAXCN] = {0};
int lstm = 0;
int i = 0;
float lstm_val;
if ((lstm_txt = fopen("test1.txt", "r"))== NULL){
fprintf(stderr,"error:file open failed 'test1.txt'.\n");
return 1;
}
while ((i + 1 < MAXCN) && ((lstm = fgetc(lstm_txt)) != ' ' ) && (lstm != EOF)){
lstm_weight[i++] = lstm;
}
//lstm_weight[i] = 0;
printf("\n lstm_weight: %s\n\n", lstm_weight);
lstm_val = atof(lstm_weight);
printf("\n convert \"lstm_weight\" to lstm_val is : %f\n\n", lstm_val);
return 0;
}
my file : lstm_txt is :
4.217959344387054443e-01 -2.566376626491546631e-01 2.173236161470413208e-01 4.217959344387054443e-01
code hasn't bug, and the result is :
lstm_weight: 4.217959344387054443e-01
convert "lstm_weight" to lstm_val is : 0.421796
but I want Istm_val is 0.4217959344387054443 ,how can I do that?
you could try something like sprintf()
here's an example:
#include <stdio.h>
int main() {
char str[50];
double n = 0.3984092590879;
sprintf(str, "%lf", n);
printf(str);
return 0;
}
prints out:
0.3984092590879
Printing %.17f you can have a precision up to 0.42179593443870544
printf("\n convert \"lstm_weight\" to lstm_val is : %.17f\n\n", lstm_val);
A double typically has about 15 digits of decimal precision. You will not get more accuracy than that if you store the value in a double. You are getting less because you didn't tell printf how many digits of precision to use for output, so you got the default.
Use something like %0.15f instead of %f.
convert "lstm_weight" to lstm_val is : 0.421795934438705
With %0.20f, I get:
convert "lstm_weight" to lstm_val is : 0.42179593443870544434
That's the best you'll do with a double.

How to check the difference between 012 and 12? [closed]

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 4 years ago.
Improve this question
I will directly get to the point.
int a,b,c, Number;
a=0;
b=1;
c=2;
Number = (a*100)+(b*10)+(c*1);
printf("%d", Number);
Result is 12.
I also have another variable that which is equal to exactly 12.
int number2 = 12;
Now I have to check the difference between them. How do I do that?
Just use a if statement like below:-
int diff = 0;
if ( Number > number2 )
{
diff = Number - number2;
printf("Number is greater than number2 by %d\n", diff);
}
else if( number2 > Number )
{
diff = number2 - Number;
printf("number2 is greater than Number by %d\n", diff);
}
else
printf("Numbers are equal \n");

I get compilation but 0 as my answer [closed]

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 3 years ago.
Improve this question
Calculating e^x but I'm really new to programming. I've made like two programs before. I've fixed any errors the shell points out yet get zero once it is run.
I have this :
#include <stdio.h>
int main (void) {
float answer = 1.0, x, next_term, n;
int power;
printf("Enter x: ");
scanf("%f", &x);
printf("Enter the number of terms: ");
scanf("%f", &n);
next_term = x;
for (power = 1; power <= n ; power = power + 1) {
answer = answer * next_term;
next_term = next_term * x / (power + 1);
}
printf("exp(%f) = %f\n", x, answer);
return 0;
}
Output:
exp(8.000000) = 0.000000
If you're calculating an infinite series, you want to add the terms, not multiply them... i.e. answer=answer+next_term;

Sum of the all the numbers between a and b [closed]

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 7 years ago.
Improve this question
I need to create a program that gives the sum of the all the numbers between the constants of a and b given by the user. b needs to be greater than a.
#include <stdio.h>
void main()
{
int index, begno, endno, sum = 0;
printf("Program for sum of all numbers in the given range\n");
printf("Enter Beg. No.: ");
scanf("%d", &begno);
printf("Enter End. No.: ");
scanf("%d", &endno);
index = begno;
for(; index <= endno; index ++)
sum = sum + index;
printf("The sum of even numbers between %d and %d is: %d", begno, endno, sum);
}
The code given looks OK, but if you want the sum without including the last number, as is generally the case you should change the for loop like this
for(; index < endno; index ++)
I would start by implementing a loop to compute:
$$\sum_{n=a}^{b}
n$$

How to display sum of numbers divided by their factorial? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm a beginner and I am having a really hard time while doing this program.
The question is:
(1/1!)+(2/2!)+(3/3!)+(4/4!)- - - -n
So here are the n number of terms(in which a number is divided by its factorial) and I have to display the output of the sum of any number of terms which are given in scanf function.
Only one thing I know is that this program can be done by using "Nested for" loop but I haven't perfect grip yet on C language. So you guys have to have help me out in this. :)
#include <stdio.h>
#include <conio.h>
void main(void){
int s,a,b,n,fact=1;
//clrscr();
printf("Enter number of terms=");
scanf("%d",&n);
for(a=1;a<=n;a++) {
fact=fact*a;
b=(a/fact);
printf("Sum=%d",s);
}
getche();
}
P.S It's must for me to do it with "Nested for" loop.
No you do not need any Nested for loops to solve your problem. Here's a procedure you may follow:
function factorial
Input: numbers L.
Output: factorial of L.
function sum
Input: n.
Output: sum.
sum = 0;
for i = 1 to n, do
sum ← sum + (i / factorial(i))
return sum
#include <stdio.h>
int main(void) {
// your code goes here
int n;
float sum = 0,d,fact =1,j,i;
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++){
fact = 1;
for (j = 1; j <= i; j++){
fact = fact * j;
}
d = (float) i / (float) fact ;
sum = sum + d;
}
printf("sum = %f", sum);
return 0;
}
Its working ..you can check over here :-https://ideone.com/JVXQVX

Resources