Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
80 views

Programming Problems

Here's some basic C++ programming problems. Document includes the following : . For loop problems . Array problems . Some arithmetical series

Uploaded by

tahsinhatake66
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

Programming Problems

Here's some basic C++ programming problems. Document includes the following : . For loop problems . Array problems . Some arithmetical series

Uploaded by

tahsinhatake66
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Quest 5 cout<<"\n\nThe average

Write a program that calculates the average distance :"<<(float)sum/8;


distance from the Sun of all the planets in the
solar system. The program should ask the user to }
enter the distance from the Sun for each planet, Quest IP_LAB_W5
and then use a loop to calculate the average
distance Write a programme that takes (initializes) an
array of 5 integers and checks if the numbers
#include <iostream>
are prime number or not.
#include <math.h>
#include <iostream>
using namespace std;

using namespace std;


int main(){

float d[8]={},sum =0;


int main(){
string
p[8]={"mercury","Venus","Earth","Mars","Jupit
er","Saturn","Uranus","Neptune"}; int a[5]={1,2,3,4,5};

cout<<"Distance of the 1st planet "<<p[0]<<"


from the Sun :";
for(int i=0;i<5;i++){
cin>>d[0];
int flag = 0;
cout<<"Distance of the 2nd planet "<<p[1]<<"
int x = a[i]/2;
from the Sun :";
for(int j=2;j<=x;j++){
cin>>d[1];
if((a[i]%j)==0){
cout<<"Distance of the 3rd planet "<<p[2]<<"
from the Sun :"; flag = 1;
cin>>d[2]; }
for(int i=3;i<8;i++){ }
cout<<"Distance of the "<<i+1<<"th planet if(flag==1){
"<<p[i]<<" from the Sun :";
cout<<a[i]<<" is a non-prime"<<endl;
cin>>d[i];
}
}
else {
for(int i=0;i<=8;i++){
cout<<a[i]<<" is a prime"<<endl;
sum = sum + d[i];
}
}
}
return 0; return 0;

} }

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 n; using namespace std;


cout<<"Enter a number for the serise = ";

cin>>n; int main(){

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>

Quest 9 using namespace std;

Write a program that calculates the sum of the


squares of all the odd numbers between 1 and a
given number. For example, if the user inputs int main(){
the number 10, the program should print out 165 int n;
(which is the sum of 1+9+25+49+81).
cout<<"Enter a number:";

#include <iostream> cin>>n;

#include <math.h> int sum = 0;

using namespace std; for(int i=1;i<=n;i++){

if((i%3)==0){

int main(){ sum = sum + i;

int n; }

cout<<"Enter a number:"; }

cin>>n; cout<<"Sum of all numbers divisible by 3 till


nth :"<<sum;
int sum = 0;
return 0;
for(int i=1;i<=n;i++){
}
if((i%2)!=0){
Quest 11
sum = sum + pow(i,2);
A weather station wants to calculate the average
} temperature and humidity over a certain period
} (5 days). Write a program that asks the user to
input the temperature and humidity readings for
cout<<"\nSum of all odd squared numbers till five days and calculate rates for the average
nth term :"<<sum; temperature and humidity.

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>

using namespace std;


using namespace std;

int main(){
int main(){
string Planet_mass[8]=
//char p[100]={};
{"Mercury","Venus","Earth","Mars","Jupiter","S
string planet[8]= {}; aturn","Uranus","Neptune"};

float x[8]={}; float x[8]={};

for(int i=0;i<8;i++){ float sum =0;


cout<<"Enter the planet name :"; for(int i=0;i<8;i++){
cin>>planet[i]; cout<<"Mass of "<<Planet_mass[i]<<" is :";
cout<<"Enter it's diameter :"; cin>>x[i];
cin>>x[i]; sum = sum + (float) x[i];
} }
cout<<"The planets & it's diameters cout<<"The total mass :"<<sum;
are:"<<endl;
return 0;
for(int i=0;i<8;i++){
}

cout<<planet[i]<<" & "<<x[i]<<endl;


} Quest 14

return 0; Write a program that calculates the average


density of the planets in the solar system. The
} program should ask the user to enter the mass
and diameter of each planet, and then use a loop
to calculate the average density (mass divided by
volume).
avg_dens = (float) sum / co_unt; //if no. of
avg is said to be changed
#include <iostream>
co_unt++;
#include <math.h>
cout<<"Show density for
"<<planet[i]<<" :"<<density[i]<<endl;
using namespace std;
}

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;

#include <iostream> for(int i=1;i<=n;i++){

sum = sum +1/(float)i;

using namespace std; }

cout<<setprecision(3);

int main(){ cout<<"\nSummation of series :"<<sum;

int n; return 0;

cout<<"Enter a number :"; }

cin>>n;

int sum =0;

for(int i=0;i<=n;i++){

sum = sum +i;

cout<<"\nSummation of series :"<<sum;

return 0;

Quest 17

Write a program that calculates the sum of the


first n terms of the series 1 + 1/2 + 1/3 + ... +
1/n. The program should ask the user to enter the

You might also like