c++ Programs
c++ Programs
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>
int main() {
int n;
long a = 1; // Use long long for larger factorials
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>
int main() {
int n;
long a = 0; // Use long long for larger factorials
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;
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>
int main() {
// Declare a 2D array with dimensions 4x4
int arr[4][4];
return 0;
}
14. 2d array with user input
#include <iostream>
int main() {
int rows, cols, count = 1;
int arr[rows][cols];
return 0;
}
15. 2d array with user input ,sum and average
#include <iostream>
int arr[rows][cols];
return 0;
}
#include <iostream>
int main() {
int rows, cols;
cout << "Enter the number of rows and columns for the matrices: ";
cin >> rows >> cols;
return 0;
}
#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;
return 0;
}
#include <iostream>
int main() {
int arr[] = {13, 2, 7, 6, 8, 4, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
19.sorting
#include <iostream>
int main() {
int arr[] = {13, 2, 7, 6, 8, 4, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int temp;
if(values[i] % 2 == 0)
{
continue;
}
if(values[j] % 2 == 0)
{
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
#include <iostream>
using namespace std;
int main() {
int temp;
if(values[i] % 2 == 0)
{
continue;
}
if(values[j] % 2 == 0)
{
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}