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
How do you test for primes with the IsPrime method below? I cannot seem to get the printf to work in my IsPrime method and no errors were thrown.
#include <stdlib.h>
#include <stdio.h>
int IsPrime(unsigned int number) {
if (number <= 1) {
return 0; // zero and one are not prime
printf("zero and one are not prime.");
}
unsigned int i;
for (i=2; i*i<=number; i++) {
if (number % i == 0) {
return 0;
printf("not a prime.");
}
}
return 1;
printf("You've found a prime!");
}
int main(void) {
int a;
printf("Please input an integer value: ");
scanf("%d", &a);
if(a >= 1 && a <= 1000) {
printf("You entered: %d\n", a);
IsPrime(a);
}
else {
printf("Error! Please enter a value between 1 and 1000.");
}
}
You're return-ing from function before printf
You wrote printf statement after return; It's an easy fix, just swap the two instructions.
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 5 days ago.
Improve this question
I am trying to make a program that detects a user input and prints stuff based on that input. The input needs to have limits set however the if statements I set up are being ignored and I'm not quite sure why
#define _CRT_SECURE_NO_DEPRECATE 1
#define _CRT_NONSTDC_NO_DEPRECATE 1
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
int main(void)
{
int num;
int exit;
exit = 0;
printf("\nLab5 p1 by Chung - En Hou\n");
num = 0;
do{
printf("\nEnter a number between 0 and 255:");
//input
scanf("%d", &num);
//ignored if statement?
if (num < 256 || num >= 0) {
Sleep(250);
printf("%d\n", num);
Sleep(250);
printf("%x\n", num);
Sleep(250);
printf("%c\n", num);
printf("Press any button to Run the program again or press Esc to exit");
exit = getch();
}
//else also ignored
else {
printf("\nthat number is turbo cringe please try again\n");
printf("\nPress any button to Run the program again or press Esc to exit\n");
exit = getch();
}
} while (exit != 27);
return 0;
}
I think you mean
if (num < 256 && num >= 0) {
instead of
if (num < 256 || num >= 0) {
That is the if statement checks whether the value of num is in the range [0, 255]
As for the expression in the if statement shown in the question then it will evaluate to true for any integer value of the variable num.
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
#include<stdio.h>
int main()
int x=45,y=46,z=65;
if(x>y && x>z)
{
printf (" i presume %d is greater \n",x);
}
else if(y>z)
{
printf ("actually %d is greater\n",y);
}
else
{
printf ("NO,%d is Greater\n",z);
}
return 3;
You forgot two braces
#include<stdio.h>
int main() {
int x=45,y=46,z=65;
if(x>y && x>z)
{
printf (" i presume %d is greater \n",x);
}
else if(y>z)
{
printf ("actually %d is greater\n",y);
}
else
{
printf ("NO,%d is Greater\n",z);
}
return 3;
}
Because you forgot the braces. It thinks you're trying to create a function in the line that's getting an error. Here's the fixed code:
#include <stdio.h>
int main() {
int x = 45, y = 46, z = 65;
if(x>y && x>z)
{
printf (" i presume %d is greater \n",x);
}
else if(y>z)
{
printf ("actually %d is greater\n",y);
}
else
{
printf ("NO,%d is Greater\n",z);
}
return 3;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 3 years ago.
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.
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.
Improve this question
I am running a hollow box statement and cant figure out the error message
error: expected identifier or ‘(’ before ‘{’ token" {
I have tried multiple variations of the bracket in different positions and it just causes more errors. Here is the updated code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main ()
{
int i;
printf("Even numbers between 25 to 75: \n");
for (i = 25; i<=75; i++)
{
if(i%2 == 0)
{
printf("%d\t", i);
}
}
printf("All odd numbers between 500 to 400: \n");
for (i = 500; i>=400; i--)
{
if(i%2 == 0)
{
printf("%d\t", i-1);
}
}
int number, result, exponent;
result = 1;
printf("Enterthe base number: ");
scanf("%d", &number);
printf("Enter the exponent: ");
scanf("%d", &exponent);
while (exponent != 0)
{
result *= number;
--exponent;
}
printf("Answer = %d \n", result);
}
int f, w;
{
for (f = 1; f <= 7; f++)
{
for (w = 1; w <= 7; w++)
{
if (f==1 || f==7 || w==1 || w==7)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
This is an update to the first question since my first post didnt show the whole code and everyones answer was asking for it.
You should put an int main(void) before the body of your main function. You can then move your variables inside the function. After you've done this, the top of your code should look like:
int main(void) /* Here! */
{
int f, w; /* Move this inside the function. */
for (f = 1; f <= 7; f++)
...
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);
}
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) {
}