find prime numbers including and between two numbers using - c

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

Related

Calculate power of all the numbers till n-1

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.

How to print this series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!) in C/C++?

I wish to write a program which calculates the series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!) by taking x and n as user inputs.
This is what i've tried, and well there's no output when I enter the values for x,n:
#include<stdio.h>
#include<math.h>
//#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
double x,n,res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%lf%lf",&x,&n);
/*if(n%2!=0)
{
printf("Please enter a positive value!\n");
exit(0);
}*/
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n %lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r,fact,exec;
for(i=1;i<=t;i+2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1.0;
while(p>0)
{
f*=p;
p--;
}
return f;
}
When I enter values for x and n, it simply shows nothing.
While I've written in C, C++ solutions are also appreciated.
Output window in code::blocks
The loop
for(i=1;i<=t;i+2)
in the function series() is an infinite loop when t >= 1 because i isn't updated in the loop. Try changing + to += and use
for(i=1;i<=t;i+=2)
instead. Also it seems you should use type int for x and n in the function main() because the arguments of series() is int. Don't forget to change the format specifier when changing their types.
Thanks to all those who helped. Here's the final working code:
#include<stdio.h>
#include<math.h>
#include<process.h>
#include<stdlib.h>
double series(int,int);
double factorial(int);
int main()
{
int x,n; double res;
printf("This program will evaluate the following series:\nx-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf("\nPlease enter a value for x and an odd value for n\n");
scanf("%d%d",&x,&n);
if(n%2==0)
{
n=n-1;
}
res=series(x,n);
printf("For the values you've entered, the value of the series is:\n%lf",res);
}
double series(int s, int t)
{
int i,sign=1; double r=0.0,fact,exec;
for(i=1;i<=t;i+=2)
{
exec=sign*(pow(s,i)/factorial(i));
r+=exec;
sign*=-1;
}
return r;
}
double factorial(int p)
{
double f=1;
while(p>0)
{
f*=p;
p--;
}
return f;
}
in loop we step by two for getting odd numbers.by multiplying the current temp variable by the previous temp variable in the loop with neccesary terms like x square and dividing by i*(i-1) i.e for factorial and multiply with -1 i.e to achive negavtive number alternatively. by using this temp variable and adding it to sum variable in every iteration will give us answer.
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n, x;
cout << "enter x and no.of terms: ";
cin >> x >> n;
float sum = 0, temp = x;
for (int i = 3; i < 2 * n + 2; i = i + 2)
{
temp = ((-1 * temp) *(x*x)) / i*(i-1);
sum = sum + temp;
}
cout << x + sum;
return 0;
}
// series x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)
#include<stdio.h>
#include<math.h>
double factorial (int);
double calc (float, float);
int
main ()
{
int x, deg;
double fin;
printf ("x-(x^3/3!)+(x^5/5!)-(x^7/7!)+...(x^n/n!)\n");
printf ("Enter value of x\n");
scanf ("%d", &x);
printf ("highest degree in denom i.e., 1 or 3 or 5 whatever, it should be odd .\n");
scanf ("%d", &deg);
fin = calc (x, deg);
printf ("the summation of series =%1f\n", fin);
return 0;
}
double calc (float num, float res)
{
int count, sign = 1;
double rres = 0;
for (count = 1; count <= res; count += 2)
{
rres += sign * (pow (num, count) / factorial (count));
sign *= -1;
}
return (rres);
}
double factorial (int num)
{
int count;
double sum = 1;
for (count = 1; count <= num; count++)
{
sum *= count;
}
return (sum);
}

