#include<stdio.h>
void Multi(int num1, int num2)
{
int a, b;
if (num1 < num2)
a = num1, b = num2;
else
a = num2, b = num1;
for (a; a > b; a++)
{
for (int i = 1; i < 10; i++)
printf("%d * %d = %d \n", a, i, a * i);
}
};
int main(void)
{
int a, b;
printf("enter two numbers :\n");
scanf_s("%d %d", &a, &b);
Dan(a, b);
return 0;
}
Both of these programs do not execute the For statement. I think there's some problem with the variables.
This for loop (the body of the loop)
for (a; a > b; a++)
is never executed because in the preceding code a is set such a way that it is less than or equal to b. So a > b always evaluates to false.
You need to write either
for ( ; a < b; a++)
or
for ( ; a <= b; a++)
Also it would be more safer to write the call of printf the following way
printf("%d * %d = %lld \n", a, i, ( long long )a * i);
Related
I thought of declaring the variables first, and then finding the gcd and lcm. But when I tried to run, my code is not working. And the VS code is not even showing the errors. I am posting my code here:
#include <stdio.h>
int gcd(int a, int b)
{
for (int j = 1; j <= a && j <= b; ++j)
{
if (a % j == 0 && b % j==0)
return j;
}
}
int main ()
{
int i, n, pro=1, g, t, lcm;
int num[n];
printf ("Enter the no. of numbers: ");
scanf ("%d", &n);
for (i = 0; i <= n-1; i++)
{
printf ("Enter the number: ");
scanf ("%d", &num[i]);
}
g = gcd (num[0], num[1]);
for (t=2; t <= n; t++)
g = gcd(num[t], g);
for (i=0; i <= n-1; i++)
pro = pro*num[i];
lcm = pro/g;
printf ("GCD is %d\n", g);
printf ("LCM is %d", lcm);
return 0;
}
Well, I tried doing it again. The problem with my code was that the return j was in the loop, and thus, it was returning the value of j instead of giving the value of g.
The corrected code:
#include <stdio.h>
int gcd(int a, int b)
{
int g;
for (int j = 2; j <= a && j <= b; ++j)
{
if (a % j == 0 && b % j==0)
g = j;
}
return g;
}
int main ()
{
int i, n, pro=1, g, t, lcm;
printf ("Enter the no. of numbers: ");
scanf ("%d", &n);
int num[n];
for (i = 0; i <= n-1; i++)
{
printf ("Enter the number: ");
scanf ("%d", &num[i]);
}
g = gcd (num[0], num[1]);
for (t=2; t <= n; t++)
g = gcd(num[t], g);
for (i=0; i <= n-1; i++)
pro = pro*num[i];
lcm = pro/g;
printf ("GCD is %d\n", g);
printf ("LCM is %d", lcm);
return 0;
}
The following code uses a user define function to print fibonacci numbers up to the user's choosing. The only problem is that the output includes the first n fibonacci numbers, followed by a zero. (e.g. 1,1,2,3,5,8,0) How can I get rid of the zero?
#include <stdio.h>
int fibonacci(int a);
int main()
{
int j, number;
printf("Enter the number of terms: ");
scanf("%d", &number);
printf("Fibonacci Series: ");
j = fibonacci(number);
printf("%d", j);
}
int fibonacci (int a)
{
int num1 = 1, num2 = 1, k, i;
for (i = 1; i <= a; ++i)
{
printf("%d, ", num1);
k = num1 + num2;
num1 = num2;
num2 = k;
}
return 0;
}
I learned that we are supposed to return something when using functions other than main.
If you learned that, you should look for a better learning source. You could well have written:
void fibonacci(int a)
{
int num1 = 1, num2 = 1, k, i;
for (i = 1; i <= a; ++i)
{
printf("%d, ", num1);
k = num1 + num2;
num1 = num2;
num2 = k;
}
}
Hi I am trying to write a code in C to find the GCD for more than 2 numbers. However, when I compile and run the code, the GCD is always 0. Would appreciate if anyone can help. Thank you.
#include <stdio.h>
static int gcd(int x, int y)
{
int r;
if (x <= 0 || y <= 0)
return 0;
while ((r = x % y) != 0)
{
x = y;
y = r;
}
return y;
}
int main (void)
{
int A[5];
int g = A[0];
int i;
int n;
printf ("How many elements are there? \n")
scanf ("%d", &n);
printf ("Input the elements. \n");
for (i = 0; i < n; i++)
{
scanf ("%d", &A[i]);
}
for (i = 1; i < n; i++)
g = gcd(g, A[i]);
printf ("GCD is: %d \n");
return 0;
}
You set g equal to A[0] before you set A[0] to any particular value.
You need a ; after the first printf.
You need to declare A with n elements after you read the number of elements n.
You have to write the g after ", in your last printf.
This is how I think your main should look like:
int main (void)
{
int i;
int n;
printf ("How many elements are there? \n");
scanf ("%d", &n);
int A[n];
printf ("Input the elements. \n");
for (i = 0; i < n; i++)
{
scanf ("%d", &A[i]);
}
int g = A[0];
for (i = 1; i < n; i++)
g = gcd(g, A[i]);
printf ("GCD is: %d", g);
return 0;
}
int a,b,n;
printf("Input Natural Number n (n<2,100,000,000) : ");
scanf("%d",&n);
for(a=1;a<=100;a++)
for(b=1;b<=100;b++)
if(a<b && a*a + b*b == n*n)
{
printf("(%d, %d, %d)\n",a,b,n);
}
/*else
{
printf("impossible \n");
}
*/
return 0;
if I delete 'else' the program runs correctly. But I want to make another function which can check the number has pythagorean numbers or not by using 'else' paragraph. But when I put 'else' paragraph in that code, the result is dizzy.... plz help me!!
Put braces around the nested code blocks.
int a, b, n;
int impossible = 1;
printf("Input Natural Number n (n<2,100,000,000) : ");
scanf("%d", &n);
for (a = 1; a <= 100; a++) {
for (b = 1; b <= 100; b++) {
if (a < b && a * a + b * b == n * n) {
printf("(%d, %d, %d)\n", a, b, n);
impossible = 0;
}
}
}
if (impossible == 1) printf("impossible \n");
return 0;
Here is a possible answer
#include <stdio.h>
int power(int base, int power);
int main(){
int N;
printf("INput the Num: ");
scanf("%d", &N);
int a, b, c;
for(a = 0; a < N ; a++) {
for(b = 0; b< N; b++) {
if ((a < b) && (b < N - a - b)) {
if (power(a, 2) + power(b, 2) == power(N - a - b, 2)) {
printf("%d^2 + %d^2 = %d^2 \n", a, b, N-a-b);
}
}
}
}
}
int power(int base, int power) {
int result = 1;
for(int i = 0; i < power ; i++) {
result *= base;
}
return result;
}
I have created the following program to find factorial factors, but I am not able to understand why the value of i becomes negative after a few iterations.
#include <stdio.h>
int main()
{
int a,b,i;
printf("enter the number: ");
scanf("%d", &a);
printf("entered value is %d\n", a);
for(i = 1; i < a; i++)
{
printf("iterating for a = %d\n", a);
b = a % i;
if(b == 0)
{
printf("%d\n", i);
}
else
{
printf("a = %d, i = %d, modulo = %d\n", b);
}
}
return (0);
}
Fix:
printf("a = %d, i = %d, modulo = %d\n", b);
to
printf("a = %d, i = %d, modulo = %d\n", a, i, b);
Also, your program doesn't find factorial!
b =1;
for(i = 1; i <= a; i++)
b*=i;
printf(" Factorial for a = %d \n", b);
you do not print i in last printf. change it to:
printf("a = %d, i = %d, modulo = %d\n", a, i, b);
No i not become 0. I try this code for 6 and 10 iteration. Its not giving negative value of i. In my case its giving value of i=1298 maybe garbage value. Maybe you are trying more in number of iteration thats why after some iterations negative value of i.