I have
int main ()
{
int x=69057;
int y=23
printf("%d", x);
printf("%d", y);
return 0;
}
And it prints 6905723. How can I convert the printed number into an integer? I can't do
int z=6905723
since in the original program I don't know what value x and y have.
What you essentially want to do here is multiply x by the smallest power of 10 that's larger than y, and then add y to it.
Assuming x and y are small enough to not overflow the long result, you could do something like this:
int x = 69057;
int y = 23
int temp = y;
long result = x;
while (temp > 0) {
result *= 10;
temp /= 10;
}
result += y;
printf("%ld\n", result);
Here's how you can get the answer into int z. It works even if you don't know x and y before. You just need to make sure that the answer will actually fit into an int.
int main ()
{
int x=69057;
int y=23
char buff[512];
sprintf(buff, "%d%d", x, y);
int z = atoi(buff); /* Now z is equal to 6905723 */
return 0;
}
You will run into a problem if y is a negative number, but it isn't clear from your question what you'd like to happen in that situation.
try this. very easy and simple
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int x,y,z,a,b;
x=69057;
y=23;
printf("x=%d y=%d\n",x,y);
a = log10(y) + 1;
for (b=0;b<a;b++)
{
x*=10;
}
z=x+y;
printf("z=%d",z);
return 0;
}
You can write a function to do it for you:
#include <stdio.h>
int my_function(int x, int y);
int main()
{
int x = 69057;
int y = 23;
int z = my_function(x, y);
printf("%d\n", z);
return 0;
}
int my_function(int x, int y)
{
int tmp = y;
do {
x *= 10;
} while (tmp /= 10);
return x + y;
}
Related
The explanation below confused me:
When an argument is pointer to a variable x, we normally assume that x will be modified :
f(&x);
It is possible, though, that f merely needs to examine the value of x, not change it.
I tired to understand and the code below can't work.
#include <stdio.h>
void function(int& a)
{
a = 5;
}
void func(int b)
{
b = 5;
}
int main(void)
{
int x = 0;
function(x);
printf("%d", function(x));
func(x);
printf("%d", func(x));
return 0;
}
Code refer from the second answer:
int f(int &a){
a = 5;
}
int x = 0;
f(x);
//now x equals 5
int f2(int b){
b = 5;
}
int y = 0;
f2(y);
//y still equals 0
An example actually using f(&x):
#include <stdio.h>
void f(int *p) {
*p = 4;
}
int main(void) {
int x;
f(&x); // Provide a pointer to `x`.
printf("%d\n", x); // 4
return 0;
}
Both of your program use int &a, which isn't a valid C declaration. That is why they don't even compile.
I created a factorial function and my output for integer b is incorrect when it is set at 5, any ideas as to why? b should be equal to the integer 120 and I am getting the number -95449088 after I compile and run it.
#include <stdio.h>
#include <stdlib.h>
int factorial(int x)
{
int i;
for(i=1; i < x; i++)
x *= i;
return x;
}
int main() {
int a=5, b;
b = factorial(a);
printf("%d\n", b);
}
Here you are using the same x value in the condition check of for-loop which is a mess up. Instead you can store the variable x to a variable temp and use this temp variable for checking the condition.
Please see below the corrected code
#include <stdio.h>
#include <stdlib.h>
int factorial(int x)
{
int i,temp=0;
temp = x; //store the x to temp
for(i=1; i < temp; i++){
x *= i;
}
return x;
}
int main() {
int a=5, b;
b = factorial(a);
printf("%d\n", b);
}
Other have explained that you should avoid mixing input and output variables. This is good advice, and as a beginner you should try to observe it.
But this is a special case, and here you can re-use the input value, provided you use a decreasing loop:
int factorial(int x)
{
int i;
for (i = x-1; i >1; i--)
x *= i;
return x;
}
It works because it implicitly initializes the return value with x and then multiplies it by all numbers below it, which is a possible definition for the factorial.
#include <stdio.h>
int f(int x, int y) {
for (int i = 10; i > 5; i--) {
if (x % i == 0) {
y = x ^ 3;
printf("x is %d and y is %d\n", x, y);
return x + y;
}
else {
y = x + 1;
printf("x is %d and y is %d\n", x, y);
return x * y;
}
}
int main() { // I am getting error on this line.Function definition is not
// allowed here.
int a = f(30, 10);
int b = f(20, 5);
return 0;
}
}
Xcode displays this as a parse issue. please help me fix this code.
You missed a } to end function f(). So by mistake you placed main() inside the function f().
The problems within your code is,
Here the closing brace } of int f(int x, int y) is missing. Please check my comments inside the code itself.
One more closing brace } is added at the end of your program which is not required.
The corrected code is
#include <stdio.h>
int f(int x, int y) {
for (int i = 10; i > 5; i--) {
if (x % i == 0) {
y = x ^ 3;
printf("x is %d and y is %d\n", x, y);
return x + y;
} //Closing brace of 'if' condition
else {
y = x + 1;
printf("x is %d and y is %d\n", x, y);
return x * y;
} //Closing brace of 'else' condition
} //Closing brace of for-loop
} //Here add the closing brace of 'int f(int x, inty)'
int main() {
int a = f(30, 10);
int b = f(20, 5);
return 0;
} //Removed the last '}' in your code
I am calculating the power of x raised to n. I can't understand one thing: why is it showing segmentation fault when I am both declaring and initializing the temp variable at the start? I do know what segmentation fault is, but why is it showing.
#include<stdio.h>
int power(int x,unsigned int y)
{
int temp=power(x,y/2);
if(y==0)
return 1;
if(y%2==0)
return temp*temp;
else
return x*temp*temp;
}
//Driver function
int main(int u, int v)
{
printf("Enter the value of u and v");
scanf("%d %u",&u,&v);
printf("%d",power(u,v));
return 0;
}
You will recurse infinitely. You need a small adjustment [please pardon the gratuitous style cleanup]:
#include <stdio.h>
int
power(int x, unsigned int y)
{
//int temp = power(x, y / 2);
if (y == 0)
return 1;
int temp = power(x, y / 2);
if (y % 2 == 0)
return temp * temp;
else
return x * temp * temp;
}
// Driver function
int
main(int argc,char **argv)
{
int u;
unsigned int v;
printf("Enter the value of u and v");
scanf("%d %u", &u, &v);
printf("%d\n", power(u, v));
return 0;
}
I can't seem to get my program running
Write a function integerPower(base, exponent) that
returns the value of
Baseexponent. For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3. Assume that
exponent is a positive, nonzero
integer, and base is an integer. Function integerPower should use for to
control the calculation.
Do not use any math library functions.
i have this program
#include<stdio.h>
int power(int b, int e){
int x
for (x = 0; x <= e; x++)
b=b;
return b;
}
void main(){
int b = 0;
int e = 0;
scanf("input a base %d\n", &b);
scanf("input an exponent %d\n", &e);
power(b,e);
printf("%d" , b);
}
In the loop
for (x = 0; x <= e; x++)
b=b;
the line b=b; is useless. It just assign the value of b to itself. You need to multiply b by e times. For this you need to take another variable with initial value 1 and multiply it by b at each iteration of loop to get be.
Change your function to this
int power(int b, int e){
int x, y = 1;
for (x = 1; x <= e; x++)
y = y*b; // Multiply e times
return y;
}
#include<stdio.h>
int power(int b, int e){
int x;
int result = 1;
for (x = 0; x < e; x++)
result*=b;
return result;
}
int main(){
int b = 0;
int e = 0;
printf("Input a base ");
scanf("%d", &b);
printf("Input an exponent ");
scanf("%d", &e);
b = power(b,e);
printf("%d" , b);
return 0;
}
First problem is with the scanf function. you are using "input a base ". For this to output you have to use printf("input a base ").
Try using the below function to calculate the value of base raised to the power of exponent
int ipower(int b, int e)
{
int x, tmp = 1;
for (x = 0; x < e; x++)
tmp *= b;
return tmp;
}
Here the for loop is iterated for e times that is from 0 to e-1. "tmp" will hold the result.
Make sure you don't change the value of "b/e"(base/exponent) inside the loop, else will lead to wrong result.
doing b*=b in function body won't help either. And why initialize b and e to zero when you're supposed to input them from the user?
Try this:
#include<stdio.h>
int power(int b, int e) {
int temp = b;
int x;
for (x = 1; x < e; x++)
b *= temp;
return b;
}
void main() {
int b ;
int e;
scanf_s(" %d", &b);
scanf_s(" %d", &e);
int h= power(b, e);
printf("%d", h);
}
int power(int b, int e){
int x,t=1
for (x = 1; x <= e; x++)
t*=b;
return t;
}
if user give e= 1 then also its work.