Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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 want to know why when running this code I get an infinite output. Here is the code:
#include <stdio.h>
int main(){
int num;
printf(" enter a number\n");
scanf(" %d", &num);
for( num = 0 ; num <= 10 ; num+=num){
printf(" %d",num);
}
return 0;
}
num+= num never incrementing the num. It is always adding 0 to 0. Also num = 0 in for loop overriding user input for num.
num+=num always adds 0 to 0(num) and hence the value of num never increments. Thus num always less than 10 and loop never exits.
while num++ would lead to num > 0, num += num with initial num = 0 what you get is num += 0, and the loop never reaches 10. Just use num++
int num;
printf(" enter a number\n");
scanf(" %d", &num);
for( num = 0 ; num <= 10 ; num++){
printf(" %d",num);
}
num+=num meaning is num=num+num and num start from 0 so num=0 always so go in infinite loop.change it num++.
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 2 months ago.
Improve this question
for (int i = 1; i < 22; i++){
if(i<=99 && i>=0){
printf("enter an age");
scanf("%d", &ages[i]);
}
else{
printf("enter a valid number");
}
}
When I enter a number outside the if statement such as 999, my program still accepts it instead of printing enter a valid number message. thanks in advance.
When I enter a number outside the if statement such as 999, my program still accepts it instead of printing enter a valid number message. thanks in advance.
I think as the 'i' is the index of the 'ages' array, you are allowing values with a range of 0-99. So, if the value is out oof this range also, it will still be stored in the 'ages' array. It won't display the warning message (enter a valid number) also. Instead, you can try something like this:
for (int i = 0; i < 22; i++){
printf("enter an age: ");
scanf("%d", &ages[i]);
if(ages[i] > 99 || ages[i] < 0){
printf("enter a valid number\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 1 year ago.
Improve this question
I have solve a basic problem in c that is count the digits in integer and I have written -
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int i;
while(n!=0)
{
n %= 10;
++i;
}
printf("%d",i);
}
I already know that above code is wrong, I should write n/=10; instead of n%=10; but I wants to know why it is not printing even value of i i.e 0.
If I have written any wrong so please ignore it ,I am new here..
If the number n is not divisible by 10 then the value of this expression (the remainder of the division)
n %= 10;
will never be equal to 0.
Also the variable i is not initialized.
int i;
You should write
int i = 0;
do
{
++i;
} while ( n /= 10 );
printf( "%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 2 years ago.
Improve this question
I wrote a simple for loop in C to find if entered number is a prime number. Upon running, even when entering simple values like 7, 13, etc. the programme just sits on it and seems to be processing something huge. I have an i53340M so processing power really isnt an issue. CPU usage shoots up to 25% on all cores and I see no result even after few minutes of waiting. Of course, modern processors are not slow and C is very fast, much faster than Python, which itself is cabale of checking for prime very swiftly.
It seems to me that I have done something stupid and left the code unoptimised or bloated. Please take a look and tell me where I went wrong :
int num,i,chk = 0 ;
printf("\nEnter positive integer to check : ");
scanf("%d", &num);
for (i = 2; i = num / 2; i++)
{
if (num%i == 0)
{
break;
chk = 1;
}
}
if(num == 1)
printf("\n\n1 is neither prime not composite.\n");
else if(chk == 1)
printf("\n\nThe number %d is indeed prime.\n", num);
else
("\n\nThe number %d is not actually prime.\n", num);
i = num / 2 assigns num / 2 to i and evaluates as “true” to continue the for loop. (when n is greater than 1). You may have intended i <= num / 2.
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 4 years ago.
Improve this question
I am trying to learn how to code by myself and is experiencing some difficulty in calculations. Can someone please explain why the pf always return 0 in the below?
int main(void)
{
//solicit input from users
long long int num = get_long_long("Credit card no: ");
eprintf("%lld\n",num);
//initialize
int i =0;
int j =0;
int counter =0;
string status;
//find length of input
while (num>0)
{
num/= 10;
counter++;
}
printf("counter is %i\n",counter);
//Identify card type by prefix
int power=(counter-2);
eprintf("power is %i\n", power);
int dp = pow(10,power);
eprintf("divofp is %i\n", dp);
//prefix=num
long long int pf=(num/dp);
eprintf("pf is %lld\n",pf);
}
pf will always be zero, because num is set to zero at the end of your while loop.
Therefore num/anything will always equal zero.
A good method of debugging, is to step through the code line by line, and look at the values of your variables at each point in time.
This can help you narrow down problems like this.
The problem is where you get the length of your number:
while (num>0)
num/=10;
num will always be 0 after this and thus your final expression will result in 0 because 0/x = 0 (x != 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 5 years ago.
Improve this question
I want to enter value to my array and between 0 and 101 and trying to put zero or 101 will return an error but putting anything from 1-100 will allow me to enter a second number.
but i am stuck when write code to add a second number the loop keeps going in 1 and i cant figure out my error. any help would be apreciated
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
char letter;
int main()
{
int i;
int grades[12] ;
for ( i = 0; i < 12; i++)
do{//loops the following peice of code if values over 100 or less than 0 are entered
printf("Please Enter Grade 1:\n");
scanf("%f", & grades[i]);
if (grades > 100 || grades < 0)
printf("Invalid Entry please enter another between 0 and 100:\n");
}while(grades < 100 || grades > 0);
}
The mistake here is you're comparing the whole array to a value. What you want is one element, plus you need to test that you're outside of the acceptable bounds:
while(grades[i] > 100 || grades[i] < 0);
As Jonathan points out below, the logic in your code tests that it's within, which means only valid answers will loop forever, the opposite of what you wanted.