I want to turn a number like 0.1235 to 1235.
i tried to do it through a loop by multiplying by 10 but i didnt know how to stop the loop.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main (){
double a;
printf("a:");
scanf("%lf", &a);
while (/* condition */)
{
a = a*10;
}
printf("a: %lf",a)
getch ();
return 0;
}
int var = (int)round(0.1235 * 10000);
This is not an easy problem to solve as it may seem at first due to decimal precision and lack of implementation details in your question. Here is an attempt of a solution to your problem, but you might need to adjust it depending on your needs.
#include <stdio.h>
#include <math.h>
#define DELTA 0.00001
int get_number_of_decimal_places(double num)
{
int count = 0;
do {
num = num * 10;
++count;
} while (num - (int)num > DELTA);
return count;
}
int main()
{
double a;
int result = 0;
printf("a:");
scanf("%lf", &a);
int decimal_places = get_number_of_decimal_places(a);
do {
a *= 10;
result += (int)a * pow(10, --decimal_places);
a -= (int)a;
} while (decimal_places != 0);
printf("result: %d", result);
getch();
return 0;
}
For input value 0.12345, the output is:
12345
Keep in mind that this solution treats input values 0.1, 0.0001, 0.010 etc. the same way, so the output would be:
1
Related
#include<stdbool.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf_s("%lf", &InputNum);
for (int i = 0; i < InputNum; --NumLoops) //incrementally goes down
{
if (isdigit(InputNum))
{
printf("%lf\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
program incrementally increases by one starting at user's input, this happens 11 times and than ends program, unfortunately it does not do that and keeps printing out the else statement in this code, Any suggestions?
So the problem is that scanf() is converting the integer you enter to a double. So when you type an int for example 5, will be converted to 5.00000, and isdigit() will not convert this to a digit so it always fails.
This is how I would do it.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf("%lf", &InputNum);
for (int i = 0; i < NumLoops; ++i)
{
if ((InputNum - floor(InputNum)) == 0)
{
printf("%f\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
Here's the code snippet, this when run with number 4 outputs 2424242448484848288288288288576576576576. Not sure as to why would the execution would jump back to while loop after exiting the function code. Any help will be appreciated. Thank you in advance.
#include <stdio.h>
#include <string.h>
int result = 1;
void FirstFactorial(int);
void FirstFactorial(int num) {
// code goes here
while (num > 0) {
result = result * num;
num--;
FirstFactorial(num);
}
printf("%d", result);
}
int main(void) {
int var;
// keep this function call here
printf ("Enter your no.\n");
scanf("%d", &var);
FirstFactorial(var);
return 0;
}
Within the function
void
FirstFactorial(int num)
{
// code goes here
while(num > 0)
{
result = result * num;
num--;
FirstFactorial(num);
}
printf("%d", result);
}
each its iteration calls itself num times and all iterations together output the global variable result.
So for example in the first call of the function the function calls itself in the while loop for the range of values [num, 1].
Remove the while loop and do not use the global variable.
Here is a demonstrative program.
#include <stdio.h>
unsigned long long int factorial( unsigned long long int n )
{
return n < 2 ? 1 : n * factorial( n - 1 );
}
int main(void)
{
printf( "%llu! = %llu\n", 4llu, factorial( 4 ) );
printf( "%llu! = %llu\n", 20llu, factorial( 20 ) );
return 0;
}
The program output is
4! = 24
20! = 2432902008176640000
Pay attention that the maximum value you may specify is 20.
Either you implement the factorial with a loop or you do it recursively.
Both ways are feasible but your code mixes it up.
Your function mixes iterative and recursive approaches. You can correct it by removing the useless recursion which causes multiple intermediary results to be computed and printed. Defining result as a global variable is also a mistake, especially since you do not reinitialize it before the loop. Using type long long will allow for larger factorials to be computed. Adding a trailing \n after the printf conversion specifier is advisable too.
Here is a corrected version:
#include <stdio.h>
void FirstFactorial(int num) {
long long result = 1;
while (num > 1) {
result = result * num;
num--;
}
printf("%lld\n", result);
}
int main(void) {
int var;
// keep this function call here
printf("Enter your number\n");
if (scanf("%d", &var) == 1)
FirstFactorial(var);
return 0;
}
The problem was to add the digits of a given number using recursion, for which I wrote the following code:
#include <stdio.h>
#include <math.h>
int addition(signed int x);
int main() {
signed int num;
printf("enter the number : ");
scanf("%d", &num);
printf("%d", addition(num));
return 0;
}
int addition(signed int x) {
signed int sum;
sum = x % 10;
x = x / 10;
if (log10(x) + 1 == 1) {
sum = sum + x;
} else {
sum = sum + addition(x);
}
return(sum);
}
This code works, but the weird problem with it is that, it is only adding digits of the number which starts with one. I can not understand whats going on and why is this happening. If any one can explain, that would be really helpful.
ps: I am a beginner with C so please tolerate and bear me.
thank you.
When you hit the else condition you will recurse infinitely because you're doing addition(x) with an x value of 0 which does not change the argument any further.
You don't really need log10 [and the test is probably wrong].
Here's a cleaned up and working version:
#include <stdio.h>
#include <math.h>
int addition(int x);
int
main()
{
int num;
printf("enter the number : ");
scanf("%d", &num);
printf("%d\n", addition(num));
return 0;
}
int
addition(int x)
{
int sum;
sum = x % 10;
x /= 10;
if (x != 0)
sum += addition(x);
return sum;
}
Decimal to octal conversion c.using code blocks 16.01.the conversion works until 63.This is my first time posting here
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// conversion of decimal to octal
int main()
{
int num,sum,count,x,y;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow(10,count);
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%d",sum);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// conversion of decimal to octal
int main()
{
int num,i,len=0,x[100];
printf("enter a number\t");
scanf("%d",&num);
while (num>0){
x[len]=num%8;
num=num/8;
len++;
}
for(i=len;i>=0;i--)
printf("\n%d",x[i]);
return 0;
}
This will work for the conversion from decimal to octal.
Note: Check the logic works!.Make the sum long int. After 63 your int no longer able to hold the answer.
The problem with the code is actually reported by the compiler if you turn on some warnings, however the problem is that you are implicitly converting from double to int, and depending on the implementation, you'd lose precision. pow returns a double but you're storing it in an int (x)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// conversion of decimal to octal
int main()
{
int num,sum,count,x,y;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow(10,count);
/* ^^^^^^^^^^^^^^^^ problem here */
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%d",sum);
return 0;
}
now the fix
Instead of using the library pow, you should implement your own that's integer only (because you KNOW you are using ints only)
/* a trivial implementation */
long pow10(int n) {
long result = 1;
while(n--) {
result *= 10;
}
return result;
}
Now to clean up the program a bit:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long pow10(int n);
/* a trivial implementation */
long pow10(int n) {
long result = 1;
while(n--) {
result *= 10;
}
return result;
}
int main()
{
int num, count;
long sum, x;
printf("enter a number\t");
scanf("%d",&num);
count = 0;
sum = 0;
while (num>0){
x=num%8;
x=x*pow10(count);
count=count+1;
num=num/8;
sum=sum+x;
}
printf("\n%ld",sum);
return 0;
}
I've created a program which takes an integer x input, then loops until x is met while also taking other integer inputs. I then do various calculations, and then find a square root of a certain value. When I divide by square root however I get a 0 when I know I should be getting a different value as the maths doesn't add up.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(void) {
int multiply1, multiply2, add, squareRoot;
int i;
int n;
int x;
int s;
double divide, test = 0;
scanf("%d", &x);
for (s = 0; s < x; s++) {
scanf("%d %d", &i ,&n);
}
multiply1 = i * i;
multiply2 = n * n;
add = multiply1 + multiply2;
squareRoot = sqrt(add);
printf("%d", i);
test = (i / squareRoot);
printf("Multiplication = %d\n", multiply1);
printf("Multiplication = %d\n", multiply2);
printf("Added together = %d\n", add);
printf("square root = %d\n", squareRoot);
printf("First output = %.3f\n", test);
return 0;
}
You are dividing two integers so the actual division returns the result rounded down. You should instead cast to double and then divide.
test = ((double)i/squareRoot);
There are two things you can do,
Without changing your program, simply cast the i and squareRoot variables to double
test = (double) i / (double) squareRoot;
Change your program and make i and squareRoot a double.
I, would choose 2 because sqrt() returns a double and that might cause an integer overflow.