A2 Fa23 BCS 155
A2 Fa23 BCS 155
A2 Fa23 BCS 155
Sahiwal, Pakistan
Assignment # 2
Submitted by:
ABDULLAH(FA23-BCS-155)
Submitted to:
Sir Shehzad Ali
Solution:
#include <iostream>
using namespace std;
int main()
{
int n;
double sum = 0.0;
cout << "Input the value for nth term: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
double termValue = 1.0;
for (int j = 1; j <= i; ++j) {
termValue *= i;
}
double term = 1.0 / termValue;
cout << "1/" << i << "^" << i << " = " << term << endl;
sum += term;
}
cout << "The sum of the above series is: " << sum << endl;
return 0;
}
Output:
Problem #10:
Write a code in C++ to calculate the sum of the series (1*1)+(2*2)+(3*3)+(4*4)+…..+(n+n).
.Solution:
#include <iostream>
int main()
int i, n, sum = 0;
cout << " Input the value for nth term: ";
cin >> n;
sum += i * i;
cout << i << "*" << i << " = " << i * i << endl;
cout << " The sum of the above series is: " << sum << endl;
return 0;
Output:
Problem #12:
Write a code in C++ to calculate the sum of series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... +
(1+2+3+4+...+n).
Solution:
#include <iostream>
int main()
cout << " Input the value for nth term: ";
cin >> n;
sum1 = 0;
sum += j;
sum1 += j;
cout << j;
if (j < i)
cout << " The sum of the above series is: " << sum << endl;
return 0;
}
Output:
Problem# 13:
Write a program in C++ to list non-prime numbers from 1 to an upper bound.
Solution:
#include <iostream>
#include <cmath>
int main()
int upper_limit;
cout << " The non-prime numbers are: " << endl;
if (num % fact == 0)
break;
}
return 0;
Output:
Problem #14
Write a program in C++ to print a square pattern with # character.
Solution:
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"input the number of character for a side:";
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<"#";
}
cout<<endl;
}
}
Output:
Problem #15:
Write a program in C++ to display the cube of the number upto given an integer.
Solution:
#include <iostream>
int main()
int n, cube;
cin >> n;
cube = i * i * i;
cout << "Number is : " << i << " and the cube of " << i << " is: " << cube << endl;
Output:
Problem #16:
Write a program in C++ to display the multiplication table vertically from 1 to n.
Solution:
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Input the number up to: ";
cin >> n;
cout << "Multiplication table from 1 to " << n << endl;
for (int i = 1; i <= 10; ++i) {
for (int j = 1; j <= n; ++j) {
cout << j << "x" << i << "=" << j * i << "\t";
}
cout <<endl;
}
return 0;
}
Output:
Problem #17:
Write a code in C++ to display the n term of odd natural number and their sum.
Solution:
#include <iostream>
int main()
int i, n, sum = 0;
cin >> n;
sum += 2 * i - 1;
cout << "\n The Sum of odd Natural Numbers up to " << n << " terms: " << sum << endl;
Output: