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
Im trying to find the sum of n numbers using a while loop so that it runs like so:
How many numbers: 3
-3,
4,
13,
The sum is: 14
However what I am getting is this:
How many numbers: 3
2,
1,
The sum is: 3
I dont understand it, because i set i = 0
#include <stdio.h>
int main(void) {
int numbers;
printf("How many numbers: ");
scanf("%d", &numbers);
int sum = 0;
int i = 0;
while (i < numbers) {
scanf("%d", &numbers);
sum = sum + numbers;
i++;
}
printf("The sum is: %d", sum);
return 0;
}
A correct solution would be:
#include <stdio.h>
int main(void) {
int numbers;
printf("How many numbers: ");
scanf("%d", &numbers);
int sum = 0;
int i = 0;
int number; // use different variable for the input numbers
while (i < numbers) {
scanf("%d", &number);
sum = sum + number;
i++;
}
printf("The sum is: %d", sum);
return 0;
}
The problem was that you were using one variable for two different things.
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 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
#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator(){
int k;
printf("Masukkan batas bilangan genap = ");
scanf("%i", &k);
printf("Bilangan genap dari 0 sampai %i adalah :\n");
for (int i=0; i<=k;i+=2){
printf("%i\n", i);
}
}
int main(){
genap_generator();
system("pause");
}
I made a program to generate odd numbers with int k as the max number but when i print the integer it doesnt print kprint error
in the line where you want to print k: printf("Bilangan genap dari 0 sampai %i adalah :\n");, you didnt pass k.
The line should be: printf("Bilangan genap dari 0 sampai %i adalah :\n", k);.
What the function does print right now is what is placed on the stack where k should have been.
Also, you are printing all the even numbers, if you want to print the odd numbers start from i=1.
Welcome to our community! Could you use English language in your code next time?
The problem was with printing in printf, you did not declare which variable you wanted to print. Here is the working code:
#include <stdio.h>
#include <stdlib.h>
//odd number generator with int k as the max number
void genap_generator()
{
int k;
printf("Enter an even number limit = ");
scanf("%i", &k);
// Had to declare, which variable to print
printf("The even numbers from 0 to %i are:\n", k);
for (int i = 0; i <= k; i += 2)
{
printf("%i\n", i);
}
}
int main()
{
genap_generator();
system("pause");
}
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
So i want my code actualy had different sum each cases, but the sum keep adding from the other cases, like for example the cases 2 sum are sum from loop 2nd + cases 1, the cases 4 sum are sum from cases 1,2,3 and loop number 4
the Output.
`
#include <stdio.h>
int main() {
int cases;
int day;
int animal;
int n;
int a;
int sum = 0;
animal = 0;
printf("Enter cases \n ");
scanf(" %d", &n);
for (cases = 0; cases < n; cases++)
{
printf("cases #%d\n", cases+1);
printf("Enter how many days.\n");
scanf(" %d", &a);
for(day=1;day<=a;){
printf("Enter how many animal that you capture at day #%d\n", day);
scanf(" %d", &animal);
day++;
sum = sum + animal;
}
animal = 0;
day = 1;
printf("cases#%d = %d\n", cases + 1, sum);
}
return 0;
}`
You need to return sum to 0 in every case
Put sum=0; under for (cases...
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
I am trying to figure out how to get the prime number n, and calculate the sum of cubes for that n (1^3 + 2^3 + ... + n^3). So far I can figure out how to get the primes. I just can't figure out how to get that same n to calculate its primes. This is what I have so far:
#include <stdio.h>
int main() {
int n, i, c = 0
printf("Enter any number n: ");
scanf("%d", &n);
for(i=2; i<=n/2; i++){
if(n%i == 0){
c=1;
break;
}
}
if (c==0)
printf(%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
printf("Enter that same number n: ");
scanf("%d", &num);
int num, cube, sum = 0, j=1;
while (j <= num) {
cube = j*j*j;
sum = sum + cube;
j++;
}
printf("sum of cubes of %d is %d\n", num, sum);
return 0:
}
I get an error on the second scanf because it says num is undeclared. What should I do to fix this situation.
Use scanf after declaring int num. Also you have used % in 2nd num. Use
int num, cube, sum = 0, j=1;
scanf("%d", &num);
instead of
scanf("%d", %num);
int num, cube, sum = 0, j=1;
Consider this starting piece
long double sumcubs(int n)
{
int i;
long double ret = 0;
for (i = 1; i <= n; ++i)
ret += (i * i * i);
return ret;
}
Once you have encapsulated this sum of cubes function, it should become easier to organize and write the rest of your code.
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