how do i add all the elements of the for loop? - c

#include <stdio.h>
int even_numbers_sum(){
int d;
for(d=0;d<=100;d++){
if(d%2==0){
printf("\n this is even number loop %d",d);
}
}
int sum;
sum=sum+d;
printf("\n the value of total sum of the loop %d",sum);
return sum;
}
int main(){
even_numbers_sum();
}
my main aim is to calculate the sum of all the elemets of for loop i took a for loop and described its value and put a equation (if) to pass anynumber if its only even other wise it will not work and i have to add all those even number till 100 how do i do it

Initialised sum to 0 at the beginning. Changed the return type of the function to void as int was unnecessary. Sum has to be incremented in the if statement.
#include <stdio.h>
void even_numbers_sum(){
int d;
int sum=0;
for(d=0;d<=100;d++){
if(d%2==0){
printf("\n this is even number loop %d",d);
sum=sum+d;
}
}
printf("\n the value of total sum of the loop %d",sum);
}
int main(){
even_numbers_sum();
}

look the following code. I've written this code for any number if you want to change the upper bound instead of 100 , you just have to pass the integer to the function i.e. if you want to get the sum of all even integers from 0 to 100, you have to pass 100 while calling the function.
#include <stdio.h>
void even_numbers_sum(int i){
int d;
int sum=0;
for(d=0;d<=i;d+=2){
printf("\n this is even number loop %d",d);
sum=sum+d;
}
}
printf("\n the value of total sum of the loop %d",sum);
}
int main(){
even_numbers_sum(100);
return 0;
}
1.first change is here in function declaration and definition you have to write int i in the parentheses.
void even_numbers_sum(int i)
2.Secondly there is no need to put the condition if you want to start from 0. it can be done by simply increment d by 2.
for(d=0;d<=i;d+=2)
3.Your main method has return type int so there should be some integer returned.
return 0;
I hope this will help you to solve your problem. If there is anything which you want to ask I'll be glad to help you.

Firstly, you should initialize sum as 0 at begin. sum may be not 0 without initialization.
Secondly, in your code, sum will be only add with the final even number (100), not add each each even number from 0 to 100.
A correct example as below:
#include <stdio.h>
int even_numbers_sum()
{
int d, sum = 0; // initialize sum as 0
for (d = 0; d <= 100; d++) {
if (d % 2 == 0) {
printf("\n this is even number loop %d", d);
sum = sum + d; // add each even number to sum
}
}
// sum=sum+d; // This line code will add final even number and sum
printf("\n the value of total sum of the loop %d", sum);
return sum;
}
int main()
{
even_numbers_sum();
}

Related

How to calculate 3 prime numbers in sequence and divide two of them

That is the code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int min=0, med=0, max=0, n=0, t=2,
k=2, j=2, flag=0, counter=0,
a=0, b=0, c=0;
scanf("%d%d", &min, &max);
for(; min<=max; min++){
while(t <= min/2){
if(min%t==0){
flag=1;
break;
}
t++;
}
if(flag==0){
counter++;
}
t=2;
flag=0;
}
printf("%d", counter);
return 0;
}
This code prints all the prime numbers of a given range.
I want to store the first 3 prime numbers into different variables, and then check if the minimum and the maximum divided by two equals the mid one.
Also, I want to keep doing it until the program reaches the end of the range.
I have no idea how to store the first three in different variables inside the loop, or even if have I cannot do it.
I'd appreciate any hints.
I am still wondering how could I implement that inside the loop, because it only gives me one value which is "min".
You could store the current found prime in another variable (c) after you stored that variable in another variable (b) after you stored that variable in another variable (a); this way, you have the last seen three primes at hand.
a = b, b = c, c = min;
if (b-a == c-b) printf("%d %d %d\n", a, b, c);

c program to calculate value of e^x, correct upto 10 decimal places

I am writing a program that calculates the value of e^x, according to the expansion formula.I need to print the answer so that it is correct upto 10 decimal places. I have tried to do it using, for, while and do while loop , however I cannot figure out where to terminate the loop , which condition to use to terminate the loop. i have written the code as follows:
#include<stdio.h>
#include<math.h>
int factorial(int x);
int main()
{
int x,n;
float sum,d_1;
printf("Enter the value of power :");
scanf("%d",&x);
n=1;sum=1;
while(sum <= %.10f)
{
d_1=pow(x,n)/factorial(n);
sum=sum+d_1;
n++;
}
printf("Answer is %f",sum);
return 0;
}
int factorial(int y)
{
int fact=1,i;
for(i=1;i<=y;i++)
{
fact=fact*i;
}
return(fact);
}
and i am getting the error message expected expression before %.
Please help.
You want to terminate the loop when the current term is smaller than some value. Also you want the loop to execute at least once, so a do-while loop is preferable:
n = -1;
do {
n++;
d_1 = pow(x, n) / factorial(n);
sum = sum + d_1;
} while (d_1 > epsilon);
As mentioned in the comments, there are other issues with the code when it comes to precision and overflow but that is a different story.

Average of Even Numbers and Product of all Odd Numbers

