Coding Questions IN C++
Coding Questions IN C++
C++
//Reverse of a number
#include
using namespace std;
//main program
int main()
{
//variables initialization
int num, reverse=0, rem;
cout<<"Enter a number: ";
//user input
cin>>num;
//loop to find reverse number
do
{
rem=num%10;
reverse=reverse*10+rem;
num/=10;
}while(num!=0);
//output
cout<<"Reversed Number: "<<reverse;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int num = 15;
int a = 0, b = 1;
int nextTerm;
return 0;
}
#include
using namespace std;
//main Program
int main ()
{
int div, num, sum=0;
cout << "Enter the number to check : ";
//user input
cin >> num;
//loop to find the sum of divisors
for(int i=1; i < num; i++)
{
div = num % i;
if(div == 0)
sum += i;
}
//checking for perfect number
if (sum == num)
cout<< num <<" is a perfect number.";
else
cout<< num <<" is not a perfect number.";
return 0;
}
//Accepting inputs.
cout<<"Enter First String: ";
gets(str1);
cout<<"Enter Second String: ";
gets(str2);
c=0;
//Calculating frequencies of characters in second string.
while(str2[c] != '\0')
{
second[str2[c]-'a']++;
c++;
}
//Checking if frequencies of both the strings are same or not.
for(c=0;c<26;c++)
{
if(first[c] != second[c])
flag=1;
}
//Priting result.
if(flag == 0)
{
cout<<"Strings are anagram.";
}
else
{
cout<<"Strings are not anagram.";
}
return 0;
#include
#include
using namespace std;
int main()
{
//Initializing variable.
char str[100];
int i,length=0,flag=0;
//Accepting input.
cout<<"Enter the string : "<<endl;
gets(str);
length=strlen(str);
}
//Printing result.
if(flag==i)
cout<<"String entered is palindrome";
else
cout<<"String entered is not palindrome";
return 0;
}
#include
using namespace std;
int main()
{
//Initializing variables.
char str[100];
int i;
int freq[256] = {0};
//Accepting inputs.
cout<<"Enter the string: ";
gets(str);
int main()
{
//Initialize the variables.
string wild,str;
bool TRUE=true,FALSE=false;
bool check[wild.length()+1][str.length()+1];
check[0][0]=TRUE;
for(int i=1;i<=str.length();i++)
check[0][i]=FALSE;
for(int i=1;i<=wild.length();i++)
check[i][0]=check[i-1][0];
else
check[i][0]=FALSE;
for(int i=1;i<=wild.length();i++)
for(int j=1;j<=wild.length();j++)
if(wild[i-1] == str[j-1])
check[i][j]=check[i-1][j-1];
check[i][j]=check[i-1][j-1];
check[i][j]=check[i-1][j]||check[i][j-1];
else
check[i][j] =FALSE;
//Printing result
if(check[wild.length()][str.length()])
cout<<"TRUE";
else
cout<<"FALSE</span.";
cout<<endl;
}
//Main function to run the program.
int main()
{
int array[] = {5, 3, 1, 9, 8, 2, 4,7};
int size = sizeof(array)/sizeof(array[0]);
bubbleSort(array, size);
return 0;
}
void mergeSort(int[],int,int);
void merge(int[],int,int,int);
int main()
{
int array[]= {8, 4, 5, 1, 3, 9, 0, 2, 7, 6};
int i;
mergeSort(array, 0, size-1);
printArray(array, size);
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
int year;
if(year % 400 == 0)
cout << year << " is a Leap Year";
else
cout << year << " is not a Leap Year";
return 0;
}
#include <iostream>
using namespace std;
int main()
{
//Initializing variables.
char str[100]="prepinsta";
int i;
int freq[256] = {0};
int main()
{
int arr[] = {1, 14, 3, 7, 0};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Unsorted array \n";
display(arr, n);
heapSort(arr, n);
#include<bits/stdc++.h>
using namespace std;
int main(){
int arr[] = { 100, 2, 70, 12 , 90};
int n = sizeof(arr) / sizeof(arr[0]);
int temp[n];
for(int i=0; i<n; i++)
temp[i] = arr[i];
#include <bits/stdc++.h>
using namespace std;
int main()
{
return 0;
#include <bits/stdc++.h>
using namespace std;
if(visited[i]!=1){
int count = 1;
for(int j=i+1; j<n; j++){
if(arr[i]==arr[j]){
count++;
visited[j]=1;
}
}
if(count==1)
cout<<arr[i]<<" ";
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
while(temp>0){
int rem = temp%10;
rev = rev*10 + rem;
temp /= 10;
}
if(n==rev)
return 1;
return 0;
}
int main(){
int arr[] = {1, 121, 55551, 545545, 10111, 90};
int n = sizeof(arr)/sizeof(arr[0]);
int res = INT_MIN;
if(res==INT_MIN)
res = -1;
cout<<res;
}
#include<iostream>
using namespace std;
int main ()
{
int num = 6, fact = 1;
cout << "Fact " << num << ": " << fact;
}
// Time complexity: O(N)
// Space complexity: O(1)
#include<iostream>
#include<math.h>
using namespace std;
// Driver Code
int main ()
{
//variables initialization
int num = 407, len;
// check if Armstrong
if (armstrong(num, len))
cout << num << " is armstrong";
else
cout << num << " is not armstrong";
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int getSum(int n)
{
if(n==0)
return n;
return n + getSum(n-1);
}
int main()
{
int n;
cout << "Enter a number : ";
cin >> n;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int getSum(int n)
{
if(n==0)
return n;
return n + getSum(n-1);
}
int main()
{
int n;
cout << "Enter a number : ";
cin >> n;
return 0;
}
#include
#include
int main()
cout << "Enter the string : "; gets(str1); len = strlen(str1) - 1; for (i = len, j = 0; i >= 0 ; i--, j++)
str2[j] = str1[i];
if (strcmp(str1, str2))
flag = 1;
if (flag == 1)
else
return 0;
//C++ Program
//Convert binary to decimal
#include
#include using namespace std;
//function to convert binary to decimal
int convert(long n)
{
int i = 0,decimal= 0;
//converting binary to decimal
while (n!=0)
{
int rem = n%10;
n /= 10;
int res = rem * pow(2,i);
decimal += res;
i++;
}
return decimal;
}
//main program
int main()
{
long binary;
cout << "Enter binary number: ";
cin >> binary;
cout << binary << " in binary = " << convert(binary) << " in decimal";
return 0;
}
//main program
int main()
{
char val;
cout<<"Enter a character: ";
cin>>val;
#include <iostream>
using namespace std;
int main()
{
//Initializing variable.
char str[100];
int i, j;
//Accepting input.
cout<<"Enter a string : ";
gets(str);
int fibo(int n)
{
if (n <= 1)
return n;
return fibo(n-1) + fibo(n-2);
}
int main ()
{
int n = 9;
cout << fibo(n);
getchar();
return 0;
}