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

C اريج

Uploaded by

bit20181210218
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

C اريج

Uploaded by

bit20181210218
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

University of Basra

College of Computer Science


and information Technology
Computer Science

‫من قبل طالبة‬

‫ل‬ ‫م‬ ‫ه‬‫م‬ ‫ي‬


‫ار ج دي و ود‬
‫المرحلة الاولى‬

2024
1.Write a program to read integer and find its factorial where
n!= 1*2*….* n

#include <iostream>
using namespace std;
int main ()
{
int n,i,fact=1;
cout << "Enter any number :" ;
cin >> n;
if (n>0)
{
for (i = 1 ; i <= n ; i++) fact *= i;
cout << "Factorial of " << n << " = "<< fact;
}
else cout << "Error! factorial of a negative numberdoesn't exist";
return 0;
}

| C++

1
2.Write a program to prints the summation of the following
series(for n terms)
𝟏 𝟐 𝟑 𝟒 𝟏𝟎
𝒔 = + + + + ⋯+
𝟐 𝟒 𝟔 𝟖 𝟏𝟐

#include <iostream>
using namespace std;
int main ()
{
cout << "enter n terms :";
float n,s=0,i=1,j=2;
cin >> n;
while (i<=n)
{
s += i/j;
j+=2;
i++;
}
cout << "S = " << s;
return 0;
}

| C++

2
3.Write a program to prints the summation of the following
series(for n terms)
𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒
𝑺= + + + + ⋯+ 𝑵
𝟐! 𝟒! 𝟔! 𝟖!

#include <iostream>
#include <math.h>
using namespace std;
int main()
{ int j, i;
float n, s=0 ,f, x;
cout<<"enter n";
cin>>n;
cout<<"enter x";
cin>>x;
for( i =1 ; i <= n; i++)
{ f=1;
for( j = 1 ; j < = 2 * i ; j++)
f=f*j;
s = s + pow ( x , i) / f ; }
cout<<s;
return 0;}
| C++

3
4.write a program which print the pattern
11111
22222
33333

#include <iostream>
using namespace std;
int main()
{
for (int i=1 ;i<=3;i++)
{
for (int j=0 ; j<5 ;j++)
cout << i << " " ;
cout << endl;
}
return 0;
}

5.write a program which print the pattern


1
22
333
4444

#include <iostream>
using namespace std;
int main()
{
for (int i=1 ;i<5;i++)
{
for (int j=0 ; j<i ;j++)
cout << i << " " ;
cout << endl;
}
return 0;

}
| C++

4
6.write a program which print the pattern
****
***
**
*

#include <iostream>
using namespace std;
int main()
{
for (int i=5 ;i>1;i--)
{
for (int j=1 ; j<i ;j++)
cout << "*" << " " ;
cout << endl;
}
return 0;
}

| C++

