int inequality as while condition not working in C [closed] - c

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
Im creating a program that asks the user for an integer and checks if it is between two values and and if not, asks the user to try again. Im currently having trouble with the condition for the while loop. I want the program to check if the input number from the user is between 28 and 31 but when i put the inequality in, it acts as if its completely skipping the while loop. statement on line 8
Thanks in advance for the help everyone, I've just started learning and want to learn as much as possible. here is the code in text form instead of image form:
printf("Enter days in the month (between 28 & 31): ");
int d = get_int();
while ( d<28 && d>31 )
{
printf("error");
}
printf("how many pennies on the first day? ");
float p = get_float();

You are having a wrong logical statement in your while loop condition.
It should be d<28 || d>31. You are falsely using the and operator instead of or operator.

I think what you are to do with the while loop is not what the code is actually doing. Based on your description, you want to keep prompting the user to enter number of days until they enter the correct value. However, what your code is currently doing is asking once and then throwing error within the while loop. Perhaps try something like this
int d;
do {
printf("Enter days in the month (between 28 & 31): ");
d = get_int();
}
while ( d<28 || d>31 );
First we declare the variable d.
Then we start a do while loop that will keep running the code within the loop (asking the user to input days in month). If user enters a value that is less than 28 OR greater than 31, the while loop resets and asks the user to enter the value again. If the user enters a valid value, the loop exists and moves to next question.

Related

I'm a beginner and I'm creating a C program to print numbers from 0 to n using while loop where n is input from user [closed]

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 11 months ago.
Improve this question
I'm a beginner and I'm creating a C program to print numbers from 0 to n using while loop where n is input from user.
//program to print numbers 0 to n where n is input from user
#include<stdio.h>
int main()
{
int i=0,num;
printf("Enter number: ");
scanf("%d",&num);
while(i<=num)
{
printf('%d',i);
i++;
}
return 0;
}
Im getting error saying expected const char
I tried to get solution over several websites
since im new to this language I'm facing trouble in such simple code
I tried running this code on several online compilers but everywhere I get the same issue
In line 11 in the printf statement you have used single quotes - '%d' which does is giving you problems here, change it to a "%d". Hope that helps.

What do I need to do to get the correct value o PI? [closed]

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 1 year ago.
Improve this question
Greeting.
I am doing the following exercise: Run a program to determine an approximate value of π using the series shown below. The calculation is performed by generating a certain number of terms in the series. The number of terms to be considered is read from the standard input (keyboard) (greater than or equal to 30000).
Note: In the resolution of this issue, you cannot use functions from the math.h library of the C programming language.
example: input a value enter total terms >=30000: entering 30000 should give you the result o pi:3.141559
The prolem I'm having: Uppon entering the same value(30000)I am not getting the corret value o pi=3.14.... but instead it's something like:0.0067...
heres my code:
#include<stdio.h>
#include<math.h>
int main(void){
double numerator, denominator, pi=0.0;
int k;
printf("input a total number o terms >=30000:");
for ( k=1;k<=30000;k++){
scanf("%d",&k);
if(k>=30000){
if(k%2==0){
numerator=1;
}
else {
numerator=-1;
}
}
denominator=2.0*k+1.0;
pi+=numerator/denominator;
pi=4*pi;
printf("value of PI is= %lf",pi);
}
return 0;
}
Can someone point out what I am doing wrong and how can I solve it pls?
Your time and attention are deeply appreciated.
Thank You.
There are many problems with what your implementation of the algorithm:
Try avoiding scanf ad printf inside the for loop.
Instead of getting the k variable from the user try and get the maximum value of k.
denominator=2*k+1 is wrong if you follow the algorithm that you gave in your question and should be changed to denominator=2*k-1.
You repeat pi*=4 every iteration.
I applied all those improvement and i get pi=3.141926 for just 3000 iterations.
Here a little help on how your for loop should look like:
for (int k=1;k<iterations;k++){
numerator*=-1;
denominator=2.0*k-1.0;
pi+=numerator/denominator;
}

