Could you tell me why this did not work?
I have to show Pi number from Basel problem, but I don't know why the program shows the same number all the time, despite the fact I choose different 'numbers'.
Thanks a lot!
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define Pi 3.14159
int main()
{
int number;
printf("max number: ");
scanf("%d",&number);
float euler;
float sum=0;
for(int i=1; i<number;i++)
{
sum=sum + (1/(i*i));
}
euler=sqrt(6*sum);
printf("Euler: %lf \n",euler);
printf("Pi from math library = %f",Pi);
return 0;
}
The problem is that the expression (1/(i*i)) is calculated using integer mathematics. In other words, the result is always an integer, and for any value of i greater than 1, the result will always be 0.
The simplest fix is to rewrite that expression as (1.0f/(i*i)). This will square i, convert the result to a float, and then do the division.
the posted code is mixing integer division with double literal and is expecting the result to be a float
The following proposed code :
cleanly compiles
performs the implemented functionality
and now, the proposed code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// define a float value rather than a double value
#define Pi 3.14159f
int main( void )
{
int number;
printf("max number: ");
scanf("%d",&number);
float euler;
float sum=0;
for(int i=1; i<number;i++)
{
// perform float math rather than integer math
sum=sum + (1.0f/(float)(i*i));
}
euler=sqrtf(6*sum); // < note float version rather than double version
printf("Euler: %lf \n",euler);
printf("Pi from math library = %f",Pi);
return 0;
}
Related
Do you know how a number can be rounded up and printed with a precision that is not fixed by some natural number, but by some variable? If the user needs to enter how many decimal places to round, how to solve it?
#include <stdio.h>
int main()
{
int r;
double var = 37.66666;
scanf("%d", &r);
printf("%.2f", var);
return 0;
}
Here's simple approach that would work in your case:
You just need to put * before f, and that's it.
#include <stdio.h>
int main()
{
int r;
double var = 37.66666;
scanf("%d", &r);
printf("%.*f",r, var);
return 0;
}
You can put a * in place of the precision, in which case you can specify an int as the precision.
printf("%.*f", r, var);
Program takes an integer input num from the keyboard and computes the sum of square of i for all I from 1 to num (inclusive).
#include <iostream>
#include <math.h>
int main()
{
int num;
int total;
printf("Please enter a number:");
scanf("%d", &num);
for (double i = 1; i <= num; i++) {
total += (i*i);
printf("%d", total);
}
}
The code above compiles correctly, but when inputting 5 it prints 15143055. Why is it doing this?
#include <iostream> is c++. #include <math> is not used. total is uninitialized. Comparing floating point values may not behave the way you want, so using a (unsigned) integer type instead as a loop counter. Loop values by convention start at 0 instead of 1 in c (you could increment i fist thing in the loop to avoid the double (i+1) but this will be optimized out anyways). Also, your loop not run for a negative value so just require unsigned values. As you sum integers the result ought to be an integer, but double would give you a larger range so I left it as such. Missing return:
#include <stdio.h>
int main() {
unsigned num;
printf("Please enter a number: ");
scanf("%u", &num);
double total = 0.0;
for (unsigned i = 0; i < num; i++) {
total += (i+1)*(i+1);
}
printf("%lf\n", total);
return 0;
}
and the resulting output:
Please enter a number: 3
14.000000
I want to print the value of sin(x) using its Taylor series.
I must add terms until the absolute value of a term becomes < 10e-06
#include <stdio.h>
#include <math.h>
double val(double, double);
int main(void){
double x,eps=10e-06;
printf("\nEnter value of x: ");
scanf("%lf",&x);
printf("\nCalculated value: %lf\n",val(x, eps));
return 0;
}
double val(double x, double eps){
int i;
double t=x, sum=0;
if(fabs(x)<= eps) return 0;
for(i=1;i<=20; i++){
sum+=t;
t*=-(x*x)/((2*i+1)*(2*i));
if(fabs(t)>=eps) break;
}
return sum;
}
But I am getting the same output as my input(that is I am getting x in output).
Can someone tell me what is wrong.
The problem is here:
if(fabs(t)>=eps) break;
You're breaking out of the loop if the term is greater than the epsilon value. You want to check if it's less:
if(fabs(t)<eps) break;
Can somebody tell me what is wrong with my code?
#include <stdio.h>
#include <math.h>
int main(void) {
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers: ");
scanf("%d%d",&n1,&n2);
for(;nSet<2;nSet++){
int sum=n1+n2;
float prod=n1*n2;
float hm=nSet/(1/n1+1/n2);
float gm=sqrt(n1+n2);
float avg=(n1+n2)/2;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
originally by initializing in for loop i got 0 as every answer but atleast that printed something. I have to use loops to find the answers and i dont see why for loop would'nt work.
In C if you declare a float and assign a value like 1/2 the result will be 0 because de expression 1/2 is evaluated. If you want to have a result as float put 1.0f/2 or 1/2.0f and will work. In case of having 2 variables and want a float you can do this: (float)n1/n2 or n1/((float)n2) if n1 and n2 are int. Another observation is in the for loop. If you declare your variables again in for they are local in the loop and outside the loop they don't exists.
These code will work:
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers. \n");
printf("n1 = ");scanf("%d",&n1);
printf("n2 = ");scanf("%d",&n2);
for(;nSet<2;nSet++)
{
sum=n1+n2;
prod=n1*n2;
hm=nSet/(1.0f/n1+1.0f/n2);
gm=sqrt(n1+n2);
avg=(n1+n2)/2.0f;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
You are declaring new var in for scope. When the program exit from the loop all of the var created in for scope will be destroyed.
Your code corrected :
#include <stdio.h>
#include <math.h>
int main(void) {
int nSet=0;
int n1,n2;
int sum;
float hm,gm,avg,prod;
printf("Enter two integers: ");
scanf("%d%d",&n1,&n2);
for(;nSet<2;nSet++){
sum=n1+n2;
prod=n1*n2;
hm=nSet/(1/n1+1/n2);
gm=sqrt(n1+n2);
avg=(n1+n2)/2;
}
printf("Sum: %d\n",sum);
printf("Product: %4.2f\n",prod);
printf("Average: %4.2f\n",avg);
printf("Geometric mean: %4.2f\n",gm);
printf("Harmonic mean: %4.2f\n",hm);
return 0;
}
It's work much better now :)
Enter two integers: 10
1
Sum: 11
Product: 10.00
Average: 5.00
Geometric mean: 3.32
Harmonic mean: 1.00
Problem Statement
You are given an array of integers of size . You need to print the sum of the elements of the array.
Note: A signed 32-bit integer value uses bit to represent the sign of the number and remaining 31 bits to represent the magnitude. The range of the 32-bit integer is . When we add several integer values, the resulting sum might exceed this range. You might need to uselong long int in C/C++ or long data type in Java to store such sums.
Input Format
The first line of the input consists of an integer. The next lines contain space separated integers describing the array.
Constraints
Output Format
Output a single value equal to the sum of the elements of the array.
Sample Input
5
1000000001 1000000002 1000000003 1000000004 1000000005`
Sample Output
5000000015
My program
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
long int a[5],sum;
for(i=0;i<=n-1;i++){
scanf("%ld %ld %ld %ld %ld",&a[1],&a[2],&a[3],&a[4],&a[5]);
}
for(i=0;i<=n-1;i++){
sum = sum + a[i];
}
printf("%ld",sum);
return 0;
}
Error description
Input (stdin):
5
1000000001 1000000002 1000000003 1000000004 1000000005
Your Output (stdout):
140692151765426
Expected Output:
5000000015
Compiler Message:
Wrong Answer
This works fine :
#include <inttypes.h>
int main()
{
int n,i;
scanf("%d",&n);
unsigned long long int a[5];
unsigned long long int sum=0;
for(i=0;i<n;i++)
{
scanf("%llu",&a[i]);
}
for(i=0;i<n;i++)
{
printf("%llu\n",sum);
sum = sum + a[i];
}
printf("\nSum is : %llu",sum);
return 0;
}
Use the ll long-long modifier with the u (unsigned) conversion
You don't basically need an array here. Just
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
long long int a, sum = 0;
while (n--) {
scanf("%lld", &a);
sum += a;
}
printf("%lld", sum);
return 0;
}
The program has following issues:
Array indexing is wrong : Array is of size 5 starting from 0 , you can use only till a[4] , but for scanf() , it tries to read value as &a[5].
No use of for loop when you are using hard coded index a[1],a[2],etc. Instead the below code will be better to get input as follows:
for (i = 0; i < n; i++ ) {
scanf("%ld",&a[i]);
}
Sum includes : sum = sum + a[i];
when i = 0 --> a[0] will have have junk values because input was not taken from user as scanf started from a[1] . Since array is not initialized this is uninitialized auto variable, it might have junk values which will get added in the sum .
sum itself is also not initialized, so it will contain junk value and for very first addition : sum = sum + a[0]; thsi junk value will get added .
Hope this answer your query for unexpected output.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
int n,i;
cin >> n;
long long int s=0;
long int a[10];
if(n>=1 && n<=10)
{
for(i=0;i<n;i++)
{
cin>>a[i];
s=s+a[i];
}
}
cout<<s;
return 0;
}
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,i;
scanf("%d",&n);
long int a[10];
long long sum=0;
for(i=0;i<=n-1;i++){
scanf("%ld",&a[i]);
}
for(i=0;i<=n-1;i++){
sum = sum + a[i];
}
printf("%ld",sum);
return 0;
}