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;
}
Related
I need to find prime numbers between and including two numbers, using functions.
For example, with <<(3 23)>> the output is 3 5 7 11 13 17 19 23
This is my code so far, but I’m having troubles with it. What am I doing wrong or how can I improve my solution?
#include<stdio.h>
int check_prime(int l,int u){
int x, i;
for (x = l; x <= u; x++){
for (i = 2; i < x; i++){
if (x % i == 0) break;
}
}
if (i == x) return x;
}
int main(){
int x, y, f;
scanf("%d%d", &x, &y);
f = check_prime(x, y);
printf("%d", f);
return 0;
}
You are printing the value returned from check_prime() and that will be one value only. If you want to print all the prime numbers in a range, i suggest instead of returning value from check_prime() you print the value in that function.
#include<stdio.h>
void check_prime(int l,int u){
int x,i;
for(x=l;x<=u;x++){
for(i=2;i<x;i++){
if(x%i==0)
break;
}
if(i==x){
printf("%d ", x);
}
}
}
int main(){
int x,y;
scanf("%d%d",&x,&y);
check_prime(x,y);
return 0;
}
Here is the executable code: https://repl.it/#fiveelements/PrintPrimeNumbersInARange?language=c
Given n, the program should calculate 1^1 + 2^2 + 3^3 + ... till n-1^n-1. Below is my code, in which there is one function inside while loop which and the passed value is from n-1 in the function. The function definition has two variables which return the ans. Output is wrong always 1.
#include <stdio.h>
#include <stdlib.h>
int power(int x, int y)
{
int la, ans;
if(y==0)
return 1;
else
la= (x*power(x, y-1));
ans+=la;
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, a, b, res, res1;
scanf("%d%d", &n, &m);
while(n-- && n>0)
{
a = power(n-1, n-1);
}
printf("%d", a);
}
return 0;
}
Some problems in your code.
As pointed in another answer, your power function was broken:
ans was not initialized
{ } were missing after the else
in the while, you compute x^x, but you forget the result, whearas you
should sum it.
first thing you do in while loop is to decrease n and to compute power(n-1, n-1)
that sound not logical.
Hence, your corrected code could be:
#include <stdio.h>
#include <stdlib.h>
int power(int x, int y)
{
if(y==0)
return 1;
else
return x*power(x, y-1);
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int n, m, b, a = 0;
scanf("%d%d", &n, &m);
while(n>1)
{
--n;
b = power(n, n);
a += b;
printf("%d^%d -> %3d\n",n, n, b);
}
printf("sum= %d", a);
}
return 0;
}
Gives for n = 6:
5^5 -> 3125
4^4 -> 256
3^3 -> 27
2^2 -> 4
1^1 -> 1
sum=3413
C uses braces to form blocks, your power() function looks like it's wanting to use indentation like in Python.
It should probably be:
int power(int x, int y)
{
int la, ans;
if(y==0)
return 1;
else
{
la= (x*power(x, y-1));
ans+=la;
return ans;
}
}
Of course since the first if has a return, the else is pointless, and you can simplify the code:
int power(int x, int y)
{
if (y==0)
return 1;
return x * power(x, y-1);
}
The variable ans was never assigned to, that looked broken so I simplified it out.
Of course this is susceptible to integer overflow.
Why am I getting the error: Floating point exception: 8
#include<stdio.h>
//grid problem
int fact(int n)
{
int i,f=1;
if(n==0)
return 1;
for(i=1;i<=n;i++)
f*=i;
return f;
}
int uniquePaths(int A, int B) {
float m;
m=fact(A+B-2)/(fact(A-1)*fact(B-1));
return m;
}
int main(int argc, char const *argv[])
{
int a,b;
//aXb grid
scanf("%d%d",&a,&b);
printf("%d\n",uniquePaths(a,b) );
return 0;
}
If you add pre- and postconditions using the function assert you can make sure that parameters and function results have reasonable values:
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
//grid problem
int fact(int n)
{
assert(n >= 0);
int i, f = 1;
if (n == 0) {
return 1;
}
for (i = 1; i <= n; i++) {
f *= i;
}
assert(f >= 1);
return f;
}
int uniquePaths(int A, int B)
{
assert(A >= 1);
assert(B >= 1);
int q = fact(A - 1) * fact(B - 1);
assert(q > 0);
int m = fact(A + B - 2) / q;
assert(m >= 1);
return m;
}
int main(int argc, char const *argv[])
{
int a, b;
//aXb grid
int n = scanf("%d%d", &a, &b);
if (n == 2) {
printf("%d\n", uniquePaths(a, b));
} else {
fprintf(stderr, "invalid input\n");
exit(1);
}
return 0;
}
On my machine, running the program above with intput 10 10, for instance, will result in
t: t.c:16: int fact(int): Assertion `f >= 1' failed.
Aborted
(I don't know why you get a floating point exception however.)
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;
}
My teacher wants the sum of all numbers from x to y... like x+(x+1)+(x+2)...until y. But I think I'm doing something wrong here!
Can someone advice me what is wrong here?
#include <stdio.h>
int sum_naturals(int n)
{
return (n-1) * n / 2;
}
int sum_from_to(int m)
{
return (m-1) * m / 2;
}
void test_sum_naturals(void)
{
int x;
scanf("%d", &x);
int z = sum_naturals(x);
printf("%d\n", z);
}
void test_sum_from_to(void)
{
int x;
int y;
scanf("%d", &x);
scanf("%d", &y);
int z = sum_naturals(x);
int b = sum_from_to(y);
printf("%d\n", z);
}
int main(void)
{
//test_sum_naturals();
test_sum_from_to();
return 0;
}
Your code should in fact be:
int sum_naturals(int n)
{
return (n+1) * n / 2;
}
int sum_from_to(int m)
{
return (m+1) * m / 2;
}
Notice + instead of your -.
To find the sum just add in the function test_sum_from_to this line:
printf("The sum is %d", b-z);
Here's one solution :
#include<stdio.h>
int sum_naturals(int n)
{
return (n+1) * n / 2;
}
int sum_from_x_to_y(int x, int y){
return sum_naturals(y) - sum_naturals(x);
}
main()
{
printf ("Sum: %d \n",sum_from_x_to_y(5, 10));
printf ("Sum: %d \n",sum_from_x_to_y(0, 10));
printf ("Sum: %d \n",sum_from_x_to_y(0, 5));
return 0;
}
Note : sum from 0 to N is (n+1)*n/2 and not (n-1)*n/2