0% found this document useful (0 votes)
19 views

PF Assignment

The document contains 4 C++ code snippets that demonstrate while loops, for loops, and calculating factorials and their sums. The first snippet uses a while loop to input 5 numbers and calculate their sum. The second uses a for loop to calculate the power of a base number. The third uses a while loop to calculate the factorial of 5. The fourth uses a for loop to calculate the factorial of numbers 1 through 5 and sum the results.

Uploaded by

Touheed Hassan
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)
19 views

PF Assignment

The document contains 4 C++ code snippets that demonstrate while loops, for loops, and calculating factorials and their sums. The first snippet uses a while loop to input 5 numbers and calculate their sum. The second uses a for loop to calculate the power of a base number. The third uses a while loop to calculate the factorial of 5. The fourth uses a for loop to calculate the factorial of numbers 1 through 5 and sum the results.

Uploaded by

Touheed Hassan
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/ 4

Qno.

2
#include<iostream>
using namespace std;
int main()
{
int num1, num2, num3, num4, num5, x = 1, sum = 0;
while (x < 6)
{
cout << "enter five integer number" << endl;
cin >> num1 >> num2 >> num3 >> num4 >> num5;
sum = num1 + num2 + num3 + num4 + num5;
x++;
}
cout << "result is" << sum << endl;
system("pause");
return 0;
}

Qno.2
#include<iostream>
using namespace std;
int main()
{
int a, b, result= 1, i;
cout << "enter base" << endl;
cin >> a;
cout << "enter power" << endl;
cin >> b;
for (i = 1; i <= b; i++)
{
result=result*a;
}
cout << result << endl;
system("pause");
return 0;
}

Qno.3
#include<iostream>
using namespace std;
int main()
{
int a=5, fact = 1, i = 1;
while (i <= a)
{
fact = fact * i;
i++;
}
cout << fact << endl;
system("pause");
return 0;
}
Qno.4
#include<iostream>
using namespace std;
int main()
{
int i, fact = 1, sum = 0;
for (i = 1; i <= 5; i++)
{
fact = fact * i;
sum = sum + fact;
}
cout << sum << endl;
system("pause");
return 0;
}

You might also like