0% found this document useful (0 votes)
78 views7 pages

Assignment #2: Programming Fundamentals

The document contains 7 questions asking to write C++ programs to perform various calculations and tasks using loops and conditional statements. Sample code is provided for each question that uses the appropriate loops and logic to solve the problem. The questions cover topics like calculating factorials, products, determining if values can form a triangle, and generating tables of values based on varying inputs.

Uploaded by

Muzzamil Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
78 views7 pages

Assignment #2: Programming Fundamentals

The document contains 7 questions asking to write C++ programs to perform various calculations and tasks using loops and conditional statements. Sample code is provided for each question that uses the appropriate loops and logic to solve the problem. The questions cover topics like calculating factorials, products, determining if values can form a triangle, and generating tables of values based on varying inputs.

Uploaded by

Muzzamil Hussain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Programming Fundamentals

Assignment #2

Class and Section:


B.Sc. (SE) PEC-Batch-02
Submitted By:
Muzzamil Hussain
Roll No:
F-201041
Submitted To:
Mr. Qamar Abbas

Dated: June 14, 2020


Q1: Write a complete C++ program to calculate factorial of a +ve number within range 1-
10.

#include <iostream>

using namespace std;

int main ()

int a, b,factorial;

cout<<"Enter a number from 1 to 10 \n";

cin>>a;

for (b=1; b<=a; b++)

factorial=factorial*b;

cout<<"Factorial of Entered Number is = "<<factorial<<endl;

system ("pause");

return 0;

Q2: Write a complete C++ program that uses a for loop statement to calculate and print
the product of the odd integers from 1 to 15.

#include <iostream>

using namespace std;

int main()

int a, product;

for (a=1; a<=15; a++)

if (a%2!=0)
{

product=product*a;

else

continue;

cout<<"The product of the odd integers from 1 to 15 = "<<product<<endl;

system ("pause");

return 0;

Q3: Write a complete C++ program that uses a for loop statement to calculate and print
the product of the integers from 1 to 25 which are divisible by 5.

#include <iostream>

using namespace std;

int main()

int a, product;

for (a=1; a<=25; a++)

if (a%5==0)

product=product*a;

else
{

continue;

cout<<"The product of the integers from 1 to 25 which are divisible by 5 =


"<<product<<endl;

system ("pause");

return 0;

Q4: Write a complete C++ program that uses a for loop statement to find the smallest of
several integers. Assume that the first value read specifies the number of values
remaining and that the first number is not one of the integers to compare.

#include <iostream>

using namespace std;

int main()

int a, small;

cout << "Enter integers (The first value read \nspecifies the number of values
remaining): \n";

cin >> a;

cin >> small;

for(int b; a > 1; a-- )

cin >> b;

if( b < small )

small = b;
}

cout << "Smallest integer is " << small << endl;

system("pause");

return 0;

Q5: Write a complete C++ program to calculate: PI = 4 – 4/3 + 4/5 - 4/7 + 4/9 – 4/11 +
4/13.

#include <iostream>

using namespace std;

int main()

double pi=0;

int sign = 1;

double terms=1, a;

cout<<"Enter the number of terms present in the series \n(Enter values greater than 800
to get more precise result) \n";

cin>>a;

while(terms <=a)

pi += sign*4.0/terms;

terms += 2;

sign *= -1;

cout<< "value of pi is "<<pi;

system ("pause");

return 0;
}

Q6: Write a complete C++ program that reads three nonzero double values and
determines and prints whether they could represent the sides of a triangle.

#include <iostream>

using namespace std;

int main()

double a, b, c;

cout << "Enter the three numbers: " << endl;

cin >> a >> b >> c;

if( (a+b<=c) || (b+c<=a) || (a+c<=b) )

cout << "They could NOT represent the sides of a triangle" << endl;

else

cout << "They could represent the sides of a triangle" << endl;

system("pause") ;

return 0;

}
Q7: Write a complete C++ program which will produce a table of values of j, k and m,
where m varies from 1 to 6, and, for each value of m, k varies from 5.5 to 12.5 in steps of
0.5.

j = 2 + ( m + 0.5 k)

#include<iostream>

using namespace std;

int main()

float j=1,k,m;

for(m=1.0;m<=6.0;m++)

for(k=5.5;k<=12.5;k+=0.5)

j=2+(m+(0.5*0.5));

printf("%.2f\t%.2f\t%.2f\n",j,m,k);

system ("pause");

return 0;

You might also like