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 5 years ago.
Improve this question
How can I remove the zeros that will not affect the number value
float X1 = (-b + sqrt(pow(b,2) - (4*a*c)))/(2*a);
sometimes if the X1 was a number without a decimal it will be X1=4.0000
how can I avoid that
{edited}
option #1
Use %g of printf
Unless alternative representation is requested the trailing zeros are
removed, also the decimal point character is removed if no fractional
part is left.
code sample:
double x = 4.0;
printf("DEBUG:%f\n", x);
printf("X1=%g\n", x);
OUTPUT:
DEBUG:4.000000
X1=4
option #2
Output it to a string and process it.
code sample:
double x = 4.0;
char buff[48];
int len;
len = snprintf(buff, sizeof buff, "%.4f", x);
printf("DEBUG:%s\n", buff);
char *p = buff + len -1;
while(*p == '0'){
*p-- = 0;
}
if(*p == '.')
*p = 0;
printf("X1=%s\n", buff);
OUTPUT:
DEBUG:4.0000
X1=4
Related
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 1 year ago.
Improve this question
Can someone help me out with correcting this. The question was supposed to be (Write a code to get the value for expression 1 + x + x^2 + x^3 + ...... + x^n)
#include<stdio.h>
int main()
{
int power = 1,sum = 0,n,x , i;
printf("Enter the number : ");
scanf("%d",&x);
printf("Enter the limit to fill the following series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum = sum + power;
while (i>0)
{
power = power * x;
i = i - 1;
}
}
printf("The sum is %d",sum);
return 0;
}
Change
while (i>0)
{
power = power * x;
i = i - 1;
}
to
power = power * x;
As this loop is not required
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 need to print my characters BEFORE the previously printed character using printf. For example:
printf("1")
printf("0")
would need to output:
01
Is there a way to do this? I cannot use arrays. To be clear I'm printing in base two (binary) representation using a divide by two algorithm:
for(int i = 0; i < 16; i++){ // 16 bit int
tmp = num % 2;
if(tmp == 1){
printf("1");
} else {
printf("0");
}
num /= 2;
}
The above code prints the binary representation backwards.
Here is my solution to print binary without using array. you can ignore initial zeros by adding another if condition.
#include<stdio.h>
int main(){
int num =50;
int i;
for(i=15;i>=0;i--){
if( (1<<i) & num){
printf("1");
}
else printf("0");
}
return 0;
}
output:
0000000000110010
Recursion can perform like a "print before". #rici
The below does not always print 16 digits, just the decimal digits needed.
void print_binary(unsigned n) {
// Break the digits into 2 groups: 1) the one least digit and 2) the rest.
unsigned last_digit = n/10;
unsigned all_the_other_more_significant_digits = n/10;
// Let us print those more significant digits first;
if (all_the_other_more_significant_digits > 0) {
print_binary(all_the_other_more_significant_digits);
}
// Now print the last digit
printf("%u", last_digit);
}
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 2 years ago.
Improve this question
I'm going a bit mad implementing this function. I think I have narrow the algorithm down but I have some strange behavior for some value. It seems to work for most of the values and bases, but for string "1000" it simply returns 0.
Below the code:
// A C program for
// implementation of atoi
#include <stdio.h>
#include <stdint.h>
int val(char c)
{
if (c >= '0' && c <= '9')
return (int)c - '0';
else
return (int)c - 'A' + 10;
}
int32_t my_atoi(uint8_t * ptr, uint8_t digits, uint32_t base){
// Initialize result
digits = 0;
// Initialize sign as positive
int sign = 1;
// Initialize index of first digit
int i = 0;
// If number is negative,
// then update sign
if (*ptr == '-') {
sign = -1;
i++;
}
// Iterate through all digits and update the result
for (; *(ptr+i) != '\0'; ++i){
digits = (digits * base) + val(*(ptr+i));
printf("Digits of %d is:%d\n",i,digits);
}
// Return result with sign
return sign * digits;
}
// Driver program to test above functions
int main()
{
uint8_t str[] = "1000";
uint8_t val2=0;
int val = my_atoi(str, val2, 16);
printf("%d \n", val);
return 0;
}
For the above code the output is:
Digits of 0 is:1
Digits of 1 is:16
Digits of 2 is:0
Digits of 3 is:0
0
I simply cannot understand why digits becomes 0 after it has the value 16.
Any help will be greatly appreciated.
Your digits is a uint8_t so it just overflows to 0. You should make it a larger, signed, data type like int32_t or int64_t so the sign works properly and it doesn't overflow.
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 don't understand this, as far as I understand this should goes like this;
4*4^2*4
but it doesn't, I know it has a simple explanation but still I tried to figure out this like for 20 minutes, I hope someone helps. Sorry for the bad editing too.
int power(int n1, int n2);
int main() {
int base, powerRaised, result;
printf("Enter base number: ");
scanf("%d", &base);
printf("Enter power number(positive integer): ");
scanf("%d", &powerRaised);
result = power(base, powerRaised);
printf("%d^%d = %d", base, powerRaised, result);
return 0;
}
int power(int base, int powerRaised) {
if (powerRaised != 0)
return (base * power(base, powerRaised - 1));
else
return 1;
}
The code behaves as expected, the power function is recursive. For arguments 4 and 2, it calls itself recursively twice:
power(4, 2)
-> 4 * power(4, 1)
-> 4 * (4 * power(4, 0))
-> 4 * (4 * 1)
-> 16
You might be more familiar with an iterative approach:
int power(int base, int powerRaised) {
int res = 1;
while (powerRaised > 0) {
res = res * base;
powerRaised--;
}
return res;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
For example:
1: 123456 should be converted to 214365
2: 12345 should be converted to 103254
int32_t conv(int32_t n){
char buff[16], *p = &buff[1];
int i, v = n < 0 ? -n : n;
sprintf(p, "%010d", v);
for(i=0;i<5;++i,++p){
char c;
c = *p;
*p = p[1];
*(++p) = c;
}
buff[0] = (n < 0) ? '-' : ' ';
return atoi(buff);
}
size_t conv(size_t n){
size_t q, r, wk, mul=1, ret = 0;
for(;n;n/=100, mul*=100){
wk = n % 100;
r = wk % 10;
q = wk / 10;
ret += (r * 10 + q)*mul;
}
return ret;
}
,
First to clarify: there's a difference between an integer and the decimal representation of that integer in a string. You want the second.
As H2CO3 points out you can do this on your own using modulo's: decompose your number into the decimal base, do the swaps then recompose the number (only mul/add).
Here's another way:
Create a string from your integer using malloc and sprintf.
Loop through the string and do the swapping of characters
print that string or if you want convert it back to an integer with atoi