Programming Problems
Programming Problems
} }
Quest 6 Quest 7
Write a program that calculates the sum of the
Write a program that calculates the sum of the
first n terms of the Fibonacci series (1, 1, 2, 3, 5,
first n terms of the series 1 - 1/2 + 1/3 - ... + (-
8, ...). The program should ask the user to enter
1)^n/n. The program should ask the user to enter
the value of n, and then use a loop to calculate
the value of n, and then use a loop to calculate
the sum.
the sum.
#include <iostream>
#Check note!
#include <math.h>
Quest 8
Write a program that calculates the sum of all
the even numbers between 1 and a given
using namespace std;
number. For example, if the user inputs the
number 10, the program should print out 30
(which is the sum of 2+4+6+8+10).
int main(){
#include <iostream>
double sum;
int i,n;
int sign = 1; cout<<"Enter a number =";
for(int i=1; i<=n;i++){ cin>>n;
if((i%2)==0){ int sum = 0;
sum = sum -(1/(double)i); for( i=0;i<=n;i++){
} if((i%2)==0){
else { sum = sum+i;
sum = sum +(1/(double)i); }
} }
} cout<<"\nSum of all evens till nth term :
cout<<sum; "<<sum;
return 0; #include <iostream>
} #include <math.h>
if((i%3)==0){
int n; }
cout<<"Enter a number:"; }
return 0;
Quest 10
Write a program that calculates the sum of all
the numbers divisible by 3 between 1 and a
given number. For example, if the user inputs
the number 9, the program should print out 18
(which is the sum of 3+6+9).
Quest 12 Quest 13
Write a program that asks the user to enter the Write a program that calculates the total mass of
name and diameter of each planet in the solar the solar system. The program should ask the
system and print them in the Command Prompt. user to enter the mass of each planet, and then
The program should then use a loop to print out use a loop to calculate the total mass.
the name and diameter of each planet.
#include <iostream>
#include <iostream>
int main(){
int main(){
string Planet_mass[8]=
//char p[100]={};
{"Mercury","Venus","Earth","Mars","Jupiter","S
string planet[8]= {}; aturn","Uranus","Neptune"};
int main(){
cout<<"\nAverage density ="<<avg_dens;
float
return 0;
density[8]={},mass[8]={},radius[8]={},volume[8]
={},diameter[8]={}; }
string Quest 15
planet[8]={"Mercury","Venus","Earth","Mars","
Write a program that keeps track of the number
Jupiter","Saturn","Uranus","Neptune"};
of copies of each comic book in a collection.
cout<<"___Mass & diameter Collection___\ The program should ask the user to enter the
n"<<endl; name and quantity of each comic book, and then
use a loop to update the count for each comic
for(int i=0;i<8;i++){ book.
cout<<"Enter mass for "<<planet[i]<<" :";
#include <iostream>
cin>>mass[i];
#include <string.h>
cout<<"Enter diameter for "<<planet[i]<<" :";
cin>>diameter[i];
using namespace std;
}
float avg_dens;
int main(){
float sum = 0;
int n;
int co_unt = 1;
char name[100];
for(int i=0;i<8;i++){
cout<<"Enter the quantity for this collection :";
radius[i] = (float) diameter[i] / 2;
cin>>n;
volume[i] = (float)
4/3*(3.1416)*pow(radius[i],3); int q[]={};
density[i] = (float)mass[i]/volume[i]; for(int i=0;i<n;i++){
sum = sum + density[i]; cout<<"Enter name of the comic book :";
cin.ignore();
cin.get(name,100); value of n, and then use a loop to calculate the
sum.
cin.ignore();
#include <iostream>
cout<<"Enter quantity :";
#include <iomanip> //(input output manipulate)
cin>>q[i];
using namespace std;
}
return 0;
int main(){
}
int n;
Quest 16
cout<<"Enter a number :";
Write a program that calculates the sum of the
first n terms of the series 1 + 2 + 3 + ... + n. The cin>>n;
program should ask the user to enter the value of
n, and then use a loop to calculate the sum. float sum =0;
cout<<setprecision(3);
int n; return 0;
cin>>n;
for(int i=0;i<=n;i++){
return 0;
Quest 17