32. Program to calculate sum of the following series using for and do while loop: 1/3 + 3/5 + 5/7 + ... + 97/99
Chapter 6:
Looping Structures
Programming Exercise
Problem # 32:
Write a program to calculate sum of the following series using for and do while loop:
1/3 + 3/5 + 5/7 + ... + 97/99
Solution:
Using a for loop:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float n1, n2, sum=0;
for(n1=1,n2=3;n1<=97;n1+=2,n2+=2)
{
sum=sum+(n1/n2);
}
cout<<"The sum of the series is: "<<sum;
getch();
}
#include<conio.h>
using namespace std;
int main()
{
float n1, n2, sum=0;
for(n1=1,n2=3;n1<=97;n1+=2,n2+=2)
{
sum=sum+(n1/n2);
}
cout<<"The sum of the series is: "<<sum;
getch();
}
Using a do while loop:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float n1=1, n2=3, sum=0;
do
{
sum=sum+(n1/n2);
n1+=2;
n2+=2;
}
while(n1<=97);
cout<<"The sum of the series is: "<<sum;
getch();
}
#include<conio.h>
using namespace std;
int main()
{
float n1=1, n2=3, sum=0;
do
{
sum=sum+(n1/n2);
n1+=2;
n2+=2;
}
while(n1<=97);
cout<<"The sum of the series is: "<<sum;
getch();
}
Let me know in the comment section if you have any question.
Previous Post:
31. Program to add first nine terms of following series using for and while loop: 1/3! + 5/4! + 9/5! + ... where ! indicates factorial.
Comments
Post a Comment