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 );
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 years ago.
Improve this question
#include<stdio.h>
void main()
{
int i=1,s=0;
do{
s+=i;
}while(i<=10);
printf("sum is %d",s);
}
this code is not giving any output, please tell me what is wrong.
You have an infinite loop because the variable i that is used in the condition of the do-while loop is not being changed within the loop. It stays equal to 1 as it was initialized.
do{
s+=i;
}while(i<=10);
It seems you mean
do{
s += i++;
} while( i <= 10 );
or
do{
s += i;
} while( ++i <= 10 );
Pay attention to that according to the C Standard the function main without parameters shall be declared like
int main( void )
Also it is desirable to flush the output buffer by including the new line character '\n' in the output stream like
printf( "sum is %d\n", s );
you didn't increment i;
do
{
s+=i;
i++;
}while(i<=10);
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 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++.
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
In the following program when the value of N is less than 100 the program is executing perfectly but for bigger values of N its showing segmentation fault.I sit because of less memory or anything wrong with program??
#include<stdio.h>
int main()
{
int N,iteration,MAX_ITERATONS;
int i,j,k,n,index,boundary1,boundary2;
double h[2][100][100];
int current = 0;
int next = 1;
printf("Enter the number of points\n ");
scanf("%d", &N);
boundary1=0.4*N;
boundary2=(0.6*N);
printf("The boundary is between %d and %d .\n",boundary1,boundary2);
for(k=0;k<2;k++)
{
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if((i==0)&&(j>=boundary1&&j<boundary2))
h[k][i][j]=100;
else
h[k][i][j]=20;
}
}
}
printf("Initial Values\n");
index = N/10;
for(i=0;i<N;)
{
for(j=0;j<N;)
{
printf("%.2f\t",h[0][i][j]);
j=j+index;
}
i=i+index;
printf("\n");
}
}
When N > 100, h is accessed to an index greater than 100, inside the nested for loop
h[k][i][j]=100;
but h is defined as
double h[2][100][100];
You are going out of bounds for h
If you want N as greater than 100 you need to redefine h or malloc it.
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
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! The input begins with the number t of test cases in a single line (t <= 10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n - m<=100000) separated by a space.
I don't know how to solve the problem with advanced concepts so I solved it by using just loops.
The time limit to this problem is 6.00s
#include <stdio.h>
int main(void)
{
int a[1],b[1],j,i,test,k,flag;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
for(k=0;k<1;k++)
{
scanf("%d %d",&a[k],&b[k]);
}
for(j=a[0];j<=b[0];++j)
{
flag=0;
for(k=2;k<j;++k)
{
if(j%k==0)
{
flag=1;
break;
}
}
if(flag==0)
{
printf("\n%d",j);
}
}
}
return 0;
}
Couple of suggestions that will improve performance.
You don't need to check all the way to b[0]. You need to check only up to sqrt(b[0]).
Update the loop so that you check only odd numbers not all numbers.
Replace
for(j=a[0];j<=b[0];++j)
{
by
int stop = sqrt(b[0]);
// Start with an odd number and keep incrementing j by 2 to keep it that way
for(j= (a[0]/2)*2+1; j <= stop; j +=2 )
{