A2 Fa23 BCS 155

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

COMSATS University Islamabad

Sahiwal, Pakistan

Course Programming Fundamental(lab) Couse code CS103


Course Instructor Shehzad Ali Program Name BSCS

Assignment # 2

Submitted by:
ABDULLAH(FA23-BCS-155)

Submitted to:
Sir Shehzad Ali

Department of Computer Science (2023-27)


Problem #9:
Write a program in C++ to find the sum of the series 1 + 1/2^2 + 1/3^3 + ..+ 1/n^n.

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>

using namespace std;

int main()

int i, n, sum = 0;

cout << " Input the value for nth term: ";

cin >> n;

for (i = 1; i <= n; i++)

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>

using namespace std;

int main()

int i, j, n, sum = 0, sum1;

cout << " Input the value for nth term: ";

cin >> n;

for (i = 1; i <= n; i++)

sum1 = 0;

for (j = 1; j <= i; j++)

sum += j;

sum1 += j;

cout << j;

if (j < i)

cout << "+";

cout << " = " << sum1 << endl;

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>

using namespace std;

int main()

int upper_limit;

cout << " Input the upper limit: ";

cin >> upper_limit;

cout << " The non-prime numbers are: " << endl;

for (int num = 2; num <= upper_limit; ++num)

int mfactor = (int)sqrt(num);

for (int fact = 2; fact <= mfactor; ++fact)

if (num % fact == 0)

cout << num << " ";

break;
}

cout << endl;

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>

using namespace std;

int main()

int n, cube;

cout << "Input the number of terms : ";

cin >> n;

for (int i = 1; i <= n; i++)

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>

using namespace std;

int main()

int i, n, sum = 0;

cout << " Input number of terms: ";

cin >> n;

cout << " The odd numbers are: ";

for (i = 1; i <= n; i++)

cout << 2 * i - 1 << " ";

sum += 2 * i - 1;

cout << "\n The Sum of odd Natural Numbers up to " << n << " terms: " << sum << endl;

Output:

You might also like