Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
this code is showing some syntactical errors I can't find out where it is, may be it is in the gcd function. here is my code.
#include<stdio.h>
#include<conio.h>
int main(void)
int gcd(int,int);
{
int a,b, temp;
printf("Enter the value of a");
scanf("%d",&a);
printf("Enter the value of b");
scanf("%d",&b);
if (b>=a)
{
int temp;
temp=b;
b=a;
a=temp;
}
elseif(b!=0)
{
gcd(a,b);
printf("The Gcd of the numbere is %d",gcd(a,b));
}
else
{
printf("The GCD of %d %d is %d:",a,b,a);
}
getch();
return 0;
}
int gcd(int a,int b)
{
int g;
while(b!=0)
{
g=a%b;
a=b;
b=g;
return g;
}
}
I would be thankful if you point out my errors and explain with the correct code.
switch the position of these two lines:
int main(void)
int gcd(int,int);
also, elseif -> else if
The gcd function uses Euclid's Algorithm. It computes a mod b, then swaps a and b with an XOR swap.
Reference
#include<stdio.h>
int gcd(int ,int );
int main(void)
{
int a,b, temp;
printf("Enter the value of a : ");
scanf("%d",&a);
printf("Enter the value of b : ");
scanf("%d",&b);
int res = gcd(a,b);
printf("The Gcd of the numbere is : %d \n",res);
return 0;
}
int gcd(int a, int b)
{
int temp;
while (b != 0)
{
temp = a % b;
a = b;
b = temp;
}
return a;
}
Related
I was solving a code of maximum and minimum using functions. I wrote the code like this:
#include <stdio.h>
int maxmin(int x, int y);
int main () {
int a, b, c;
scanf("%d%d", &a, &b);
c = maxmin(a, b);
if (maxmin == 1) {
printf("%d is maximum,%d is minimum", a, b);
}
else
printf("%d is maximum,%d is minimum", b, a);
return 0;
}
int maxmin(int x, int y) {
int XisMax = 0;
if (x > y) {
XisMax=1;
}
else {
XisMax=0;
}
return XisMax;
}
So my output shows this results:
Input:9,10;
10 is maximum,9 is minimum
Input:10,9;
9 is maximum,10 is minimum
What is the mistake here? What should I do?
PS:I have an exam on functions so solutions using functions will be helpful.
if (maxmin==1)
change toif (c==1)
your problem is solve.
Have a Good day
You should be checking if(c == 1), not if(maxmin == 1).
Your function can also be made shorter:
int maxmin(int x, int y)
{
if (x>y)
{
return 1;
}
else
{
return 0;
}
}
Also, I think your scanf is missing a comma between the two %d's.
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
I face problems with my code when I enter 1 3 4 . although I couldn't find any error as it works perfectly with other numbers / ps. the was built as a solution to the codechef problem POTATOES
Problem summary: Write a program that inputs an integer T followed by T lines containing two space-separated positive integers. For each of these lines, output the smallest number (>1) that, when added to the sum of these two numbers, results in a sum that is a prime number.
and my code is
#include<stdio.h>
#include<math.h>
int prime(int a,int b);
int main() {
int c;
scanf("%d",&c);
int a,b,d[c];
for(a=0; a<c; a++) {
int x,y;
scanf("%d %d",&x,&y);
b=(x+y);
if(prime(x,y)-b!=0)
d[a]=prime(x,y)-b;
else d[a]=prime((x+1),y)-b;
}
for(a=0; a<c; a++)printf("%d\n",d[a]);
return 0;
}
int prime(int a,int b) {
int c,e;
for(c=2; c<(a+b); c++) {
if((a+b)%c==0) {
b++;
continue;
}
return(a+b);
}
}
Your code is wrong. In your function prime() due to the statement b++; , (a+b) is changing in (a+b)%c but the incremented c is never went back.So for bigger prime numbers your code will fail
Eg:- (89,1) (79,1) etc
Also You don't need that d[c] in your code. You don't need to store every output.When you compute one output just print it .That is ok with code-chef. Also You can divide the problem into to functions.
Try this simplified code :-
#include <stdio.h>
#include <math.h>
int prime(int n);
int make_prime(int a);
int main()
{
int c;
scanf("%d", &c);
int a, b;
for (a = 0; a < c; a++)
{
int x, y;
scanf("%d %d", &x, &y);
b = (x + y);
printf("%d\n", make_prime(b));
}
return 0;
}
int make_prime(int a)
{
int c=1;
while(prime(a+c)==0){
c++;
}
return c;
}
int prime(int n){ // simple prime function
int i,flag=1;
for (i = 2; i <= (n)/2; i++)
{
if ((n) % i == 0)
{
flag=0;
break;
}
}
return flag;
}
can't find an error please help, this is a C code to find minimum number of possible quadrangles on co-ordinate plane
#include <stdio.h>
int quadrangle(int *,int);
int min(int,int);
int main(){
int t,i,j,n,p[n][n];
printf("\nEnter the number of test cases");
scanf("%d",&t);
for(i=0;i<t;i++){
scanf("%d",&n);
for(j=0;j<n;j++){
scanf("%d %d",&p[j][0],&p[j][1]);
}
printf("%d",quadrangle(&p[0][0],n));
}
return 0;
}
int quadrangle(int *p,int len){
int f=0,s=0,t=0,fo=0,i;
for(i=0;i<len;i++){
if(*(p+i*len)>0&&*(p+i*len+1)>0)
f++;
if(*(p+i*len)>0&&*(p+i*len+1)<0)
s++;
if(*(p+i*len)<0&&*(p+i*len+1)<0)
t++;
if(*(p+i*len)<0&&*(p+i*len+1)>0)
fo++;
}
return min(min(f,s),min(t,fo));
}
int min(int a,int b){
if(a>b) return b;
else return a;}
I tested it on codeblocks software it is compiling okay but ends abruptly when I execute it . The control doesn't even enter main() .
n is uninitialized and you create an array of size n (guess what's the value of n) here:
int t,i,j,n,p[n][n];
Declare the array after n gets initialized, i.e, change
int t,i,j,n,p[n][n];
to
int t,i,j,n;
and add
int p[n][n];
after
scanf("%d",&n);
This question already has answers here:
C function declaration
(4 answers)
Closed 8 years ago.
This is a small program to multiply and add two numbers using functions..
#include<conio.h>
#include<stdio.h>
int main()
{
int a,b,result;
clrscr();
printf("Enter two numbers to be added and multiplied...\n");
scanf("%d%d",&a,&b);
add(a,b);
getch();
return 0;
}
int add(int a,int b)
{
int res;
printf("%d + %d = %d", a,b,a+b);
res=mult(a,b);
printf("\n%d * %d = %d",a,b,res);
return 0;
}
int mult(int a,int b)
{
return a*b;
}
Although, I don't think that I need to have a return type add function,so I tried to use this code...
#include<conio.h>
#include<stdio.h>
int main()
{
int a,b,result;
clrscr();
printf("Enter two numbers to be added and multiplied...\n");
scanf("%d%d",&a,&b);
add(a,b);
getch();
return 0;
}
void add(int a,int b)
{
int res;
printf("%d + %d = %d", a,b,a+b);
res=mult(a,b);
printf("\n%d * %d = %d",a,b,res);
}
int mult(int a,int b)
{
return a*b;
}
but it tells me that there's an error for mismatch type declaration??
You need to provide a prototype before the first use:
void add(int a, int b); /* This tells the compiler that add() takes */
/* two ints and returns nothing. */
int main() {
...
add(a, b);
}
void add(int a, int b) {
...
}
Otherwise the compiler is obliged to assume that add() returns an int.
For more information, see Must declare function prototype in C?
First, you have to put declaration of functions before using it:
void add(int a,int b);
int mult(int a,int b);
Then you can put its definition wherever you want.
Or you can declare & define them before using.
I am new to Programming, just a student.
I have written a program to calculate the GCD of two numbers using recursive function, but it's giving the correct answer for some, while it gives wrong answer for a few others. Kindly help me in identifying the problem:
#include<stdio.h>
#include<conio.h>
int gcd(int,int,int)
int main(){
int a,b,x,val;
printf("Enter the first number: ");
scanf("%d",&a);
printf("Enter the second number: ");
scanf("%d",&b);
if(a>b)
x=b;
else
x=a;
val=gcd(a,b,x);
printf("The GCD of the two numbers you entered is:%d",val);
getch();
return 0;
}
int gcd(int a,int b,int x){
if(a%x==0){
if (b%x==0)
return x;
}else
return gcd(a,b,x-1);
}
For example, the program gives a wrong answer when first number = 69, second number = 65, whereas in some other cases it mysteriously gives the right answer.
Can someone help me out here?
Check your code path. Not all condition return an integer in the gcd function.
Which compiler are you using? It should give you a warning or error.
Try this:
int gcd(int a,int b,int x){
if(a%x==0 && b%x==0) return x;
}else return gcd(a,b,x-1);
}
This catches both the modulus comparisons to 0 in one if statement, all conditions not falling within this get gcd calls.
Example: consider the numbers a=100 and b=44. When it reaches x=25, which divides 100, but not 44, your code doesn't have a proper path to take.
You are not taking all the conditions (paths) into consideration.
int gcd(int a,int b,int x){
if(a%x==0){
if (b%x==0)
return x;
else
// ERROR ERROR ERROR,
// if the code reaches here, then it neither calls gcd recursively,
// nor does it return anything valueable
}else
return gcd(a,b,x-1);
}
You should change the code as below.
int gcd(int a,int b,int x){
if(a%x==0)
if (b%x==0)
{
return x;
}
return gcd(a,b,x-1);
}
OR
int gcd(int a,int b,int x){
if(a%x==0 && b%x==0) return x;
return gcd(a,b,x-1);
}
GCD of two numbers in C (the easiest way):
while(secNum != 0) {
Temp = fNum % secNum;
fNum = secNum;
secNum = Temp;
}
printf("GCD of the given two numbers : %d",fNum);