You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#include<bits/stdc++.h>usingnamespacestd;voidinsertionSort(int array[],int n){
for(int i=1;i<n;i++){
int current=array[i];
int j=i-1;
while(array[j]>current&&j>=0){
array[j+1]=array[j];
j--;
}
array[j+1]=current;
}
return;
}
int32_tmain(){
int n;
cin>>n;
int array[n];
for(int i=0; i<n; i++){
cin>>array[i];
}
insertionSort(array,n);
for(int i=0; i<n; i++){
cout<<array[i]<<"";
}
return0;
}
Chocolate Riddle
#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){
int money;
cout<<"How much money you have:"<<endl;
cin>>money;
int chocolate_count=0;
int wrapper_count=0;
for(int i=1;i<=money;i++){
chocolate_count++;
}
int chocolates=chocolate_count;
while(chocolate_count>=3){
wrapper_count+=chocolate_count/3;
chocolate_count/=3;
if(wrapper_count>=3&&wrapper_count<6){ // we have to increse the chocolate count only when we have less than 6 chocolates
chocolate_count++; // and greater than or equal to 3.
}
}
chocolates+=wrapper_count;
cout<<chocolates;
return0;
}
Selection Sort
#include<bits/stdc++.h>usingnamespacestd;voidselectionSort(int array[], int n){
for(int i = 0; i < n-1;i++){
for(int j=i+1; j < n; j++){
if(array[i] > array[j]) {
swap(array[i],array[j]);
}
}
}
return;
}
int32_tmain(){
int n;
cin>>n;
int array[n];
for(int i=0; i<n; i++){
cin>>array[i];
}
selectionSort(array, n);
for(int i=0; i<n; i++){
cout<<array[i]<<"";
}
return0;
}
Linear Search
#include<bits/stdc++.h>usingnamespacestd;intlinearSearch(int array[], int n, int key){
for(int i=0;i<n;i++){
if(array[i]==key){
return i;
}
}
return -1;
}
int32_tmain(){
int n;
cin>>n;
int array[n];
for(int i=0;i<n;i++){
cin>>array[i];
}
int key;
cin>>key;
cout<<linearSearch(array,n,key)<<endl;
return0;
}
Binary Search
#include<bits/stdc++.h>usingnamespacestd;intbinarySearch(int array[],int n,int key){
int start = 0;
int end=n;
while(start<=end){
int mid=(start+end)/2;
if(array[mid]==key){
return mid;
}
elseif(array[mid]>key){
end=mid-1;
}else{
start=mid+1;
}
}
return -1;
}
int32_tmain(){
int n;
cin>>n;
int array[n];
for(int i=0;i<n;i++){
cin>>array[i];
}
int key;
cin>>key;
cout<<binarySearch(array,n,key);
return0;
}
Program to declare array
#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){
int Marvel_mem[5]={5,6,7,4,3};
cout<<Marvel_mem[0];
}
Program to take take input from user and then make array
#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){
int size;
cout<<"eneter the size of the array"<<endl;
cin>>size;
int array[size];
for(int i=0; i<size; i++){
cin>>array[i];
cout<<array[i];
}
for(int i=0; i<size; i++){ // another way
cout<<array[i]<<"";
}
}
Program to take input of an array of size n and then find maximum and minimum values
#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){
int n;
cin>>n;
int array[n];
for(int i=0; i<n; i++){
cin>>array[i];
}
int maximum=INT32_MIN;
int minimum=INT32_MAX;
for(int i=0; i<n; i++){
maximum=max(maximum,array[i]);
minimum=min(minimum,array[i]);
}
cout<<"Max no.="<<maximum<<endl;
cout<<"Min no.="<<minimum<<endl;
}
Program to print Kids With the Greatest Number of Candies
#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){
int n,i;
cin>>n;
int array[n];
for(i=0; i<n; i++){
cin>>array[i];
}
int extracandidate;
cin>>extracandidate;
int maximum=INT32_MIN;
for(i=0; i<n; i++){
maximum=max(maximum,array[i]);
}
for(i=0; i<n; i++){
if(array[i]+extracandidate>=maximum){
cout<<"true"<<endl;
}else{
cout<<"false"<<endl;
}
}
return0;
}
Program to print Running Sum of 1d Array
#include<bits/stdc++.h>usingnamespacestd;// Driver codeintmain()
{
int n;
cin>>n;
int array[n];
int sum=0;
for(int i=0; i<n; i++){
cin>>array[i];
sum+=array[i];
cout<<sum<<endl;
}
return0;
}
#include<bits/stdc++.h>usingnamespacestd;intsumOfNums(int n) {
int sum;
sum=(n*(n+1))/2;
return sum;
}
int32_tmain(){
int num;
cin>>num;
cout<<sumOfNums(num);
return0;
}
Check whether the given triplet is pyhagorian triplet or not
#include<bits/stdc++.h>usingnamespacestd;intmaximumNumber(int a, int b, int c){
int max;
if(a>b&&a>c){
max=a;
}if(b>a&&b>c){
max=b;
}if(c>a&&c>b){
max=c;
}
return max;
}
boolcheck(int a, int b,int c){
int x= max(a,max(b,c));// max function is inbuilt function which can find maximum among two numbers//int x= maximumNumber(a,b,c); You can also use this function to call above one int y,z;
if(x==a){
y=b;
z=c;
}elseif(x==b){
y=a;
z=c;
}else{
y=b;
z=a;
}
if(pow(x,2)==pow(y,2)+pow(z,2)){
returntrue;
}else{
returnfalse;
}
}
intmain(){
int a,b,c;
cin>>a>>b>>c;
if(check(a,b,c)){
cout<<"Pythagorian triplet"<<endl;
}else{
cout<<"Not pythagorian triplet"<<endl;
}
return0;
}
Octal number to decimal number
#include<bits/stdc++.h>usingnamespacestd;intOctalToBinary(int num){
int new_num=0;
int count=0;
while(num>0){
intremainder = num%10;
new_num = (remainder*pow(8,count))+new_num;
num/=10;
count++;
}
return new_num;
}
int32_tmain(){
int n;
cin>>n;
cout<<OctalToBinary(n);
return0;
}
Decimal to octal representation
#include<bits/stdc++.h>usingnamespacestd;intDecimalToOctal(int num){
int new_num=0;
int count=1;
while(count<=num){
count*=8;
}count/=8;
while(count>0){
int lastdigit=num/count;
num=num-lastdigit*count;
count/=8;
new_num=(new_num*10)+lastdigit;
}
return new_num;
}
int32_tmain(){
int n;
cin>>n;
cout<<DecimalToOctal(n);
return0;
}
program to print the factorial of two numbers using functioms
#include<bits/stdc++.h>usingnamespacestd;intfactorial(int num){
int fact=1;
for(int i=num;i>=1;i--){
fact=fact*i;
}
return fact;
}
int32_tmain(){
int n1,n2;
cin>>n;
cout<<"Factorial of "<<n1<<"="<<factorial(n1);
cout<<"Factorial of "<<n2<<"="<<factorial(n2);
return0;
}
Factorial
#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){
int n;
cin>>n;
int fact=1;
for(int i=1;i<=n;i++){
fact=fact*i;
}
cout<<fact;
return0;
}
Binary to Decimal
#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){
int n;
cout<<"Enter Only Binary number to convert it into Decimal"<<endl;
cin>>n;
int count=0;
int sum=0;
while(n>0){
int lastdigit=n%10;
sum+=pow(2,count)*lastdigit;
n=n/10;
count++;
}
cout<<sum<<endl;
return0;
}
Revese a number
#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){
int n;
cin>>n;
int reverse=0;
while (n>0)
{
int lastdigit=n%10;
reverse=reverse*10 + lastdigit;
n=n/10;
}
cout<<reverse<<endl;
return0;
}
Armstrong or not
#include<bits/stdc++.h>usingnamespacestd;int32_tmain(){
int n;
cin>>n;
int original_num=n;
// code to check the length of numberint count=0;
while (original_num>0){
original_num=original_num/10;
count++;
}
original_num=n; // Origianl number is reassigned to nint sum=0;
while(n>0){
int lastdigit=n%10;
sum+=(pow(lastdigit,count));
n=n/10;
}
if(sum==original_num){
cout<<"Armstrong";
}else{
cout<<"Not an armstrong";
}
return0;
}