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.
Related
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 3 months ago.
Improve this question
#include <stdio.h>
int main() {
int num;
printf("Enter the number you want multiplication table of:\n");
scanf("%d", &num);
printf("multiplication table of %d is as follow:\n", num);
//printf("%d X 1 = %d\n", num, num * 1);
//printf("%d X 2 = %d\n", num, num * 2);
//printf("%d X 3 = %d\n", num, num * 3);
//printf("%d X 4 = %d\n", num, num * 4);
//printf("%d X 5 = %d\n", num, num * 5);
//printf("%d X 6 = %d\n", num, num * 6);
//printf("%d X 7 = %d\n", num, num * 7);
//printf("%d X 8 = %d\n", num, num * 8);
//printf("%d X 9 = %d\n", num, num * 9);
//printf("%d X 10 = %d\n", num, num * 10);
//printf("%d X 11 = %d\n", num, num * 11);
//printf("%d X 12 = %d\n", num, num * 12);
//printf("%d X 13 = %d\n", num, num * 13);
//printf("%d X 14 = %d\n", num, num * 14);
//printf("%d X 15 = %d\n", num, num * 15);
//printf("%d X 16 = %d\n", num, num * 16);
//printf("%d X 17 = %d\n", num, num * 17);
//printf("%d X 18 = %d\n", num, num * 18);
//printf("%d X 19 = %d\n", num, num * 19);
//printf("%d X 20 = %d\n", num, num * 20);
for (int i = 1; i < 11; i++) {
printf("%d X %d = %d\n", num, i, num * i);
}
return 0;
}
I need a quick answer.
I am facing a problem in the table multiplication in C. And I have been stuck in it. I'm expecting that I will get the answer of the this problem.
The for loop you wrote is a good approach at simplifying the commented code.
It you want the full table, you should run the loop from i = 1 to i <= 20. You should also check the return value of scanf().
Here is a modified version:
#include <stdio.h>
int main() {
int num;
printf("Enter the number you want multiplication table of:\n");
if (scanf("%d", &num) != 1) {
printf("invalid input\n");
return 1;
}
printf("multiplication table of %d is as follows:\n", num);
for (int i = 1; i <= 20; i++) {
printf("%d X %d = %d\n", num, i, num * i);
}
return 0;
}
I'm creating a program that compares numbers with their respective exponents, and indicates which of the three is the smallest. The problem is that I wouldn't want it to perform the operation, for example:
Let's say the smallest number was $4^{8}$, so it would calculate that value, but I want it to just print "4^8" on the screen, or related things, but without doing that operation (in this case, it prints 65536).
int main()
{
int a, b, c, smaller, x, y, z, for_a, for_b, for_c;
printf("first value: ");
scanf("%d", &a);
printf("Second value: ");
scanf("%d", &b);
printf("Third value: ");
scanf("%d", &c);
printf("Value of x: ");
scanf("%d", &x);
printf("Value of y: ");
scanf("%d", &y);
printf("Value of z: ");
scanf("%d", &z);
for_a= pow(a, x);
for_b= pow(b, y);
for_c= pow(c, z);
if (for_a< for_b && for_a < for_c){
smaller= for_a;
}
else if (for_b < for_c){
smaller = for_b;
}
else {
smaller = for_c;
}
printf("Smaller= %d\n", smaller);
return 0;
}
You need to identify the set of data that corresponds to your result. Storing the result only is not enough.
You could do this as follows:
#include <stdio.h>
#typedef struct values_s{
int base;
int exponent;
int result;
} values_t;
#define NUMBER_OF_VALUES 3
int main(void)
{
values_t values[NUMBER_OF_VALUES];
for (int i = 0; i < NUMBER_OF_VALUES; i++)
{
printf("Base value #%d: ", i+1);
fflush(stdout);
int result = scanf("%d", &values[i].base);
// TODO: Error handling in case result != 1
}
for (int i = 0; i < NUMBER_OF_VALUES; i++)
{
printf("Exponent #%d: ", i+1);
fflush(stdout);
int result = scanf("%d", &values[i].exponent);
// TODO: Error handling in case result != 1
}
for (int i = 0; i < NUMBER_OF_VALUES; i++)
{
values[i].result = pow(values[i].base, values[i].exponent);
}
int smallest = 0;
for (int i = 1; i < NUMBER_OF_VALUES; i++)
{
if (values[i].result < values[smallest].result)
{
smallest = i;
}
}
printf("Smallest: %d^%d = %d\n", values[smallest].base, values[smallest].exponent, values[smallest].result);
return 0;
}
This is based on the assumptions that your variables of type int are large enough to hold the values you are dealing with. Otherwise you need to adjust accordingly.
#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);
instructions are:
Write a program which reads 10 different integers from the user and finds and prints
a) Minimum element
b) Sum of all elements and
c) Average of elements.
Note: do not use arrays to store the user-entered integers.
i did b and c part like this but i can't a
there is my code:
#include <stdio.h>
void main() {
int n, i, sum = 0;
double avarage = 0;
int min = 0;
i = 1;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
++i;
}
printf("Sum = %d \n", sum);
avarage = sum / 10;
printf("Avg = %.2lf \n", avarage);
printf("Min = %d \n", min);
}
this is output of my code:
How can i print Minimum of those.
you min variable starts with 0, so every number you entered is larger then that.
int min = INT_MAX;
start min with the largest possible integer will guarantee every number you take as input will be smaller
another approach is the use a flag (like boolean) for the first input, and if so directly put it into min:
int min = 0;
i = 1;
int my_flag=0;
while (i <= 10) {
printf("Enter an integer: ");
scanf ("%d", &n);
sum += n;
if (n < min) {
min = n;
}
if(my_flag==0){
min=n;
my_flag=1;
}
++i;
}
Can you go through all the steps as why is the answer is 9, 0 and 3.
This prints 9. How?
int sum,i, j, g[3][3] = {{0,0,0},{1,1,1},{2,2,2}};
sum = 0;
for (i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
sum=sum+g[i][j];
}
printf("The value is:%d", sum);
This prints 0. How?
sum = 1;
for (i=0;i<=2;i++)
{
for(j=0;j<=1;j++)
sum=sum*g[i][j];
}
printf("The value is:%d", sum);`
This prints 3. How?
sum = 0;
for (i=0;i<=2;i++)
sum=sum+g[i][1];
printf("The value is:%d", sum);
To see how it works try something like:
int sum,i, j, g[3][3] = {{0,0,0},{1,1,1},{2,2,2}};
sum = 0;
for (i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum=sum+g[i][j];
printf("i=%d, j=%d, sum=%d", i, j, sum);
}
}
printf("The value is:%d", sum);
Try this...
int sum,i, j, g[3][3] = {{0,0,0},{1,1,1},{2,2,2}};
sum = 0;
for (i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum=sum+g[i][j];
printf("i=%d, j=%d, g[%d][%d]=%d, sum=%d", i, j, i, j, g[i][j], sum);
}
}
printf("The value is:%d", sum);