4.
swapping
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"enter two number"<<endl;
cin>>a>>b;
cout<<"Before swapping:"<<endl;
cout<<"a="<<a<<",b="<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"After Swapping:"<<endl;
cout<<"a ="<<a<<",b="<<b<<endl;
return 0;
}
5. greater
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter first integer:";
cin>>a;
cout<<"Enter second integer:";
cin>>b;
if(a>b)
{
cout<<"first interger is greater";
}
else if(a<b)
{
cout<<"second integer is greater";
}
else
cout<<"Both integers are equal";
return 0;
}
6. factorial
#include <iostream>
using namespace std;
int main() {
int n;
long a = 1; // Use long long for larger factorials
cout << "Enter any number: ";
cin >> n;
if (n < 0) {
cout << "Invalid input. Please enter a non-negative integer." << endl;
} else {
for (int i = 1; i <= n; ++i) {
a *= i;
}
cout << "The factorial is " << n << " is " << a << endl;
}
return 0;
}
7. submission
#include <iostream>
using namespace std;
int main() {
int n;
long a = 0; // Use long long for larger factorials
cout << "Enter any number: ";
cin >> n;
if (n < 0) {
cout << "Invalid input. Please enter a non-negative integer." << endl;
} else {
for (int i = 1; i <= n; ++i) {
a += i;
}
cout << "The submission is " << n << " is " << a << endl;
}
return 0;
}
8. number entered by user
#include<stdio.h>
int main()
{
int i,n;
printf("Enter any number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("%d",i);
}
return 0;
}
9. fibbonaci series
using namespace std;
int main() {
int n;
int a = 0, b = 1, c;
cout << "Enter any number: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 0; i <= n; ++i) {
cout << a << " ";
c = a + b;
a = b;
b = c;
}
cout << endl; // Add a newline at the end
return 0;
}
10. ARRAY
#include<iostream>
using namespace std;
int main()
{
string cars[4]={"audi","volvo","maruti","eicher"};
cars[0]="mahindra";
cout<<cars[1];
return 0;
}
11. sum and average of array
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter size of array";
cin>>n;
int arr[n];
cout<<"Enter the elements of array";
for(int i=0;i<n;i++)
{cin>>arr[i];
}
int sum=0;
for(int i=0;i<n;i++)
{
//sum=(sum+arr[i])/2;
sum+=arr[i];
}
cout<<"The sum of elements of array is:"<<sum<<endl;
float avg=sum/n;
cout<<"the average of elements of array is:"<<avg<<endl;
return 0;
}
12. min and max of array
13. 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16 in 2d array
#include <iostream>
using namespace std;
int main() {
// Declare a 2D array with dimensions 4x4
int arr[4][4];
// Initialize the array with numbers 1 to 16
int count = 1;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
arr[i][j] = count++; // Assign the current count value to the element
}
}
// Print the array elements
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
cout << arr[i][j] << " "; // Print each element followed by a space
}
cout << endl; // Move to the next line after each row
}
return 0;
}
14. 2d array with user input
#include <iostream>
using namespace std;
int main() {
int rows, cols, count = 1;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
int arr[rows][cols];
// Fill the array with numbers 1 to 16
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
arr[i][j] = count++;
}
}
// Print the array
cout << "\nThe 2D array is:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << arr[i][j] << " ";
}
cout << endl;
}
return 0;
}
15. 2d array with user input ,sum and average
#include <iostream>
using namespace std;
int main() {
int rows, cols, num = 1;
double sum = 0, average;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
int arr[rows][cols];
// Fill the array with numbers 1 to 16
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
arr[i][j] = num++;
sum += arr[i][j];
}
}
// Print the array
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
cout << arr[i][j] << " ";
}
cout << endl;
}
cout<<"The sum is :"<<sum<<endl;
// Calculate and print the average
average = sum / (rows * cols);
cout << "The average of all elements is: " << average << endl;
return 0;
}
16. addition of 2 matrix
#include <iostream>
using namespace std;
const int MAX_ROWS = 100; // Maximum allowed rows for matrices
const int MAX_COLS = 100; // Maximum allowed columns for matrices
void addMatrices(int mat1[][MAX_COLS], int mat2[][MAX_COLS], int result[]
[MAX_COLS], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
int main() {
int rows, cols;
cout << "Enter the number of rows and columns for the matrices: ";
cin >> rows >> cols;
int matrix1[MAX_ROWS][MAX_COLS], matrix2[MAX_ROWS][MAX_COLS], result[MAX_ROWS]
[MAX_COLS];
// Input elements for matrix1
cout << "Enter elements of matrix1:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix1[i][j];
}
}
// Input elements for matrix2
cout << "Enter elements of matrix2:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cin >> matrix2[i][j];
}
}
addMatrices(matrix1, matrix2, result, rows, cols);
// Print the result matrix
cout << "Sum of the matrices:\n";
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
cout << result[i][j] << " ";
}
cout << endl;
}
return 0;
}
17. fibonaci series using recursive function
#include<iostream>
using namespace std;
int fibonacci(int n)
{
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
int
main()
{
int n;
cout << "Enter the number of terms: ";
cin >> n;
cout << "Fibonacci Series: ";
for (int i = 0; i < n; ++i) {
cout << fibonacci(i) << " ";
}
cout << endl;
return 0;
}
18. a without using sort function
#include <iostream>
using namespace std;
int main() {
int arr[] = {13, 2, 7, 6, 8, 4, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
// Bubble sort implementation
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Print the sorted array
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
19.sorting
#include <iostream>
using namespace std;
int main() {
int arr[] = {13, 2, 7, 6, 8, 4, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
// Bubble sort implementation
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// Print the sorted array
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
20 sorting in odd numbers
#include <iostream>
using namespace std;
int main() {
const int size = 5;
cout<<"odd numbers"<<endl;
int values[size] = {1, 3, 5, 7, 9,};
int temp;
for(int i = 0; i < size; i++)
{
if(values[i] % 2 == 0)
{
continue;
}
for(int j = i + 1; j < size; j++)
{
if(values[j] % 2 == 0)
{
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
for(int i = 0; i < size; i++)
{
cout << values[i] << " ";
}
return 0;
}
21.sorting in even numbers
#include <iostream>
using namespace std;
int main() {
const int size = 5;
cout<<"even numbers"<<endl;
int values[size] = {2, 4, 6, 8, 10,};
int temp;
for(int i = 0; i < size; i++)
{
if(values[i] % 2 == 0)
{
continue;
}
for(int j = i + 1; j < size; j++)
{
if(values[j] % 2 == 0)
{
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
for(int i = 0; i < size; i++)
{
cout << values[i] << " ";
}
return 0;
}