How to calculate long long / int [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 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).

Related

why n mod 10 in loop doesn't show output [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 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 );

Calculating n! and x^n and proceeding the calculation [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 3 years ago.
Improve this question
Write a C program to calculate (x^n)/(n!) where x is a floating point number and n is an integer greater than or equal to zero.
I coded the following:
#include<stdio.h>
#include<math.h>
void main()
{
float x,p;
int i,n,f=1;
printf("Enter the value of x,n\n");
scanf("%d %d",&x,&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
f=f*i;
}
p=(float)pow(x,n)/f;
printf("The value of p is %.3f",p);
}
if(n==0)
{
p=(float)pow(x,n)/1;
printf("The value of p is %d",p);
}
getch();
}
But this is not running well. Where have I gone wrong?
PS: Edit
In your question I have recognize 3 problems.
main problem is scanf("%d %d",&x,&n); should be change into scanf("%f %d",&x,&n);
because x is `float type #dragosht has mentioned it.
printf("The value of p is %d",p); should be correct as printf("The value of p is %f",p); beacause p is also float type.
It is better to set p = 0; at the beginning because you did not assign value to p using keyboard. There for some times you will get corrupted values because of this.

scanf() is not taking more than 6 times in a for loop [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 5 years ago.
Improve this question
It is really very unexpected, I have looked a lot of time where can be the problem but couldn't find it.
Problem is:
Say I've given 7 integers to calculate their average, but it is taking first 6 numbers and calculating their average.
However I give any number more than 6, it will calculate only first 6's
The code is:
#include <stdio.h>
int main()
{
int n,i,total = 0;
int numArr[n];
printf("How many numbers do you want to print? => ");
scanf("%d",&n);
for(i = 0; i < n; i++)
{
scanf("%d",&numArr[i]);
total += numArr[i];
}
printf("--------------------------------\n");
printf("Average of this %d numbers: %d",n,total/n);
}
I'm not sure whether it is compiler problem or problem of my code.
initialize n first and then declare numArr[n]
Modify your code as
int n,i,total = 0;
printf("How many numbers do you want to print? => ");
scanf("%d",&n);
int numArr[n];
then scan the array.

Code running forever in C — what did I do wrong? [closed]

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++.

Program working fine for smaller values but throwing Segmentation Fault for bigger values [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 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.

Resources