I wanted to print a number to variable number of decimal places in C.
I have written the code
#include<stdio.h>
main()
{ int a;
printf("Upto which number of decimal places you want to print value of '2.554648' ?");
scanf("%d", &a);
printf("Value of '2.554648 upto %d number of decimal places = %.af", a, 2.554648);
return 0;
}
Use * in printf() to mark how many decimal places you want:
#include <stdio.h>
int main(void)
{
int a;
printf("Upto which number of decimal places you want to print value of '2.554648' ?");
scanf("%d", &a);
printf("Value of '2.554648 upto %d number of decimal places = %.*f", a, a, 2.554648);
return 0;
}
You need the * format specifier. Here is a short example(see in ideone):
#include <stdio.h>
int main(void) {
int a = 5;
double temp = 5.0 / 7;
printf("%.*f",a, temp);
return 0;
}
Related
Do you know how a number can be rounded up and printed with a precision that is not fixed by some natural number, but by some variable? If the user needs to enter how many decimal places to round, how to solve it?
#include <stdio.h>
int main()
{
int r;
double var = 37.66666;
scanf("%d", &r);
printf("%.2f", var);
return 0;
}
Here's simple approach that would work in your case:
You just need to put * before f, and that's it.
#include <stdio.h>
int main()
{
int r;
double var = 37.66666;
scanf("%d", &r);
printf("%.*f",r, var);
return 0;
}
You can put a * in place of the precision, in which case you can specify an int as the precision.
printf("%.*f", r, var);
I have compiled this code and it works just fine up to value 4 then it starts returning character instead of integer. I am talking about first equation => x= num*2; Here when I enter num value as 5 the output returns a.
#include <stdio.h>
int main(void)
{
int num;
int x; This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num); //
//printf("%d\n", num);
x = num * 2 ;
printf("%x\n", x);
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
}
please tell me if there is a mistake I am a newbie to coding.
As I see you are learning C language, and after reading your explanation, I feel that you want to print the integer value of variable x.
Kindly replace %x with %d in the print statement of variable x,
and you will be successfully able to print the value.
#include <stdio.h>
int main(void)
{
int num;
int x; // This right here is an integer still it returns a character
char s[10] = "helloworld";
char f[10];
scanf("%d", &num);
x = num * 2 ;
printf("%d\n", x); // %d for integer and %x for hexadecimal values
scanf("%c", &f[10]);
if(s[10] = f[10]){
printf("helloworld");
}
return 0;
}
Finally, do read more about format specifiers in scanf and
printf statements.
Scan a number along with its base value. If a number starts with 0 it is octal nd if it starts with 0x it is hexadecimal. print the corresponding decimal value.
#include<stdio.h>
int main()
{
int a,b;
scanf("%x %d",&a,&b);
printf("%d",a);`
}
3 test cases shows error .please help me with this.
Use scanf i flag:
Matches an optionally signed integer; the next pointer
must be a pointer to int. The integer is read in base 16
if it begins with 0x or 0X, in base 8 if it begins with 0,
and in base 10 otherwise. Only characters that correspond
to the base are used.
#include<stdio.h>
int main(void)
{
int a,b;
scanf("%i %i",&a,&b);
printf("%d %d\n", a, b);`
return 0;
}
#include<stdio.h>
int main(void)
{
int oc,hex;
scanf("%i",&oc);
scanf("%i",&hex);
printf("\ndecimal:%d",oc);
printf("\ndecimal:%d",hex);
return 0;
}
another way
#include<stdio.h>
int main()
{
int oc,hex;
printf("enter the octal value:");
scanf("%o",&oc);
printf("enter the hex value:");
scanf("%x",&hex);
printf("\noctal to decimal:%d",oc);
printf("\nhex to decimal:%d",hex);
}
i want to convert from binary to decimal but convert function does not return any thing where is the problem ??
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int convert(int x); //fun to convert from binary to decimal
int main()
{
int x; //binary number
printf("plz enter binary number !\n\nbinary: ");
scanf("%d",&x);
printf("\ndecimal: ",convert(x));
return 0;
}
int convert(int x)
{
int sum=0; //decimal number
int i=0;
int r; //remainder
while(x!=0){
r=x%10;
sum+=r*pow(2,i);
x=x/10;
i++;
}
return sum;
}
You can also try
scanf("%i", &x);
printf("%d", x);
you should change your printf in main().
printf("\ndecimal: %d",convert(x));// add %d
Because to print a value by printf need to spacify the data type such as for int %d for character %c.
binary numbers are series of ones and zeros (0|1), if you are referring to this type of numbers then check this source code below. you may also find this link useful http://www.math.grin.edu/~rebelsky/Courses/152/97F/Readings/student-binary
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int bintodec(char bin[32]){
int dec=0;
while(*bin){
if((*bin!='0') && *bin!='1')
break;// only accept '0' & '1'
dec<<=1;
dec+=*bin-'0';
bin++;
}
return dec;
}
int main(){
char bin[32]=""; //i believe binary numbers are series of '1' and '0'
printf("plz enter binary number !\n\nbinary: ");
scanf("%s",bin);
printf("\ndecimal: %d\n",bintodec(bin));
return 0;
}
Use format specifier:
%d for integer (in this case)
%c for character
%f for float value
%s for string
%lf for double
%x for hexadecimal
In this case change print statement to:
printf("\ndecimal: %d",convert(x));
The program is just a base 10 to base 2 converter it works fine. I just want the cursor at the new line.
I have put "\n" everywhere I can logically put it but it just does not bring the cursor to the new line. The program is doing what I need it to do.
EX
"Enter a decimal number from 0 to 18,446, 744,073,709,551,615: 156"
The binary value for 156 is:10011100{{CURSOR}}
(I want the {{CURSOR}} here)
#include <stdio.h>
int main(void)
{
long int dec, num;
int store[100], i=1,j;
printf("Enter a decimal number from 0 to 18,446,744,073,709,551,615: ");
scanf("%ld",&dec);
num = dec;
while(num!=0)
{
store[i++]=num%2;
num=num/2;
}
printf("The binary value for %d is: ",dec);
for(j=i-1;j>0;j--)
{
printf("%d",store[j]);
}
return 0;
}
When all is said and done, you need to print a newline after the number is done using printf("\n"). The rest, like you said, does what it should:
#include <stdio.h>
int main(void)
{
long int dec, num;
int store[100], i=1,j;
printf("Enter a decimal number from 0 to 18,446,744,073,709,551,615: ");
scanf("%ld",&dec);
num = dec;
while(num!=0)
{
store[i++]=num%2;
num=num/2;
}
printf("The binary value for %d is: ",dec);
for(j=i-1;j>0;j--)
{
printf("%d",store[j]);
}
printf("\n");
return 0;
}