5
7. uses a nested for loop to find the prime numbers from 2 to 100 .
)Using a function(

#include <iostream>
using namespace std;

void printPrimes() {
for (int i = 2; i < 100; i++) {
int j;
int k = i / j;
for (j = 2; j <= k; j++) {
int x = i % j;
if (!x) break;
}
if (j > (i / j))
cout << i << " is prime\n";
}
}

int main() {
printPrimes();
return 0;
}
| C++

6
8.Write a program that read number of three digit and separate its
(units , tens , hundreds ) digit , )Using a function(
example:
345 units=5 tens=40 hundreds=300
401 units= 1 tens=00 hundreds=400
45 units= 5 tens=40
7 units=7

#include <iostream>
using namespace std;

void analyzeNumber() {
int n ,h,s,a;
cout << "Enter any 3 number :";
cin >> n ;
h = n / 100;
a = n % 100 / 10;
s = n % 10;
cout << n << "\tunits= " << s << "\ttens= " << a * 10 << "\thundreds= "
<< h * 100 << endl;
}

int main() {
analyzeNumber();
return 0;
}
| C++

7
9.Write a program that read number of three digit and check if
(units , tens , hundreds ) digit is )Using a function(
greater than 5 , covert this digit to 5 .
Example:
147 after check 145
983 after check 553

#include <iostream>
using namespace std;

void limitDigits() {
int n ,h,s,a;
cout << "Enter any 3 number :";
cin >> n ;
h = n / 100;
a = n % 100 / 10;
s = n % 10;
if (h > 5) h = 5;
if (a > 5) a = 5;
if (s > 5) s = 5;
cout << h << a << s;
}

int main() {
limitDigits();
return 0;
}
| C++

8
10.Write a program that reads an array of 7 numbers, then selects
the largest element in this array and prints it.

#include <iostream>
using namespace std;

int main() {
int i, array1[7], max;

cout << "Enter 7 numbers: ";


for (i = 0; i < 7; i++)
cin >> array1[i];

max = array1[0];
for (i = 0; i < 7; i++) {
if (array1[i] > max)
max = array1[i];
}

cout << "Max number in array1 is= " << max;

return 0;
}
| C++

9
11.Write a program to arrange the elements of an array entered by a
user in ascending order

#include <iostream>
using namespace std;

int main() {
int array[5], i, j, item;

cout << " Array before sorting, enter number :\n";


for (i = 0; i < 5; i++)
cin >> array[i];

for (i = 0; i < 5 - 1; i++) {


for (j = i; j < 5; j++) {
if (array[j] < array[i]) {
item = array[j];
array[j] = array[i];
array[i] = item;
}
}
}

cout << " Array after sorting:\n";


for (i = 0; i < 5; i++)
cout << array[i] << "\n";

return 0;
| C++

10
12.Write a program to determine the max and min element in an
array

#include <iostream>
using namespace std;

int main() {
const int SIZE = 6;
int array[SIZE];
int min, max;

cout << "Please enter " << SIZE << " elements for the array:\n";
for (int i = 0; i < SIZE; ++i) {
cout << "Element " << i+1 << ": ";
cin >> array[i];
}
min = max = array[0];
for (int i = 1; i < SIZE; ++i) {
if (array[i] < min)
min = array[i];
if (array[i] > max)
max = array[i];
}

cout << "Minimum element in the array: " << min << endl;
cout << "Maximum element in the array: " << max << endl;

return 0;
| C++

11
13.Write a program to print major diagonal elements in Matrix 2
Dimensional

#include <iostream>
using namespace std;

int main() {
int i, j;
int a[3][3];

cout << "Enter 5x5 elements for the array:\n";


for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
cin >> a[i][j];

cout << "Diagonal elements of the array:\n";


for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
if (i == j)
cout << a[i][j] << "\n";

return 0;
}
| C++

12
14.Write a program to arrange the elements of 2 dimensional arrays
in descending order

#include <iostream>
using namespace std;

int main() {
const int ROWS = 3;
const int COLS = 3;
int array[ROWS][COLS];
cout << "Enter " << ROWS * COLS << " elements for the 2D array:\n";
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> array[i][j]; } }
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
for (int k = 0; k < ROWS; ++k) {
for (int l = 0; l < COLS; ++l) {
if (array[i][j] > array[k][l]) {
int temp = array[i][j];
array[i][j] = array[k][l];
array[k][l] = temp; } } } } }
cout << "Sorted array in descending order:\n";
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
cout << array[i][j] << " ";
}
| C++

cout << endl; } return 0; }

13
15.Write a program to find the largest and smallest elements in a
2 dimensional array

#include <iostream>
using namespace std;

int main() {
const int ROWS = 3;
const int COLS = 3;
int array[ROWS][COLS];

cout << "Enter " << ROWS * COLS << " elements for the 2D array:\n";
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
cout << "Element [" << i << "][" << j << "]: ";
cin >> array[i][j]; }
}
int min = array[0][0];
int max = array[0][0];
for (int i = 0; i < ROWS; ++i) {
for (int j = 0; j < COLS; ++j) {
if (array[i][j] < min)
min = array[i][j];
if (array[i][j] > max)
max = array[i][j]; } }
cout << "Smallest element in the array: " << min << endl;
cout << "Largest element in the array: " << max << endl;
| C++

return 0; }

14

You might also like