2024
Rayaan Ahmad
BCS243203
Rayaan Ahmad
ITP Lab-6
Practices From Home:
Code-1:-
#include<iostream>
using namespace std;
int main()
{
int n, m, ans = 1;
cout << "Enter number : ";
cin >> n;
cout << "Enter power : ";
cin >> m;
for (int i = 0; i < m; i++)
{
ans = ans * n;
}
cout << "Result : " << ans;
return 0;
}
Output:-
Code-2:-
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n, sq;
do {
cout << "Enter number : ";
cin >> n;
if (n < 0)
cout << "Number must be positive\n";
} while (n < 0);
cout << "Numbers that are perfect squares:\n";
for (int i = 0; i < n; i++)
{
sq = sqrt(i);
if (sq * sq == i)
cout << i << endl;
}
return 0;
}
Output:-
Code-3:-
#include <iostream>
using namespace std;
int main()
{
for (int i = 3; i <= 60; i += 3) {
cout << i << " ";
}
cout << endl;
return 0;
}
Output:-
Code-4:-
#include <iostream>
using namespace std;
int main()
{
float product = 1;
for (int i = 1; i <= 50; i += 2)
{
product *= i;
}
cout << "The product of all odd numbers from 1 to 50 is: " << product << endl;
return 0;
}
Output:-
Practice Tasks:
Code-1:-
#include <iostream>
using namespace std;
int main()
{
int num, even = 0, odd = 0, d7 = 0;
cout << "Enter 10 numbers:\n";
for (int i = 0; i < 10; i++)
{
cin >> num;
if (num % 2 == 0)
even++;
else
odd++;
if (num % 7 == 0)
d7++;
}
cout << "Total even numbers : " << even << endl;
cout << "Total odd numbers : " << odd << endl;
cout << "Total numbers divisible by 7 : " << d7 << endl;
return 0;
}
Output:-
Code-2:-
#include <iostream>
using namespace std;
int main()
{
int n, fac = 1;
cout << "Enter number : ";
cin >> n;
for (int i = 1; i <= n; i++)
fac *= i;
cout << "\nFactorial of " << n << " = " << fac << endl;
return 0;
}
Output:-
Code-3:-
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 4; i++)
{
if (i == 1)
cout << "****\n";
if (i == 2)
cout << "***_\n";
if (i == 3)
cout << "**__\n";
if (i == 4)
cout << "*___\n";
}
return 0;
}
Output:-
Code-4:-
#include <iostream>
using namespace std;
int main()
{
char option;
do {
int num, even = 0, odd = 0, d7 = 0;
cout << "Enter 10 numbers:\n";
for (int i = 0; i < 10; i++)
{
cin >> num;
if (num % 2 == 0)
even++;
else
odd++;
if (num % 7 == 0)
d7++;
}
cout << "Total even numbers : " << even << endl;
cout << "Total odd numbers : " << odd << endl;
cout << "Total numbers divisible by 7 : " << d7 << endl;
int n, fac = 1;
cout << "Enter number : ";
cin >> n;
for (int i = 1; i <= n; i++)
fac *= i;
cout << "\nFactorial of " << n << " = " << fac << endl;
for (int i = 1; i <= 4; i++)
{
if (i == 1)
cout << "****\n";
if (i == 2)
cout << "***_\n";
if (i == 3)
cout << "**__\n";
if (i == 4)
cout << "*___\n";
}
cout << "Do you want to continue again Y/N+\n";
cin >> option;
} while (option == 'Y' || option == 'y');
return 0;
}
Output:-
ITP Lab-7
Practices From Home:
Code-1:-
#include<iostream>
using namespace std;
int main()
{
int rows = 0;
cout << "Enter number of rows: ";
cin >> rows;
for (int i = 1; i <= rows; i = i++)
{
for (int j = i; j <= rows - 1; j++)
cout << " ";
for (int k = 1; k < 2 * i; k++)
cout << "*";
cout << endl;
}
return 0;
}
Output:-
Code-2:-
#include<iostream>
using namespace std;
int main()
{
int rows = 0;
cout << "Enter number of rows: ";
cin >> rows;
for (int i = 0; i < rows; i = i++)
{
cout << "* ";
for (int j = 0; j < i; j++)
cout << "*";
cout << endl;
}
return 0;
}
Output:-
Practice Tasks:
Code-1:-
#include<iostream>
using namespace std;
int main()
{
int rows = 0;
cout << "Enter number of rows: ";
cin >> rows;
for (int i = 1; i <= rows; i = i++)
{
for (int j = i; j <= rows - 1; j++)
cout << " ";
for (int k = 1; k < 2 * i; k++)
cout << "*";
cout << endl;
}
return 0;
}
Output:-
Code-2:-
#include<iostream>
using namespace std;
int main()
{
int n = 1;
for (int i = 1; i <= 5; i++)
{
for (int j = i; j <= 5; j++)
cout << " ";
for (int k = 1; k <= i; k++)
{
if (n <= 9)
{
cout << n << " ";
n++;
}
else
{
cout << n << " ";
n++;
}
}
cout << endl;
}
for (int i = 2; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
cout << " ";
for (int k = i; k <= 5; k++)
{
if (n <= 9)
{
cout << n << " ";
n++;
}
else
{
cout << n << " ";
n++;
}
}
cout << endl;
}
return 0;
}
Output:-
Code-3:-
#include<iostream>
using namespace std;
int main()
{
do {
int num;
bool isprime = true;
cout << "Enter number : ";
cin >> num;
if (num == -1)
return 0;
for (int i = 2; i <= num / 2; i++)
{
if (num % i == 0)
isprime = false;
}
if (num == 2)
isprime = true;
if (isprime)
cout << "Prime Number\n";
else
cout << "Not a Prime Number\n";
} while (true);
return 0;
}
Output:-
ITP Lab-8
Practices From Home:
Code-1:-
#include<iostream>
using namespace std;
int main()
{
int list[10], max;
cout << "Enter 10 numbers:\n";
for (int i = 0; i < 10; i++)
cin >> list[i];
max = list[0];
for (int i = 1; i < 10; i++)
{
if (list[i] > max)
max = list[i];
}
cout << "Maximum value: " << max;
return 0;
}
Output:-
Code-2:-
#include<iostream>
using namespace std;
int main()
{
char list[5], alpha;
cout << "Please enter 5 alphabets:\n";
for (int i = 0; i < 5; i++)
cin >> list[i];
cout << "Alphabet to be deleted:";
cin >> alpha;
for (int i = 1; i < 5; i++)
{
if (list[i] == alpha)
list[i] = NULL;
}
cout << "After Deletion Array:\n";
for (int i = 0; i < 5; i++)
cout << list[i];
return 0;
}
Output:-
Practice Tasks:
Code-1:-
#include<iostream>
using namespace std;
int main()
{
int A[20], B[20];
for (int i = 0; i < 20; i++)
B[i] = 0;
cout << "Enter values for array A:\n";
for (int i = 0; i < 20; i++)
cin >> A[i];
for (int i = 0; i < 20; i++)
{
B[i] = A[i] - 2;
B[i] *= B[i];
}
cout << "Calculated array B values:\n";
for (int i = 0; i < 20; i++)
{
cout << B[i];
if (B[i] % 2 != 0)
cout << " Is ODD";
cout << endl;
}
return 0;
}
Output:-
Code-2:-
#include<iostream>
using namespace std;
int main()
{
int A[5], temp;
cout << "Enter 5 elements in array A:\n";
for (int i = 0; i < 5; i++)
cin >> A[i];
for (int i = 0; i < 5; i++)
{
if (A[i] % 3 == 0)
A[i]++;
}
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - i - 1; j++)
{
if (A[j] > A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
cout << "Sorted Array:\n";
for (int i = 0; i < 5; i++)
cout << A[i] << " ";
return 0;
}
Output:-
Code-3:-
#include<iostream>
using namespace std;
int main()
{
int A[5], temp, option, sum = 0, diff = 0;
cout << "Enter 5 elements in array A:\n";
for (int i = 0; i < 5; i++)
cin >> A[i];
cout << "Enter 1 to sort in ascending order\nEnter 2 to sort in descending order\n";
cin >> option;
if (option == 1)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - i - 1; j++)
{
if (A[j] > A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
for (int i = 0; i < 5; i++)
{
cout << A[i] << " ";
sum += A[i];
}
cout << "\nSum : " << sum;
}
else if (option == 2)
{
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5 - i - 1; j++)
{
if (A[j] < A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
for (int i = 0; i < 5; i++)
{
cout << A[i] << " ";
diff -= A[i];
}
cout << "\nDifference : " << diff;
}
else
cout << "Invalid Option";
return 0;
}
Output:-