Name: Anees abbasi Department: Ai-22-Blue
Name: Anees abbasi.
Department: Ai-22-Blue.
Submitted to: Dr. Saima Jabeen.
Assignment no: 1.
Program 1:
Write a program to compute the sum and the sum of the squares of all data elements in the following
array arr[ ]={1,2,3,4,5}
Answer:
Code:
#include<iostream>
using namespace std;
int main()
int array[5]={1,2,3,4,5};
int n= sizeof(array)/sizeof(array[0]);
int sum=0;
int sum_of_square=0;
for(int i=0;i<n;i++)
sum=sum+array[i];
sum_of_square=sum_of_square+array[i]*array[i];
cout<<"Sum of Array is:"<<sum<<endl;
cout<<"Sum of Square of each element of array is:"<<sum_of_square;
Output:
Name: Anees abbasi Department: Ai-22-Blue
Program 2:
Write a Program that calculates the total price of four items {2.0, 5.0, 1.0, 2.0} after entering the quantity
for each item such as:
Answer:
Code:
#include<iostream>
using namespace std;
int main()
int item_prices[4]={2.0, 5.0, 1.0, 2.0};
int n = sizeof(item_prices) / sizeof(item_prices[0]);
int item_quantities[n];
for (int i = 0; i < n; i++)
cout <<"Enter quantity for item " << i+1 << ": ";
cin >>item_quantities[i];
float total_price=0.0;
for (int i = 0; i < n; i++)
total_price=total_price+item_prices[i] * item_quantities[i];
} cout<<"The total price of all items is:"<<total_price;
Output:
Name: Anees abbasi Department: Ai-22-Blue
Program 3:
(Conversion from kilograms to pounds) Write a program that displays the following table (note that 1 1
Kilogram is 2.2 pounds):
Answer:
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
cout <<setw(9) << "Kilogram" <<setw(10) << "Pounds" << endl;
cout <<setw(9) << "--------" <<setw(10) << "------" << endl;
for (int kg = 1;kg <= 20; kg++)
double pounds= kg*2.2;
cout<<kg<<"\t\t" <<pounds<<endl;
return 0;}
Output:
Name: Anees abbasi Department: Ai-22-Blue
Program 4:
Suppose that the tuition for a university is PKR. 90,000 this year and increases 10% every year. In one
year, the tuition will be PKR. 99,000. Write a program that computes the tuition in ten years and the total
cost of four years’ worth of tuition after the tenth year.
Answer:
Code:
#include <iostream>
using namespace std;
int main() {
const double TUITION = 90000.0;
const double RATE = 0.1;
double tuitionTenYears = TUITION;
double totalCost = 0.0;
// Compute tuition in ten years
for (int i = 1; i <= 10; i++)
tuitionTenYears =tuitionTenYears*(1.0 + RATE);
// Compute total cost of four years' tuition after ten years
for (int i = 1; i <= 4; i++)
totalCost =totalCost+tuitionTenYears;
tuitionTenYears =tuitionTenYears*(1.0 + RATE);
cout << "Tuition in ten years: PKR " <<fixed <<tuitionTenYears <<endl;
cout << "Total cost of four years tuition after ten years: PKR "<< totalCost <<endl;
return 0;
}
Name: Anees abbasi Department: Ai-22-Blue
Output:
Program 5:
Write an if-else statement to compute the amount of shipping due on an online sale. If the cost of the
purchase is less than $20, the shipping cost is $5.99. If the cost of the purchase over $20 and at most
$65, the shipping cost is $10.99. If the cost of the purchase is over $65, the shipping cost is $15.99.
Answer:
Code:
#include <iostream>
using namespace std;
int main()
double purchaseCost;
cout<<"Enter your purchase cost to know about the shipment cost:";
cin>>purchaseCost;
double shippingCost = 0.0;
if (purchaseCost < 20.0)
shippingCost = 5.99;
else if (purchaseCost <= 65.0)
shippingCost = 10.99;
else
shippingCost = 15.99;
Name: Anees abbasi Department: Ai-22-Blue
cout << "Shipping cost: $" << shippingCost << endl;
return 0;
Output:
Program 6:
Write a program called Strings2 that requires the user to type a long sentence. The program displays the
longest word in the sentence.
Sample Run:
Enter Sentence: The longest I have ever worked in my life-time was eight days!
Longest word is: life-time.
Answer:
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
string sentence;
cout << "Enter a long sentence: ";
getline(cin, sentence);
string longestWord;
string currentWord;
for (int i = 0; i < sentence.length(); i++) {
Name: Anees abbasi Department: Ai-22-Blue
char c = sentence[i];
if (c != ' ') {
currentWord += c;
} else {
if (currentWord.length() > longestWord.length()) {
longestWord = currentWord;
currentWord = "";
// Check last word
if (currentWord.length() > longestWord.length())
longestWord = currentWord;
cout << "The longest word in the sentence is: " << longestWord <<endl;
return 0;
Output:
Program 7:
Write a C++ program to swap two integers using pointers. Your program should take two integer inputs
from the user, swap their values using pointers, and then print the swapped values. You should
implement the swapping function using pointers, and not using any other temporary variable.
Name: Anees abbasi Department: Ai-22-Blue
Answer:
Code:
#include<iostream>
using namespace std;
int main()
int num1=52;
int num2=45;
int *ptr_num1=&num1;
int *ptr_num2=&num2;
cout<<"The value of 1 before swaping:"<<*ptr_num1<<endl;
cout<<"The value of 2 before swaping:"<<*ptr_num2<<endl;
ptr_num1=&num2;
ptr_num2=&num1;
cout<<"After Swapping\n";
cout<<"The Value of 1:"<<*ptr_num1<<endl;
cout<<"The Value of 2:"<<*ptr_num2<<endl;
Output:
Program 8:
Write a recursive function in C++ to print a sequence of numbers in ascending order.
Answer:
Code:
#include <iostream>
Name: Anees abbasi Department: Ai-22-Blue
using namespace std;
void printNumbers(int start, int end)
if (start > end) {
return;
cout << start << " ";
printNumbers(start + 1, end);
int main()
int start, end;
cout << "Enter the starting number: ";
cin >> start;
cout << "Enter the ending number: ";
cin >> end;
cout << "The sequence of numbers is: ";
printNumbers(start, end);
return 0;
Output:
Program 9:
Write a recursive function in C++ to compute the nth Fibonacci number, where the nth Fibonacci
number is defined as the sum of the (n-1)th and (n-2)th Fibonacci numbers, with the first two Fibonacci
Name: Anees abbasi Department: Ai-22-Blue
numbers being 0 and 1. Your function should take an integer n as input and return the nth Fibonacci
number.
Answer:
Code:
#include <iostream>
using namespace std;
int fibonacci(int n)
if (n == 0 || n == 1) {
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
int main()
int n;
cout << "Enter a positive integer n: ";
cin >> n;
cout << "The nth Fibonacci number is: " << fibonacci(n) << endl;
return 0;
Output:
Name: Anees abbasi Department: Ai-22-Blue
Program 10:
Requirements:
Title of the project:
“Finance management system”
Members:
Muhammad Ali
Abdul Rafay
Talha Mehmood kiyani
Anees abbasi
Leader:
Talha Mehmood Kiyani
Brief Explanation:
“financial management allows users to track their expenses and income. This code uses basic C++
concepts such as arrays, loops, Structure, Pointers and functions”