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.
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 8 months ago.
Improve this question
I have just started learning programming in c. I have written a program that has an arithmetic sequence based on the number of arithmetic sequence sentences n.
The program will not work after 17 without giving me an error
I have tried several different ideas but I have not received an answer
Thank you for your help.
#include <stdio.h>
int main()
{
int n ,k;
float d ;
printf("please write your arithmetic sequence sentences ");
scanf("%d",&n);
printf("\n");
printf("please write your common differences");
scanf("%d",&k);
printf("\n");
printf("please write your initial element ");
scanf("%f",&d);
printf("error 1");
printf("\n");
printf("error 2");
printf("number \t sum");
printf("erorr 3");
int i = 0;
int j = 0;
int sum = 0;
while (i < n)
j = d + i*k;
sum += j;
printf("%d\t%d",j,sum);
i++;
return 0;
}
it is not python. You need to use {} to declare compound statement
while (i < n)
{
j = d + i*k;
sum += j;
printf("%d\t%d\n",j,sum);
i++;
}
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);
Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 5 years ago.
Improve this question
Why should I write the statement "while(scanf("%d",&t)==1)" on the online judges ? Why I get WRONG verdict if i Submit without this statement ? As my IDE (Code blocks compiler) doesn't find any error .
#include<stdio.h>
int main()
{
int t,i;
float h,l,w;
while(scanf("%d",&t)==1)
{
for(i=1;i<=t;i++)
{
scanf("%f%f%f",&l,&w,&h);
if (l<=20 && w<=20 && h<=20)
printf("Case %d: good\n",i);
else
printf("Case %d: bad\n",i);
}
return 0;
}
}
"While" loop isn't necessary. Your code works perfectly fine without while loop, if you write like this:
scanf("%d", &t);
for (i = 1; i <= t; i++)
If you want multiple "t" inputs you should move your return statement after while brace:
int t, i;
float h, l, w;
while (scanf("%d", &t)) {
for (i = 1; i <= t; i++)
{
scanf("%f%f%f", &l, &w, &h);
if (l <= 20 && w <= 20 && h <= 20)
printf("Case %d: good\n", i);
else
printf("Case %d: bad\n", i);
}
}
return 0;
Also, checking if scanf returned 1 is some kind of protection. scanf returns number of elements filled (int this case 1). If you try to write non-digits it returns 0. You can check what your scanf returns with this or similar code:
printf("%d", scanf("%d", &t));
Good luck!
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 7 years ago.
Improve this question
I'm a beginner and I am having a really hard time while doing this program.
The question is:
(1/1!)+(2/2!)+(3/3!)+(4/4!)- - - -n
So here are the n number of terms(in which a number is divided by its factorial) and I have to display the output of the sum of any number of terms which are given in scanf function.
Only one thing I know is that this program can be done by using "Nested for" loop but I haven't perfect grip yet on C language. So you guys have to have help me out in this. :)
#include <stdio.h>
#include <conio.h>
void main(void){
int s,a,b,n,fact=1;
//clrscr();
printf("Enter number of terms=");
scanf("%d",&n);
for(a=1;a<=n;a++) {
fact=fact*a;
b=(a/fact);
printf("Sum=%d",s);
}
getche();
}
P.S It's must for me to do it with "Nested for" loop.
No you do not need any Nested for loops to solve your problem. Here's a procedure you may follow:
function factorial
Input: numbers L.
Output: factorial of L.
function sum
Input: n.
Output: sum.
sum = 0;
for i = 1 to n, do
sum ← sum + (i / factorial(i))
return sum
#include <stdio.h>
int main(void) {
// your code goes here
int n;
float sum = 0,d,fact =1,j,i;
printf("Enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++){
fact = 1;
for (j = 1; j <= i; j++){
fact = fact * j;
}
d = (float) i / (float) fact ;
sum = sum + d;
}
printf("sum = %f", sum);
return 0;
}
Its working ..you can check over here :-https://ideone.com/JVXQVX
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 9 years ago.
Improve this question
The following code generates Fibonacci numbers using a Do While function, instead of specifying the exact number of the exit condition while (b < 144) I want to be less precise and just make it not to show numbers that are greater that an specific value (let's say 150).
how can I do it?
please note that I'm only know basic functions.
int main()
{
int a;
int b = 0;
int c = 1;
printf("the fibonacci numbers up to 144 are:\n");
printf("%i", b);
do
{
a = c+b;
printf("\n%i", a);
c = a+b;
printf("\n%i", c);
b = c+a;
printf("\n%i", b);
}
while (b < 144);
return 0;
}
I'm not sure what do you intend for "on demand". If you mean on demand by code you could use an internal if and if condition is true then exit the loop with break command. Example extracted by linked URL:
for (i = 0; i < 5; i++) {
if (string[i] == '\0')
break;
length++;
}
If on demand means "as soon you want to stop it" then use CTRL+C sice you are in a console application.
Edit: Since your function already exit on command, you probably want to know "how to read the standard input in a console application?"
Answer: Using the scanf function (here an example):
#include <stdio.h>
int main() {
int n;
printf("Insert the value of N: ");
scanf ("%d",&n);
printf("N*N is equal to: %d\n", n*n);
getch();
}
You might want to use somthing like that :
while (a < MAX_VALUE && b < MAX_VALUE && c < MAX_VALUE)
Where MAX_VALUE is a preprocessor macro at the start of your program after the includes (http://gcc.gnu.org/onlinedocs/cpp/Macros.html#Macros), for example :
#define MAX_VALUE 100000
You can enter the limit for the number up to which do you want the series.
int n;
printf("Enter the limit: ");
scanf("%d", &n);
printf("\nthe fibonacci numbers up to %d are:\n", n);
printf("%i", b);
do
{
...
n--;
}
while (n);
Solved it: added a user specified limit and an if statement to check after each addition:
thanks for the help!
float lim;
int a;
int b = 0;
int c = 1;
printf("The following program generates Fibonacci numbers up to a specified value\n");
printf("Please enter a value of your choise:");
scanf("%f4", &lim);
printf("the fibonacci numbers up to %.2f are:\n", lim);
printf("%i", b);
do
{
a = c+b;
c = a+b;
b = c+a;
if ( a >= lim) break;
printf("\n%i", a);
if ( c >= lim) break;
printf("\n%i", c);
if ( b >= lim) break;
printf("\n%i", b);
}
while(b <= lim);