C functions in finding a max value [closed]

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 6 years ago.
Improve this question
How to write a C Function that takes three integers as arguments and returns the value of the largest one.
int largest(int x,int y,int z)
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
printf("Largest number = %d", x);
if(y>=x && y>=z)
printf("Largest number = %d", y);
if(z>=x && z>=y)
printf("Largest number = %d", z);
}
I have tried this codes but they don't work i need help please I am also a beginner at this
This should work fine.
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d %d %d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z){
int max;
max=x;
if(y>max){
max=y;
}
if(z>max){
max=z;
}
return max;
}
Try this one:
#include <stdio.h>
int largest(int x, int y, int z);
int main() {
int val1, val2, val3;
int maximum;
printf("enter value \n");
scanf("%d", &val1, &val2, &val3);
maximum = largest(val1, val2, val3);
printf("the largest integer is %d = \n", maximum);
return 0;
}
int largest(int x, int y, int z){
if (x >= y && x >= z)
return x;
if (y >= x && y >= z)
return y;
// otherwise
return z;
}
The problem is that you wanted the method to return the largest value, but simply didnt do that - the code is not compiling because the largest function is defined to "return" an int but there's no return statement anywhere in your function.
If you dont know what exactly "returning function" is then take a look at this tutorial: http://www.cplusplus.com/doc/tutorial/functions/
This is a pretty short way of doing what you want:
int largest(int a, int b, int c)
{
a = (a > b) ? a : b;
a = (a > c) ? a : c;
return a;
}
To return a value you must use a return statement in function.
Let us inspect what you've done,
#include<stdio.h>
int largest(int x,int y,int z)/* missed ';' */
/* missed 'void main()' */
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d",&val1,&val2,&val3); /* missed format specifier for other two values */
maximum=largest(val1,val2,val3);
printf("the largest integer is %d = \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
printf("Largest number = %d", x);/* written print statement instead of return statement */
if(y>=x && y>=z)
printf("Largest number = %d", y);/* written print statement instead of return statement */
if(z>=x && z>=y)
printf("Largest number = %d", z);/* written print statement instead of return statement */
}
After modification the code should be like this,
#include<stdio.h>
int largest(int x,int y,int z);
int main()
{
int val1,val2,val3;
int maximum;
printf("enter value \n");
scanf("%d %d %d",&val1,&val2,&val3);
maximum=largest(val1,val2,val3);
printf("the largest integer is %d \n",maximum);
return 0;
}
int largest(int x,int y,int z)
{
if(x>=y && x>=z)
return x;
if(y>=x && y>=z)
return y;
if(z>=x && z>=y)
return z;
}

C- function for printing each base exponent For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3

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.

C - Powers of a^b for big numbers using Array

The question consists of two numbers, a and b, and the answer to it is the sum of digits of a^b.
I have written the below code. It is giving correct result in all cases. But when the input is as such a < b, then after giving the correct answer, I am getting segmentation fault.
I tried a lot to debug it but could not identify the issue. Any help would be greatly appreciated.
Thanks in advance..!
#include<stdio.h>
void power (int, int, int *product);
int main()
{
int a,b,product[200];
scanf("%d %d",&a, &b);
power(a,b,product);
return 0;
}
void power(int a, int b, int *product)
{
int i,m,j,x,temp,sum=0;
int *p = product;
*(p+0)=1; //initializes array with only 1 digit, the digit 1
m=1; // initializes digit counter
temp=0; //Initializes carry variable to 0.
for(i=1;i<=b;i++)
{
for(j=0;j<m;j++)
{
x = (*(p+j))*a+temp;
*(p+j)=x%10;
temp = x/10;
}
while(temp>0) //while loop that will store the carry value on array.
{
*(p+m)=temp%10;
temp = temp/10;
m++;
}
}
//Printing result
for(i=m-1;i>=0;i--)
sum = sum + *(p+i);
printf("\n%d",sum);
printf("\n");
}
I hope the below code does what you are trying to do. Which is simple and looks good too.
#include<stdio.h>
void power (int, int);
int main()
{
int a,b;
scanf("%d %d",&a, &b);
power(a,b);
return 0;
}
void power(int a, int b)
{
int c=1,sum=0;
while(b>0)
{
c = c*a;
b--;
}
printf("%d\n",c);
while(c!=0)
{
sum = sum+(c%10);
c =c/10;
}
printf("%d\n",sum);
}

Resources