C PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

University of Basra

College of Computer Science

and Information Technology

Department of Information Systems

QUESTIONS AND EXERCISES


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

‫عذراء أجمد مردان‬

‫املرحلة االول‬

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>

using namespace std;

int main()

long i,n,x,sum=1;

cout<<"1+x+x^2+......+x^n";

cout<<"\n\nEnter the value of x and 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>

using namespace std;

void printSeries(int end) {

int i, a = -1, b;

for (i = 1; i <= end; i += 3) {

a *= -1;

b = i;

b *= a;

cout << b << " ";

int main() {

int end;

cout << "Enter the end of the series: ";

cin >> end;

printSeries(end);

return 0;

5
6.Write C++ Program to Print following serie
𝟏 𝟐 𝟑 𝟒
s= + + + +⋯+𝑵
𝒙𝟏 𝒙𝟐 𝒙𝟑 𝒙𝟒

#include <iostream>

#include <math.h>

using namespace std;

int main() {

int i;

float n, s = 0, x;

cout << "Enter n: ";

cin >> n;

cout << "Enter x: ";

cin >> x;

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

s = s + i / pow(x, i);

cout << s;

return 0;

6
7.Program c++ to Print a Half-Pyramid Using
*
**
***
****
*****

#include <iostream>

using namespace std;

int main() {

int rows;

cout << "Enter number of rows: ";

cin >> rows;

for(int i = 1; i <= rows; ++i) {

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

cout << "* ";

cout << "\n";

return 0;

7
8.Program c++ to Print a Half-Pyramid Using

*
***
*****
*******
*********

#include <iostream>
using namespace std;

int main() {

int space, rows;

cout <<"Enter number of rows: ";


cin >> rows;

for(int i = 1, k = 0; i <= rows; ++i, k = 0) {


for(space = 1; space <= rows-i; ++space) {
cout <<" ";
}

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() {

int rows, coef = 1;

cout << "Enter number of rows: ";


cin >> rows;

for(int i = 0; i < rows; i++) {


for(int space = 1; space <= rows-i; space++)
cout <<" ";

for(int j = 0; j <= i; j++) {


if (j == 0 || i == 0)
coef = 1;
else
coef = coef*(i-j+1)/j;

cout << coef << " ";


}
cout << endl;
}
return 0; }

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;

cout << "Enter the number of rows: ";


cin >> rows;

cout << "Enter the number of columns: ";


cin >> 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];
}
}

cout << "Even numbers: ";


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] % 2 == 0) {
cout << arr[i][j] << " ";
}} }

cout << endl;

cout << "Odd numbers: ";


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (arr[i][j] % 2 != 0) {
cout << arr[i][j] << " ";
}} }

cout << endl;

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;

cout << "Enter the number of rows: ";


cin >> rows;

cout << "Enter the number of columns: ";


cin >> 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

You might also like