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
Related
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 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.
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 3 years ago.
Improve this question
While solving the code for the problem Plus Multiply on CodeChef, I tried to solve it using binary search, but my solution gives a TLE.
I have written the following code:
#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
for(int a=0;a<t;a++)
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++)
scanf("%d",&arr[i]);
int s=0,j,k,f,m,mm;
(n%2==0)?(mm=n/2+1):(mm=n/2+1);
for(j=0;j<mm;j++)
{
f=n-j-1;
for(k=j+1;k<n-1;k++)
{
m=n-k-1;
if((arr[j]*arr[k])==(arr[j]+arr[k]))
s++;
if((arr[f]*arr[m])==(arr[f]+arr[m]))
s++;
}
if((arr[j]*arr[k])==(arr[j]+arr[k]))
s++;
}
printf("%d\n",s);
}
return 0;
}
Kindly suggest me on how to improve the time complexity of the above code.
As per your solution, the code looks to me as of complexity O(n^2).
But, as per the given constraints, the question expects me to solve it in O(n) time complexity.
The property, ab = a+b is special and exclusive only to 0 and 2.
Therefore, just by finding the number of pairs of 0's and 2's.
And it can be calculated using combinations.
Have a look at the following code:
#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;
int main(){
int t;
cin>>t;
while(t--){
int n;
cin>>n;
LL z = 0; //Zero count
LL t = 0; //One count
while(n--){
int x;
cin>>x;
if(x==0){
z++;
}
if(x==2){
t++;
}
}
cout<<((z)*(z-1LL)/2LL) + ((t)*(t-1LL)/2LL)<<endl;
}
return 0;
}
Time Complexity: O(N)
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 3 years ago.
Improve this question
Calculating e^x but I'm really new to programming. I've made like two programs before. I've fixed any errors the shell points out yet get zero once it is run.
I have this :
#include <stdio.h>
int main (void) {
float answer = 1.0, x, next_term, n;
int power;
printf("Enter x: ");
scanf("%f", &x);
printf("Enter the number of terms: ");
scanf("%f", &n);
next_term = x;
for (power = 1; power <= n ; power = power + 1) {
answer = answer * next_term;
next_term = next_term * x / (power + 1);
}
printf("exp(%f) = %f\n", x, answer);
return 0;
}
Output:
exp(8.000000) = 0.000000
If you're calculating an infinite series, you want to add the terms, not multiply them... i.e. answer=answer+next_term;
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 need to create a program that gives the sum of the all the numbers between the constants of a and b given by the user. b needs to be greater than a.
#include <stdio.h>
void main()
{
int index, begno, endno, sum = 0;
printf("Program for sum of all numbers in the given range\n");
printf("Enter Beg. No.: ");
scanf("%d", &begno);
printf("Enter End. No.: ");
scanf("%d", &endno);
index = begno;
for(; index <= endno; index ++)
sum = sum + index;
printf("The sum of even numbers between %d and %d is: %d", begno, endno, sum);
}
The code given looks OK, but if you want the sum without including the last number, as is generally the case you should change the for loop like this
for(; index < endno; index ++)
I would start by implementing a loop to compute:
$$\sum_{n=a}^{b}
n$$