Sum of series in c language [closed] - c

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
Sum of series using c language
sample input : 12 15
sample output : 54
sample input : 1 100
sample output : 5050
#include <stdio.h>
int main(){
int a, b;
scanf("%d %d", &a, &b);
printf("%lld",a*(b+1)/2);
return 0;
}

Your formula: a*(b+1)/2 is wrong
For a + (a+1) + (a+2) + ... + (b-1) + b use the formula:
((b+1)*b - a*(a-1))/2 or (b-a+1)(b+a)/2
(assuming that b >= a)
So the program could be:
#include <stdio.h>
int main(void)
{
int a, b;
if (scanf("%d %d", &a, &b) != 2) exit(1);
if (b < a) exit(1);
printf("%d", ((b+1)*b - a*(a-1))/2);
return 0;
}

The %lld format specifier is used to print a long long int. The argument you're passing has type int. Mismatching format specifiers triggers undefined behavior, which in this case will likely result in output you don't expect.
To print an int use %d.
printf("%d",a*(b+1)/2);

Related

convert string to double , the answer is correct ,but not very accurate [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 2 years ago.
Improve this question
I want to use atof to convert my string to a double,the answer is correct ,but not very accurate
ATTENTION: because of some other reasons, fscanf is not permitted
my code is :
#include <stdio.h>
#include <stdlib.h>
#define MAXCN 50
int main(void)
{ FILE* lstm_txt = NULL;
char lstm_weight[MAXCN] = {0};
int lstm = 0;
int i = 0;
float lstm_val;
if ((lstm_txt = fopen("test1.txt", "r"))== NULL){
fprintf(stderr,"error:file open failed 'test1.txt'.\n");
return 1;
}
while ((i + 1 < MAXCN) && ((lstm = fgetc(lstm_txt)) != ' ' ) && (lstm != EOF)){
lstm_weight[i++] = lstm;
}
//lstm_weight[i] = 0;
printf("\n lstm_weight: %s\n\n", lstm_weight);
lstm_val = atof(lstm_weight);
printf("\n convert \"lstm_weight\" to lstm_val is : %f\n\n", lstm_val);
return 0;
}
my file : lstm_txt is :
4.217959344387054443e-01 -2.566376626491546631e-01 2.173236161470413208e-01 4.217959344387054443e-01
code hasn't bug, and the result is :
lstm_weight: 4.217959344387054443e-01
convert "lstm_weight" to lstm_val is : 0.421796
but I want Istm_val is 0.4217959344387054443 ,how can I do that?
you could try something like sprintf()
here's an example:
#include <stdio.h>
int main() {
char str[50];
double n = 0.3984092590879;
sprintf(str, "%lf", n);
printf(str);
return 0;
}
prints out:
0.3984092590879
Printing %.17f you can have a precision up to 0.42179593443870544
printf("\n convert \"lstm_weight\" to lstm_val is : %.17f\n\n", lstm_val);
A double typically has about 15 digits of decimal precision. You will not get more accuracy than that if you store the value in a double. You are getting less because you didn't tell printf how many digits of precision to use for output, so you got the default.
Use something like %0.15f instead of %f.
convert "lstm_weight" to lstm_val is : 0.421795934438705
With %0.20f, I get:
convert "lstm_weight" to lstm_val is : 0.42179593443870544434
That's the best you'll do with a double.

program is not taking in input even when I want it to [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 2 years ago.
Improve this question
#include <stdio.h>
int main(void)
{
int F = printf("Enter temperature in Fahrenheit: ");
int C = (F - 32) / 1.8;
printf("%i", C);
}
output:
Enter temperature in Fahrenheit: 0
I want to create a program which easily converts Fahrenheit to degree Celsius so the output should be
Enter temperature in Fahrenheit: (number given by user)
(degree Celsius)
The function printf is only for outputting formatted strings and returns the number of characters printed if the call is successful.
Consider using scanf
#include <stdio.h>
int main(void)
{
int F = 0;
printf("Enter temperature in Fahrenheit: ");
scanf("%d", &F);
int C = (F - 32) / 1.8;
printf("%i\n", C);
}

why this recursive programis having garbage value even after assignment [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
The values in image appear from nowhere:
What are these values and if they are garbage values then why they are still present after assigning J the value. I am also attaching the source code.
int sum(int);
void main()
{
int a, b;
int j = 0;
printf("please enter the number to find the sum\n");
scanf("%d", &a);
j = a + 1;
printf("%d\n", j);
b = sum(j);
printf("the sum is %d", b);
}
int sum(int j) {
printf("jis %d\n", j);
int f;
if (j == 0)
{
printf("if cond\n");
return f;
}
else
{
j = j - 1;
printf("f up is %d\n", f);
f = j + sum(j);
printf("f dw is %d\n", f);
return f;
}
}
In the if block of function sum, you declare int f without assigning it a value and hence it possesses a garbage value. The only time you assign it a value is after the statement printf("f up is %d\n",f);. Hence, this statement is always going to print a garbage value.
If you use an uninitialized variable, that will lead to undefined behavior . and you have used uninitialized int f in your if-elsestatement in sum function. Initialize it.
Also don't use void main use int main.

Converting a signed char to an int in c [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 4 years ago.
Improve this question
Im having an issue converting a signed string into an int.
char *crn1, *crn2, *credit1, credit2;
char course1, course2;
crn1=strtok(course1,"/");
credit1=strtok(NULL,"/");
crn2 = strtok(course2,"/");
credit2 = strtok(NULL,"/");
Im trying to convert the signed char credit1 or credit2 to an integer for math used later on in my code. I either get a huge number or an error.
Use strtol (which is the safer version of atoi).
#include <stdlib.h>
#include <stdio.h>
int main() {
char *str = "5";
int n;
n = strtol(str, NULL, 10);
printf("n+1 is %d\n", n+1);
}
Using atoi:
#include <stdlib.h>
#include <stdio.h>
int main() {
char *str = "1";
int n;
n = atoi(str);
printf("n+1 is %d\n", n+1);
}
If instead of converting the digits in the string into an integer, you'd rather use the numerical value of the character, you can:
#include <stdio.h>
int main() {
char c = 'a';
printf("ascii code of %c is %hhu\n", c, c);
printf("after %c is %c with ascii code %u", c, c+1, c+1);
return 0;
}

C programming: Write a program that reads three integers from keyboard and outputs their sum [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 7 years ago.
Improve this question
I have to write code that reads three integers from keyboard and outputs their sum. Does this mean that only integers should be entered, or should it be able to add characters as well? Here's my code:
#include <stdio.h>
int main(void) {
int a, b, c, d;
printf("\n Enter the three numbers:");
scanf("%d %d %d", &a, &b, &c);
d = a + b + c;
printf("sum of numbers is %d", d);
}
scanf("%d %d %d", &a, &b, &c); parses the input stream for 3 decimal integers optionally separated by white space (spaces, tabs, linefeeds...).
If any other characters are present (such as letters, decimal points, commas...) or if not enough input is available, scanf will return a result different than 3 and some of the output variables will not by set. Always test the return value of scanf.
Here is a corrected version:
#include <stdio.h>
int main(void) {
int a, b, c, d;
printf("\n Enter the three numbers:");
if (scanf("%d %d %d", &a, &b, &c) == 3) {
d = a + b + c;
printf("sum of numbers is %d\n", d);
}
return 0;
}

Resources