Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Im trying to write a code that computes Fibonacci with a few minor changes.
there's a limit to the number of iterations, there is a goal number and every i iterations the number is divided by 2.
somehow my code does not work properly.
it only works for lim = 1
in all other cases it does not do anything
can anyone see the bug?
int main()
{
int i, lim, gen_num,
fib1, fib2, next, count = 0, iterations = 0;
printf("Please enter the desired population\n");
scanf("%d", &lim);
printf("Please enter the number of generations alowd\n");
scanf("%d", &i);
printf("Please enter the number of generations between each disaster\n");
scanf("%d", &gen_num);
fib1 = 0;
fib2 = 1;
while (fib2 < lim);
{
if (iterations <= i)
{
next = fib1 + fib2;
fib1 = fib2;
fib2 = next;
count += 1;
iterations += 1;
if (count%gen_num == 0)
{
fib2 /= 2;
}
printf("%d ", fib2);
}
else
{
printf("Number of iterations exceeded\n");
return 0;
}
}
return 0;
}
This line:
while (fib2 < lim);
is an infinite loop when fib2 is less than lim. The semicolon at the end means that you have an empty statement as the body of the loop, it's equivalent to:
while (fib < lim) {
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I have just started learning programming in c. I have written a program that has an arithmetic sequence based on the number of arithmetic sequence sentences n.
The program will not work after 17 without giving me an error
I have tried several different ideas but I have not received an answer
Thank you for your help.
#include <stdio.h>
int main()
{
int n ,k;
float d ;
printf("please write your arithmetic sequence sentences ");
scanf("%d",&n);
printf("\n");
printf("please write your common differences");
scanf("%d",&k);
printf("\n");
printf("please write your initial element ");
scanf("%f",&d);
printf("error 1");
printf("\n");
printf("error 2");
printf("number \t sum");
printf("erorr 3");
int i = 0;
int j = 0;
int sum = 0;
while (i < n)
j = d + i*k;
sum += j;
printf("%d\t%d",j,sum);
i++;
return 0;
}
it is not python. You need to use {} to declare compound statement
while (i < n)
{
j = d + i*k;
sum += j;
printf("%d\t%d\n",j,sum);
i++;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed last year.
Improve this question
Why does my program stop here? Is it creating an infinite loop or something? It doesn't print the highest grade and lowest grade and the general weighted average. It only shows it when I type 'exit.' I use Dev-C++.
int grades[5], i, lGrade, hGrade;
float sum = 0, GWA;
printf("Enter 5 grades in percentile format within 0-100.");
for (i = 0; i < 5; i++) {
if (i == 0) {
printf("\n");
}
printf("\tGrade No. %d => ", i + 1);
scanf("%d", &grades[i]);
if ((grades[i] < 0) || (grades[i] > 100)) {
printf("\n\tOnly enter grades from 0 to 100. Please try again.\n\n");
i -= 1;
} else {
continue;
}
}
for (i = 0; i < 5; i++) {
scanf("%d", &grades[i]);
sum += grades[i];
}
GWA = sum / 5;
lGrade = grades[0];
hGrade = grades[0];
for (i = 0; i < 5; i++) {
if (grades[i] < lGrade)
lGrade = grades[i];
else if (grades[i] > hGrade)
hGrade = grades[i];
}
printf("\nThe highest grade is %d.", hGrade);
printf("\nThe lowest grade is %d.", lGrade);
printf("\nThe general weighted average is %0.2f.", GWA);
Your code asks for the grades twice, it's halting at the scanf here:
for (i = 0; i < 5; i++) {
scanf("%d", &grades[i]);
sum += grades[i];
}
Perhaps you meant printf() not scanf().
As already said, the program stops in the second scanf; also I'd like to add that if you just want to print the higher, lower and average values, the second for loop would be unnecesary; and the
sum += grades[i];
should be inside the else {} clause.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Im trying to find the sum of n numbers using a while loop so that it runs like so:
How many numbers: 3
-3,
4,
13,
The sum is: 14
However what I am getting is this:
How many numbers: 3
2,
1,
The sum is: 3
I dont understand it, because i set i = 0
#include <stdio.h>
int main(void) {
int numbers;
printf("How many numbers: ");
scanf("%d", &numbers);
int sum = 0;
int i = 0;
while (i < numbers) {
scanf("%d", &numbers);
sum = sum + numbers;
i++;
}
printf("The sum is: %d", sum);
return 0;
}
A correct solution would be:
#include <stdio.h>
int main(void) {
int numbers;
printf("How many numbers: ");
scanf("%d", &numbers);
int sum = 0;
int i = 0;
int number; // use different variable for the input numbers
while (i < numbers) {
scanf("%d", &number);
sum = sum + number;
i++;
}
printf("The sum is: %d", sum);
return 0;
}
The problem was that you were using one variable for two different things.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am trying to figure out how to get the prime number n, and calculate the sum of cubes for that n (1^3 + 2^3 + ... + n^3). So far I can figure out how to get the primes. I just can't figure out how to get that same n to calculate its primes. This is what I have so far:
#include <stdio.h>
int main() {
int n, i, c = 0
printf("Enter any number n: ");
scanf("%d", &n);
for(i=2; i<=n/2; i++){
if(n%i == 0){
c=1;
break;
}
}
if (c==0)
printf(%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
printf("Enter that same number n: ");
scanf("%d", &num);
int num, cube, sum = 0, j=1;
while (j <= num) {
cube = j*j*j;
sum = sum + cube;
j++;
}
printf("sum of cubes of %d is %d\n", num, sum);
return 0:
}
I get an error on the second scanf because it says num is undeclared. What should I do to fix this situation.
Use scanf after declaring int num. Also you have used % in 2nd num. Use
int num, cube, sum = 0, j=1;
scanf("%d", &num);
instead of
scanf("%d", %num);
int num, cube, sum = 0, j=1;
Consider this starting piece
long double sumcubs(int n)
{
int i;
long double ret = 0;
for (i = 1; i <= n; ++i)
ret += (i * i * i);
return ret;
}
Once you have encapsulated this sum of cubes function, it should become easier to organize and write the rest of your code.
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 6 years ago.
Improve this question
Here is the code. Very basic and should work properly:
int main() {
int n, status, i;
printf("Please enter an integer bigger than 1: ");
status = scanf("%d", &n);
if (status != 1 || n > 1) {
printf("Invalid input!");
return 1;
}
for (i = 2; i <= n; i++) {
printf("What is going on %d\n", i);
}
return 0;
}
The program compiles good, and after I insert "10" as input, the program does nothing. Just as if it was supposed to return 0 without doing nothing.
EDIT: The main lesson here is don't post questions when it's late and you're super tired. The accepted answer (and handful of friendly comments) show why
Do you not see the problem here?
if (status != 1 || n > 1) { // n can not be larger than 1.
printf("Invalid input!");
return 1;
}
for (i = 2; i <= n; i++) { // n must be larger than 2.
printf("What is going on %d\n", i);
}