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.
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 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 1 year ago.
Improve this question
Hi I was study for computer and we got this assignment to do break statement in c
this is my code
#include<stdio.h>
int main()
{
float num, sum=0;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d", &n);
for(i=1; i<=n; ++i);
{
printf("Enter number %d: ", i);
scanf("%f", &num);
if(num<0.0)
break; //for loop breaks if num < 0.0
sum = sum + sum;
}
printf("Total addition: %.2f ", sum);
return 0;
}
I already copy like my lecture instruct but I got the error break statement is not within loop
or switch. I don't know were wrong so I hope you can tell the error and some explain for me thank you
You have a stray ; after your for loop:
// here ------v
for(i=1; i<=n; ++i);
{
This means you actually have a for loop with an empty body followed by a standalone block statement, so the break is not inside of a loop.
Get rid of the extra ; so that the block statement is the body of the loop.
for(i=1; i<=n; ++i)
{
On the line with for(i=1; i<=n; ++i); you have an added semicolon that prevents the contents of the for loop from being executed. The corrected code would be as follows:
#include<stdio.h>
int main()
{
float num, sum=0;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d", &n);
for(i=1; i<=n; ++i)
{
printf("Enter number %d: ", i);
scanf("%f", &num);
if(num<0.0)
break; //for loop breaks if num < 0.0
sum = sum + sum;
}
printf("Total addition: %.2f ", sum);
return 0;
}
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 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) {
}
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
OK, I edited my program ***I am sorry if this is not formatted correctly (I don't know how to do that to make it easier to read for everyone :( I went through and fixed everything I could, but its still giving me errors.
#include <stdio.h>
#include <stdlib.h>
#define Max 1
#define Min 0
int getValidInteger(char promptString[], char errorString[], int lowerBound,
int upperBound);
float averageIntegerArray(int height[], int count);
float printIntegerArray(char heading[], int values[], int count);
int findExtremeInIntegerArray(int minOrMax, int values[], int count);
//Get user input
int main(int argc, char** argv) {
//Get the number of scores
int n = getValidInteger("Welcome to the Statisticamator! \n"
"How many heights would you like to enter (1-100)?",
"Number of scores must be 1 or higher\n", 1, 100);
printf("N is %d\n", n);
//Read in information from the user
int height[n];
for (int i = 0; i < n; i++) {
height[i] = getValidInteger("Enter height (0 to 100):",
"Height must be between 0 and 100", 0, 100);
}
//Prints average heights
printf("\n");
printf("Average Height: %.2f inches\n", averageIntegerArray(height, n));
//prints minimum and maximum
int min = findExtremeInIntegerArray(Min, values, n); //ISSUE HERE TOO
printf("Minimum value is: %d\n", min);
int max = findExtremeInIntegerArray(Max, values, n);
printf("Maximum value is: %d\n", max);
return (EXIT_SUCCESS);
}
//Average height in array
float averageIntegerArray(int height[], int count) {
int sum = 0;
for (int i = 0; i < count; i++) {
sum += height[i];
}
return (float) sum / count;
}
//Get integer input from user
int getValidInteger(char promptString[], char errorString[], int lowerBound,
int upperBound) {
int input;
//Prompt user for input
printf(promptString);
scanf("%d", &input);
//Validate input
while (input < lowerBound || input > upperBound) {
printf("errorString");
printf("\n");
printf("promptString");
scanf("%d", &input);
}
return input;
}
//Min and Max values
int findExtremeInIntegerArray(int minOrMax, int values[], int count);
int Extreme = values[0];
for (int i = 0; i < count; i++) //THIS IS WHERE IM HAVING 1 ISSUE
{
if ((values[i] < Extreme && minOrMax == Min) || (values[i] > Extreme && `enter code here`minOrMax == Max)) {
Extreme = values[i];
}
}
return (Extreme);
}
You should add a parameter after the " in the following two statements.
printf("Your minimum number is: %d inches \n");
printf("Your maximum number is: %d inches \n");
If you wanted to print 3 variables you would have three format specifiers.
printf("Your maximum number is: %d inches %d feet %d miles \n", inches, feet, miles);
The format specifier is a place holder. What the warning is telling you is that you have not given it a number to format in that place. %d is hanging out by itself.