Professional Documents
Culture Documents
Assignment #2: Programming Fundamentals
Assignment #2: Programming Fundamentals
Assignment #2
#include <iostream>
int main ()
int a, b,factorial;
cin>>a;
factorial=factorial*b;
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>
int main()
int a, product;
if (a%2!=0)
{
product=product*a;
else
continue;
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>
int main()
int a, product;
if (a%5==0)
product=product*a;
else
{
continue;
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>
int main()
int a, small;
cout << "Enter integers (The first value read \nspecifies the number of values
remaining): \n";
cin >> a;
cin >> b;
small = b;
}
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>
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;
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>
int main()
double a, b, c;
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>
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;