C PDF
C PDF
C PDF
املرحلة االول
2024
1.Write a c++ Program to accept any single digit number
and print it in words. Using functions
#include <iostream>
using namespace std;
void printNumberName(int n) {
switch(n) {
case 0: cout<<"ZERO"; break;
case 1: cout<<"ONE"; break;
case 2: cout<<"TWO"; break;
case 3: cout<<"THREE"; break;
case 4: cout<<"FOUR"; break;
case 5: cout<<"FIVE"; break;
case 6: cout<<"SIX"; break;
case 7: cout<<"SEVEN"; break;
case 8: cout<<"EIGHT"; break;
case 9: cout<<"NINE"; break;
default: cout<<"please enter the number between 0 and 9";
}
}
int main() {
int n;
cout << "enter a number :";
cin >> n;
printNumberName(n);
return 0;
}
1
2.Write C++ program to find the largest number of a list of
Numbers entered through keyboard using functions
#include<iostream>
#include<conio.h>
using namespace std;
int findLargestNumber() {
int i, n, x, large = 0;
cout << "How many numbers?";
cin >> n;
for (i = 0; i < n; ++i) {
cout << "\nEnter number " << i + 1 << ":";
cin >> x;
if (x > large)
large = x;
}
return large;
}
int main() {
int largest = findLargestNumber();
cout << "\n\nThe largest number is " << largest;
return 0;
}
2
3.Write C++ Program to calculate and print the sum of even
and odd integers of the first n natural numbers. Using functions
#include<iostream>
using namespace std;
void calculateSumOfEvenAndOdd(int n) {
int i, sumeven = 0, sumodd = 0;
cout << "Numbers: ";
for (i = 1; i <= n; ++i) {
cout << i << " ";
if (i % 2 == 0)
sumeven += i;
else
sumodd += i;
}
cout << "\nSum of even Numbers is " << sumeven;
cout << "\nSum of odd Numbers is " << sumodd;
}
int main() {
int n;
cout << "Enter value of n:";
cin >> n;
calculateSumOfEvenAndOdd(n);
return 0;
}
3
4.Write C++ program to find sum of series
1+x+x^2+......+x^n
#include<iostream>
#include<conio.h>
#include<math.h>
int main()
long i,n,x,sum=1;
cout<<"1+x+x^2+......+x^n";
cin>>x>>n;
for(i=1;i<=n;++i)
sum+=pow(x,i);
cout<<"\nSum="<<sum;
return 0;
4
5.Write C++ Program to Print following series: 1 -4 7 10..........-40…..N
#include<iostream>
int i, a = -1, b;
a *= -1;
b = i;
b *= a;
int main() {
int end;
printSeries(end);
return 0;
5
6.Write C++ Program to Print following serie
𝟏 𝟐 𝟑 𝟒
s= + + + +⋯+𝑵
𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒
#include <iostream>
#include <math.h>
int main() {
int i;
float n, s = 0, x;
cin >> n;
cin >> x;
s = s + i / pow(x, i);
cout << s;
return 0;
6
7.Program c++ to Print a Half-Pyramid Using
*
**
***
****
*****
#include <iostream>
int main() {
int rows;
return 0;
7
8.Program c++ to Print a Half-Pyramid Using
*
***
*****
*******
*********
#include <iostream>
using namespace std;
int main() {
while(k != 2*i-1) {
cout << "* ";
++k;
}
cout << endl;
}
return 0;
}
8
9.Program c++ to Print a Half-Pyramid Using
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
#include <iostream>
using namespace std;
int main() {
9
10.Write a C++ program to sort a list of numbers in
ascending order.
#include<iostream>
using namespace std;
int main( )
{
int a[10], n, i, j, temp;
cout<<"Enter the no. of elements:";
cin>> n;
cout<<"Enter the array elements:";
for(i=0; i< n; i++)
cin>>a[i];
for( i=0; i< n; i++)
{
for(j=i; j< n-1; j++)
{
if(a[i]> a[j+1] )
{
temp= a[i];
a[i]= a[j+1];
a[j+1]= temp;
}
}
}
cout<<"Elements after sorting:";
for( i=0; i< n; i++)
cout<< a[i]<<" ";
return 0;
}
10
11.Write a C++ program to find both the largest and smallest
number in a array of integers
#include<iostream>
using namespace std;
int main()
{
int a[100], max, min, i, n;
cout<<"Enter number elements in the array? :";
cin>>n;
for( i=0; i< n ; i++)
{
cout<<"Enter the numbers: ";
cin>>a[ i ];
if(i==0)
{ max=a[i ];
min=a[i ]; }
if(a[ i]>max)
max= a[i ];
if(a[i ]< min)
min= a[i ];
}
cout<<"Maximum : "<< max<<endl;
cout<<"Minimum : "<< min;
getch( );
}
11
12.Write a C++ program to insert an element in an array
#include<iostream>
using namespace std;
int main()
{
int a[20],n,x,i,pos=0;
cout<<"Enter size of array:";
cin>>n;
cout<<"Enter the array in ascending order:\n";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nEnter element to insert:";
cin>>x;
for(i=0;i<n;++i)
if(a[i]<=x&&x<a[i+1])
{
pos=i+1;
break;
}
for(i=n+1;i>pos;--i)
a[i]=a[i-1];
a[pos]=x;
cout<<"\n\nArray after inserting element:";
for(i=0;i<n+1;i++)
cout<<a[i]<<" ";
return 0;
}
12
13.Write a C++ program to find largest and second largest no from a 2D
array.
#include<iostream>
using namespace std;
int main()
{
int a[3][3],big1,big2,n,m,i,j;
cout<<"Enter no of rows and columns(max 3):";
cin>>m>>n;
cout<<"Enter the array:\n";
for(i=0;i<m;i++)
for(j=0;j<n;++j)
cin>>a[i][j];
big1=a[0][0];
for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
if(a[i][j]>big1)
big1=a[i][j];
}
big2=a[0][0];
for(i=0;i<m;++i)
for(j=0;j<n;++j)
{
if(a[i][j]>big2&&a[i][j]<big1)
big2=a[i][j]; }
cout<<"\n\nLargest number:"<<big1;
cout<<"\nSecond largest number:"<<big2;
return 0; }
13
14. program to extract even and odd numbers from a 2D array.
#include <iostream>
using namespace std;
int main() {
int rows, cols;
int arr[rows][cols];
cout << "Enter " << rows * cols << " numbers:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
}
return 0; }
14
15. program to calculate the sum of the elements of a 2D array .
#include <iostream>
using namespace std;
int main() {
int rows, cols;
int arr[rows][cols];
cout << "Enter " << rows * cols << " numbers:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> arr[i][j];
}
}
int sum = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
sum += arr[i][j];
}
}
cout << "Sum of all elements in the array: " << sum << endl;
return 0;
}
15