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;
}
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
I was wondering how I can input the numbers using a function with the code written below, and a bit stuck on how I can input and give it an output I am just starting out on functions level 0 at it basically.
int addTwoInt(int a, int b);
int main(void)
{
printf("Enter a number: ");
scanf("%d", &addTwoInt(<#int a#>, <#int b#>));
// printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
return sum;
printf("The sum of the numbers are %d", sum);
}
int addTwoInt(int a, int b);
int main(void)
{
int x;
int y;
printf("Enter a number: ");
scanf("%d", &x);
scanf("%d", &y);
int z = addTwoInt(x, y);
printf("%d", z);
//printf("The two numbers added are %d", addTwoInt);
}
int addTwoInt(int a, int b)
{
int sum;
sum = a + b;
printf("The sum of the numbers are %d", sum);
return sum;
}
You asked for cleaner way to add two numbers or other arithmetic operations u can simply do it in return statement just like this:
int addTwoInts(int a, int b){
return a+b
}
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.
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
#include <stdio.h>
double minimum(double x, double y, double z) {
double temp = 0;
//Logic here
}
int main(void) {
double x, y, z, minVal;
printf("Please enter three numeric values: ");
scanf("%lf%lf%lf", &x, &y, &z);
minVal = minimum(x, y, z);
printf("minimum(%0.10f, %0.10f, %0.10f) = %0.10f\n", x, y, z, minVal);
return 0;
}
Logic of the code should go within comment in first function. Function should then result in minVal and printed too console
#include <stdio.h>
#include <math.h>
double minimum(double x, double y, double z)
{
double temp = 0;
if (isnan(x) || isnan (y) || isnan(z))
return NAN;
temp = (x < y) ? x : y;
return (temp < z)? temp : z;
}
int main(void) {
double x, y, z, minVal;
printf("Please enter three numeric values: ");
scanf("%lf%lf%lf", &x, &y, &z);
minVal = minimum(x, y, z);
printf("minimum(%0.10f, %0.10f, %0.10f) = %0.10f\n", x, y, z, minVal);
return 0;
}
method for double:
int main(void)
{
double a, b, c, temp, min;
printf ("Enter three nos. separated by spaces: ");
scanf ("%lf%lf%lf", &a, &b, &c);
temp = (a < b) ? a : b;
min = (c < temp) ? c : temp;
printf ("The Minimum of the three is: %lf", min);
/* indicate success */
return 0;
}
method for int:
int main(void)
{
int a, b, c, temp, min;
printf ("Enter three nos. separated by spaces: ");
scanf ("%d%d%d", &a, &b, &c);
temp = (a < b) ? a : b;
min = (c < temp) ? c : temp;
printf ("The Minimum of the three is: %d", min);
/* indicate success */
return 0;
}
First of all you should have return type double for the minValue function
Then your logic should be
Consider 3 numbers a, b, c and another double temp
Then...
If (a < b)
Temp = a
Else
Temp = b
If(temp < c)
Return temp
Else
Return c
#include<stdio.h>
int calsum(int x,int y,int z);
int main()
{
while(1)
{
int a, b, c, sum;
printf("Enter any3 numbers");
scanf("%d%d%d", &a, &b, &c);
sum = calsum(a, b, c);
printf("sum=%d\n", sum);
}
}
int calsum (int x, int y, int z)
{
int d;
d = x + y + z;
if(d > 2)
return d;
else
d = 1;
return;
}
when I am giving input as -1 1 0 my output should be 1 but it is giving 0
why?
it is all about adding three numbers
int calsum (int x,int y,int z)
{
return ;
}
Your function is declared and defined to return an int, but your return statement is expressionless. It's a language constraint violation.
The behavior of your program is undefined. Funny results is a possible outcome in this case.
Update your calsum function as below. You are assigning d=1 in else part but not returning it.
int calsum (int x,int y,int z){
int d;
d=x+y+z;
if(d>2)
return d;
else
return 1;
}