While trying to do the GCD and LCM program from programming simplified...I am facing problems with the results. I did everything correct(according to me) and even checked word by word but the problem still persists...I am pasting the code of normal method only.
#include <stdio.h>
int main()
{
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
gcd = a;
lcm = (x * y)/gcd;
printf("Greatest common divisior of %d and %d = %d\n", x, y, gcd);
printf("Lowest common divisior of %d and %d = %d\n", x, y, lcm);
getch();
}
At least this part is fundamentally wrong:
int a, b, x, y, t, gcd, lcm;
printf("Enter first number :");
scanf("%d", &a);
printf("Enter first number :");
scanf("%d", &b);
a = x;
b = y;
So you're declaring x and y uninitialized, then you're assigning them to a and b - now a and b don't contain the values the user entered, but some garbage. You probably want
x = a;
y = b;
instead.
Better try this. This is simpler to run.
#include<stdio.h>
int GCD(int,int);
void main(){
int p,q;
printf("Enter the two numbers: ");
scanf("%d %d",&p,&q);
printf("\nThe GCD is %d",GCD(p,q));
printf("\nThe LCM is %d",(p*q)/(GCD(p,q)));
}
int GCD(int x,int y){
if(x<y)
GCD(y,x);
if(x%y==0)
return y;
else{
GCD(y,x%y);
}
}
Try it
#include<stdio.h>
int main(){
int a,b,lcm,gcd;
printf("enter two value:\n");
scanf("%d%d",&a,&b);
gcd=GCD(a,b);
lcm=LCM(a,b);
printf("LCM=%d and GCD=%d",lcm,gcd);
return 0;
}
int GCD(int a, int b){
while(a!=b){
if(a>b){
a=a-b;
}else{
b=b-a;
}
}
return a;
}
int LCM(int a, int b){
return (a*b)/GCD(a,b);
}
Related
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
}
In this code I thought I would get the result of calculation x / y and x - y but the program shows 0 for i and j. What is wrong?
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
printf("\nThe results are: %f %f %f\n", calculate(a, b), i, j);
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
It is undefined behavior, when you call the calculate() function within the same printf and i as well as j are calculated within that printf (same function). By the way, it is not a good idea to use global variables ( i, j ) ... For test purpose only, you could calculate() before the next printf of i and j.
You can test that behavior with:
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
printf("\nThe results are: %f", calculate(a, b));
printf(" %f %f\n", i, j);
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
It might be related to the parsing, reference, and execution order of the arguments in printf function. The printf function uses the arguments right-to-left direction. You can easily check the order through below codes.
#include <stdio.h>
float calculate(float, float);
float i, j;
int main()
{
float a, b;
printf("Enter two numbers:\n");
scanf("%f%f", &a, &b);
//printf("\nThe results are: %f %f %f\n", calculate(a, b), i, j);
printf("\nThe results are: %f %f %f\n", i, j, calculate(a, b));
return 0;
}
float calculate(float x, float y)
{
float r;
r = x * y;
i = x / y;
j = x - y;
return r;
}
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 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;
}
My program compiles correctly but i am having problem when i run it. The first scanf (width) works correctly but when i try with another scanf(height) i get segmentation fault 11.
And can i do this program to work without using pointers. (Also i need limit checker function because i have to use it again and again in my program).
#include <stdio.h>
void limitChecker(int x, int y, int* input);
int main(void)
{
int* x;
int* y;
printf("Enter the width of the windows. (3 - 5) : ");
scanf("%d", x);
limitChecker(3, 5, x);
printf("width: %d \n", *x);
printf("Enter the height of the windows. (2 - 4) : ");
scanf("%d", y);
limitChecker(2, 4, y);
printf("Height: %d \n", *y);
}
void limitChecker(int x, int y, int* input)
{
while(!(*input>=x && *input<=y))
{
printf("Please enter a value between (%d - %d): ",x,y);
scanf("%d", input);
}
}
You need to use a reference to the variables used in the scanf().
For example, scanf("%d", &x);
The first parameter of scanf() is for the type of data, and the following parameter(s) are a list of pointers to where you would like the user input to be stored.
CORRECTED CODE:
#include <stdio.h>
void limitChecker(int x, int y, int* input);
int main(void)
{
int x;
int y;
printf("Enter the width of the windows. (3 - 5) : ");
scanf("%d", &x);
limitChecker(3, 5, &x);
printf("width: %d \n", x);
printf("Enter the height of the windows. (2 - 4) : ");
scanf("%d", &y);
limitChecker(2, 4, &y);
printf("Height: %d \n", y);
}
void limitChecker(int x, int y, int* input)
{
while(!(*input>=x && *input<=y))
{
printf("Please enter a value between (%d - %d): ",x,y);
scanf("%d", input);
}
}
You did not allocate memory to hold x and y.
Allocate them on the stack and then use the & address of operator to obtain a pointer to that memory.
#include <stdio.h>
int limitChecker(int x, int y, int input);
int main(void)
{
int x;
int y;
printf("Enter the width of the windows. (3 - 5) : ");
scanf("%d", &x);
x = limitChecker(3, 5, x);
printf("width: %d \n", x);
printf("Enter the height of the windows. (2 - 4) : ");
scanf("%d", &y);
y = limitChecker(2, 4, y);
printf("Height: %d \n", y);
}
int limitChecker(int x, int y, int input)
{
while(!(input>=x && input<=y))
{
printf("Please enter a value between (%d - %d): ",x,y);
scanf("%d", &input);
}
return input;
}
If you want x and y to be pointers then you have to assign them valid memory before you use them.
int * x = malloc(sizeof(int));
int * y = malloc(sizeof(int));
#include <stdio.h>
int limitChecker(int x, int y, int value){
return x <= value && value <= y;
}
int inputInt(void){
//x >= 0
int x = 0;
int ch;
while('\n'!=(ch=getchar())){
if('0'<=ch && ch <= '9')
x = x * 10 + (ch - '0');
else
break;
}
return x;
}
int main(void){
int x, y;
do{
printf("Enter the width of the windows. (3 - 5) : ");
x = inputInt();
}while(!limitChecker(3, 5, x));
printf("width: %d \n", x);
do{
printf("Enter the height of the windows. (2 - 4) : ");
y = inputInt();
}while(!limitChecker(2, 4, y));
printf("Height: %d \n", y);
return 0;
}