C programming - Finding the necessary number in the array [closed]

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
The task is to calculate how many times a certain digit occurs in the entered sequence of numbers. The number of numbers to be entered and the number to be calculated are set by typing. Ask me if you have got question about code. The problem in finding a match with the number entered in the array.Can you give me hints or instructions, also i think about loop while but i don't know how to realize it please
The code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, b, n, c=0, arr[30];
printf("The count of numbers: ");
scanf("%d", &n);
printf("The number what is finding: ");
scanf("%d", &b);
for (i = 0; i < n; ++i)
{
scanf("%d", &arr[i]);
}
for(i=0;i < n;i++)
{
if(arr[i]=b)
{
c++;
printf("%d", c);
}
}
}
You should be compiling your code with at least some basic compilation flags. If you do, you will get a heads up that something is wrong before having to run it to find out. It saves a lot of time in the long run. Consult your compiler's documentation.
For instance, it would point out that your if condition is using an assignment (=) instead of an equality comparison (==). It should be:
if (arr[i] == b)
Also, you probably want to print out the total count at the end of the program - after the loop is finished. So move the printf("%d\n", c); after the loop. (You were also missing a newline which you probably wanted).
Also, scanf has a return value - you should check it. If the user enters invalid integers, you want to catch that and handle it properly.
Finally, since you declare your array to be of size 30, you should add a check that the desired length of the input array is no longer than that -- otherwise, you would get a buffer overflow.
Side note: please use more descriptive variable names. Not doing so often leads to confusion, especially for beginners. A small exception to this is for loop counters, like i in this case -- its perfectly fine to use a single letter. But consider b -- there is no obvious meaning; it should be something like target or to_find. Also, c could be count or total. As for n, perhaps size or length would be more suited.
if(arr[i]=b) it's wrong. x = y is an assignment but you want to do a check. To check if two elements are equal you should write if(arr[i] == b).

Using Do While in C [Find Mistake] [closed]

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 7 years ago.
Improve this question
I have a relatively simple program that asks for user input between a range and then checks it using Do - while loop.
int n;
do
{
print ("hello, world\n");
n = getvalue() //just making this syntax up.It takes value from user
}
while (n<0 && n>99);
print ("%d\n",n);
I want to prompt the user to input another value if he/she enters either a negative number or a triple digit number (or higher). But the condition within while is not being validated.
What the output looks like:
No matter what number I enter, it get printed. i.e. -2 is printed as -2, 101 as 101 and 50 as 50. Ideally, -2 should prompt user to enter a number again, so should 101 and only 50 should print out.
I think the problem is your rather cryptic condition of n < 0 && n > 99, which can't reasonably be satisfied.
You need to look more closely at the logic of your loop. If you want to prompt the user again if they enter a number less than zero or greater than 99, you need to use the logical or operator ||.
while (n < 0 || n > 99);

Printf don't show the variable, but a random number [closed]

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 7 years ago.
Improve this question
I am starting to learn C. Today I was trying a little program that just do a average point starting from 3 input.
After all I wanted to print the number of the averages done in the session, so I insert a simple
counter=counter+1;
into the main while loop and a
printf("you done the average %d times", counter);
before the return 0.
The problem is: if I do the average for just 1 or 2 times, the counter show
every time a different number, never the right, but ever around the int maximum. I tried everything, but it don't work. Where is my mistakes?
This is my first post on this site, i read the rules but i'm sorry if i'm breaking just one. The variable "counter" is declared.
int main()
{
int vote1, vote2, vote3, tot, media, contatore, err;
char opz;
do{
after this, i start an while loop, and this is its end:
contatore=contatore+1;
} while(opz!='n');
printf("hai eseguito la media %d volte", contatore);
return 0;
obviously the code is in italian, where counter=contatore
You have to initialise the variable:
int contatore = 0;

Resources