#include <stdio.h>
#include <conio.h>
int getn(int n, int i);
int main()
{
int n, i;
getn(n, i);
getch();
return 0;
}
int getn(int n, int i)
{
int even = 0;
int odd = 1;
int avg;
printf("Enter ten integers: \n");
for (i = 1 ; i <= 10 ; i++)
{
printf("Integer %d: ", i);
scanf("%d", &n);
if ( n % 2 == 0 )
{
even = even + n;
}
else
{
odd = odd * n;
}
}
avg = even / 10;
printf("\n\nAverage of even numbers: %d", avg);
printf("\nProduct of odd numbers: %d", odd);
}
It seems the even calculations worked but when it comes to odd it gives the wrong answer. Please help
Our instructor wants us to use looping or iterations. No arrays. Please help me
First, your C code needs some correction:
at least give the prototype of getn before using it
getn is defined to return an int and doesn't return anything. Either replace int with void or return a value.
Second,
Your code computes the product of ten numbers, if this product is too big, it cannot be store as-is in an int. For example, it works well if you enter ten times number 3, the result is 59049, but if you enter ten times number 23, it will answer 1551643729 which is wrong because 23^10=41426511213649 but that can't be stored in an int. This is known as arithmetic overflow.
Your average is bad, because you sum ints, but the average is (in general) a rational number (average(2,3)=2.5 isn't it ?). So double avg = out/10.0; (means compute a floating division) and printf("Average %f\n",avg); would be better.

Fibonacci sequence while loop

I have to write code that displays the Fibonacci sequence to the user desired number of terms and must also use a while loop. I'm not sure why this code isn't working.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int max;
printf("Enter the max term of the Fibonacci Sequence:\n");
scanf("%i", &max);
int a=0;
int b=0;
a=2;
while(a<max) {
if((a==0||a==1))
{
printf("%i\n", &a);
++a;
}
else if(a>1)
{
a=(a-1)+(a-2);
printf("%i\n", &a);
++a;
}
}
return 0;
}
You can try this.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
int max;
printf("Enter the max term of the Fibonacci Sequence:\n");
scanf("%i", &max);
int n=0;
int a=0;
int b=1;
int next;
while(n<max) {
if ( n <= 1 )
{
next = n;
n++;
}
else
{
next = a + b;
a = b;
b = next;
n++;
}
printf("%d\n", next);
}
return 0;
}
issues with your code:
following declaration & initialisation, you set a=2 => it won't take the true branch of the if statement -- '0' will not be printed in your result.
a=(a-1)+(a-2); i.e a = 1
then you are doing ++a; => a == 2. thus it again else statement with same a==2.
hence it will print the same value and loop executes infinitely.
In the very beginning of your program (before the while loop) a is 2 (see the line a=2).
And in the while loop you do following:
a=(a-1)+(a-2); // a = 2-1+2-2 i.e. a = 1
and right after it
++a; // a == 2
So, after it a==2 again. This loop never ends.
But it is technical problem. More important is that you are trying to calculate not Fibonacci Sequence. In the Fibonacci Sequence each subsequent number is the sum of the previous two. But in your code there is adding of not two previous numbers of Fibonacci Sequence, but previous two Natural numbers.
You have variable b, because someone told you to add it. And it was right! Just remember previous found element of the Fibonacci Sequence in b. When you know previous one element and current one, it is possible to calculate next one.

How to save do...while status/progress in C?

I wrote this program, which returns the biggest integer inserted by the user. Now, I would like the program to return the 2nd biggest integer. I created a new variable (called "status"), which is supposed to increment 1 unit every time the cycle repeats. Then, after the break condition happens, I would step back 1 unit in status variable, in order to retrieve the 2nd biggest number. I would like to follow this line of thought (if it is reliable in C) and I ask you fellows what is wrong with my current implementation.
#include <stdio.h>
int main()
{
int x, tmp=0, status=0, bigger;
printf("Insert numbers:\n");
do{
status+=1;
scanf("%d", &x);
if (tmp>=x)
bigger=tmp;
else {
bigger=x;
tmp=x;
}
}while (x!=0);
status-=1;
printf("The second biggest number is %d.\n", bigger);
return 0;
}
If you need only two biggest integers, you can just save them in each iteration, such that you have the max and max2 values:
#include <stdio.h>
int main()
{
int x, max=0, max2=0;
printf("Insert numbers:\n");
do {
scanf("%d", &x);
if (x > max) {
max2 = max; // Save the previous max, as the second largest value
max = x ; // Save the new max
}
else if (x > max2) {
max2 = x; // The input is not max, but greater than second max
}
}while (x!=0);
printf("The second biggest number is %d.\n", max2);
return 0;
}
The status in your code is just a counter. To solve your problem :
Use an array to store all the data.
Make a variable and test it if it is different than the biggest and bigger than all other entries
try this :
int main()
{
int x, status=0, bigger=0, secondbigger=0, Array[50];
while(true){
scanf("%d",&x)
if(x==0)break;
Array[status]=x;
status++;
}
for(i=0;i<status-1;i++){
if(Array[i]>bigger) bigger=Array[i];
}
for(i=0;i<status-1;i++){
if(Array[i]>secondbigger && Array[i]!=bigger) secondbigger=Array[i];
}
printf("The second biggest number is %d.\n", secondbigger);
return 0;
}

Resources