Showing posts with label reverse. Show all posts
Showing posts with label reverse. Show all posts

C program to convert Decimal to binary, binary to decimal, reverse a number, pallindrom check

Decimal to Binary
//C program to convert decimal to binary
#include<iostream>
using namespace std;

int main()
{
cout<<"manin()\n";
int num;
cout<<"Enter a decimal number:";//13
cin>>num;
int n,r,i=1;
unsigned long s=0;
n=num;
while(n>0)
{
r=n%2;//13%2=1 //6%2=0 //3%2=1 //1%2=1//
n=n/2;//13/2=6 //6/2=3 //3/2=1 //1/2=0
s=s+r*i;//0+1*1=1 //1+0*10=1 //1+1*100=101 //101+1*1000=1101
i=i*10;//1*10=10//10*10=100 //100*10=1000 //10000
cout<<"r="<<r<<" n="<<n<<" s="<<s<<" i="<<i<<endl;
}
cout<<"Decimal number "<<num<<" in binary is: "<<s<<endl;//1101

return 0;
}
/*
Enter a decimal number:13
r=1 n=6 s=1 i=10
r=0 n=3 s=1 i=100
r=1 n=1 s=101 i=1000
r=1 n=0 s=1101 i=10000
Decimal number 13in binary is: 1101
*/
//Binary to Decimal
#include<iostream>
using namespace std;
#include<math.h>

int main()
{
cout<<"manin()\n";
int num;
cout<<"Enter a binary number:";//1101=13
cin>>num;
int n,r,i=0;
unsigned long s=0;
n=num;
while(n>0)
{
r=n%10;//
n=n/10;//
s=s+r*pow(2,i);//
++i;
cout<<"r="<<r<<" n="<<n<<" s="<<s<<" i="<<i<<endl;
}
cout<<"binary number "<<num<<" in Decimal is: "<<s<<endl;//1101

return 0;
}
/*
manin()
Enter a binary number:1101
r=1 n=110 s=1 i=1
r=0 n=11 s=1 i=2
r=1 n=1 s=5 i=3
r=1 n=0 s=13 i=4
binary number 1101 in Decimal is: 13
*/
//Sum of digits of a numbers
#include<iostream>
using namespace std;

int main()
{
cout<<"manin()\n";
int num,n,r,sum=0;
cout<<"Enter a number:";
cin>>num;//1234
n=num;
while(n>0)
{
r=n%10;//4//3//2//1%10=1
n=n/10;//123//12//1//1/10=0
sum=sum+r;//4//4+3//4+3+2//4+3+2+1=10
}
cout<<"sum of "<<num<<" is "<<sum<<endl;//10
return 0;
}
//Reverse Number
#include<iostream>
using namespace std;

int main()
{
cout<<"manin()\n";
int num,n,r,sum=0;
cout<<"Enter a number:";
cin>>num;//1234
n=num;
while(n>0)
{
r=n%10;//4//3//2//1%10=1
n=n/10;//123//12//1// 1/10=0
sum=sum*10+r;//0*10+4=4//4*10+3=40+3=43//43*10+2=430+2=432//432*10+1=4320+1=4321
}
cout<<"sum of "<<num<<" is "<<sum<<endl;//4321
return 0;
}
//Pallindrom number
#include<iostream>
using namespace std;

int main()
{
cout<<"manin()\n";
int num,n,r,sum=0;
cout<<"Enter a number:";
cin>>num;//1234
n=num;
while(n>0)
{
r=n%10;//4//3//2//1%10=1
n=n/10;//123//12//1// 1/10=0
sum=sum*10+r;//0*10+4=4//4*10+3=40+3=43//43*10+2=430+2=432//432*10+1=4320+1=4321
}
if(num==sum)
{
cout<<"Its a pallindrom "<<num<<" is equal to "<<sum<<endl;//4321
}
else
{
cout<<"Not a pallindrom.\n";
}
return 0;
}
//Reversing String
#include<iostream>
using namespace std;
#include<string.h>
void strRev(char *arr);

int main()
{
cout<<"manin()\n";
char arr[]="Hello";
///*
int result;
char *end;
char *start;
end=arr;
start=arr;
int l,i;
char t;
l=strlen(arr);
for(i=0;i<l-1;i++)
end++;
for(i=0;i<=l/2;i++)
{
t=*start;
*start=*end;
*end=t;
start++;
end--;
}
//*/
cout<<"reversed string is:"<<arr<<endl;
cout<<"\nAgain reversing using function:\n";
strRev(arr);
cout<<"arr is now again: "<<arr<<endl;
//cout<<"Reverse string is: "<<strRev(arr)<<endl;
return 0;
}
void strRev(char *arr)
{
char *start=arr;
char *end=arr;
int l,i;
char t;
l=strlen(arr);
for(i=0;i<l-1;i++)
end++;
for(i=0;i<l/2;i++)
{
t=*start;
*start=*end;
*end=t;
start++;
end--;
}
}
/*
manin()
reversed string is:olleH
Again reversing using function:
arr is now again: Hello
*/

Number programs in C C++ asked in interviews

//C/CPP/C++ linux code to find factorial of a given number
#include <iostream>
using namespace std;
int main()
{
int n,i=1,f=1;
cout<<"Enter a number whose factorial is required:"<<endl;
cin>>n;//5
while(i<=n)
{
f=f*i;
i++;
}
cout<<"factorial is:"<<f<<endl;
return 0;
}
//C/CPP/C++ linux code to find fibonacci series
#include <iostream>
using namespace std;
int main()
{
int n,first=0,second=1,next;
cout<<"Enter upto which fibonacci needed:"<<endl;
cin>>n;
cout<<first<<"\t"<<second<<"\t";
for(int i=0;i<n;i++)
{
next=first+second;
first=second;
second=next;
cout<<next<<"\t";
}
cout<<endl;
return 0;
}
//C/CPP/C++ linux code to check given number is palindrom or not
////C/CPP/C++ linux code to find sum of digits of given number
#include <iostream>
using namespace std;
int main()
{
int n,r,s=0;
cout<<"Enter a number whose digits needs to be added:"<<endl;
cin>>n;//123
int temp=n;
while(n)
{
r=n%10;
n=n/10;
s=s*10+r;
}
cout<<"revers of "<<temp<<" is: "<<s<<endl;//6
if(s==temp)
cout<<"Yes "<<temp<<" and "<<s<<" are palindrome"<<endl;
else
cout<<"No "<<temp<<" and "<<s<<" are not palindrome"<<endl;
return 0;
}
//C/CPP/C++ linux code to check given number is prime or not
#include<iostream>
using namespace std;
int main()
{
int n,i,count=0;
cout<<"Enter a number to check its prime or not?"<<endl;
cin>>n;
cout<<n<<endl;
for(i=2;i<=n/2;i++)
{
if(n%i==0)
{
count++;
break;
}
}
if(count>0)
cout<<"Not prime"<<endl;
else
cout<<"prime"<<endl;

return 0;
}
//C/CPP/C++ linux code to find prime numbers in range
//prime in series
#include<iostream>
using namespace std;
int main()
{
int n,i,count,min,max;
cout<<"Enter a min number of range to check prime:"<<endl;
cin>>min;
cout<<"Enter the max number of range to check prime:"<<endl;
cin>>max;
for(n=min;n<=max;n++)
{
count=0;
    for(i=2;i<=n/2;i++)
    {
        if(n%i==0)
        {
        count++;
        break;
    }
}
if(count==0 && n!=1 && n!=0)
cout<<n<<endl;
//else
//cout<<"Not a prime"<<endl;
}
return 0;
}
//C/CPP/C++ linux code to reverse digits of a number
#include <iostream>
using namespace std;
int main()
{
int n,r,s=0;
cout<<"Enter a number:"<<endl;
cin>>n;//123
int temp=n;
while(n)
{
r=n%10;
n=n/10;
s=s*10+r;
}
cout<<"revers n "<<temp<<"is:"<<s<<endl;//6
return 0;
}
//C/CPP/C++ linux code to add digits of number
#include <iostream>
using namespace std;
int main()
{
int n,r,s=0;
cout<<"Enter a number whose needs to be added:"<<endl;
cin>>n;//123
while(n)
{
r=n%10;
n=n/10;
s=s+r;
}
cout<<"Sum of digits of n "<<n<<"is:"<<s<<endl;//6
return 0;
}
//C/CPP/C++ linux code to find a year is leap year or not
#include<stdio.h>
int main()
{
int year;
  printf("Enter a year to check if it is a leap year\n");
  scanf("%d", &year);
  if ( year%400 == 0)
    printf("%d is a leap year.\n", year);
  else if ( year%100 == 0)
    printf("%d is not a leap year.\n", year);
  else if ( year%4 == 0 )
    printf("%d is a leap year.\n", year);
  else
    printf("%d is not a leap year.\n", year);
return 0;
}
//C/CPP/C++ linux code to revert bit at given position
#include <iostream>
using namespace std;
void bitSet(int,int);
void showBit(int);
int main()
{
int n,b;
cout<<"Enter a number and bit postion you want to revert:"<<endl;
cin>>n>>b;
bitSet(n,b);
return 0;
}
void showBit(int n)
{
cout<<"n="<<n<<endl;
unsigned int s=1;
s=s<<sizeof(int)*8-1;
while(s>0)
{
if(n&s)
cout<<"1";
else
cout<<"0";
s=s>>1;
}
cout<<endl;
}
void bitSet(int n, int b)
{
cout<<"n="<<n<<" b="<<b<<endl;
showBit(n);
unsigned int s=1;
s=s<<b-1;
n=n|s;
cout<<"after Setting bit"<<endl;
showBit(n);
}
/*
6
4
n=6 b=4
n=6
00000000000000000000000000000110
after Setting bit
n=14
00000000000000000000000000001110

*/
//C/CPP/C++ linux code to find endianness of your PC
#include<stdio.h>
int main()
{
   unsigned int i = 1;
   char *c = (char*)&i;/*a character pointer c is pointing to an integer i*/
   if (*c)    /*Since size of character is 1 byte when the character pointer is de-referenced it will contain only first byte of integer.*/
    printf("Your machine is Little endian.\n");//01 00 00 00 because 01 means 1 and true//My Machine
  
else
       printf("It is a Big endian Machine.\n");//00 00 00 01
  
   return 0;
}

Stack implementation using array in C

/*stack implementation using array
push pop operation
*/
#include<stdio.h>
void push(int item);
int pop();
void display();
int stack[10];
int top=-1;
int main()
{
push(10);
push(20);
push(30);
push(40);
push(50);
display();
printf("\n");
pop();
pop();
display();
return 0;
}
void push(int item)
{
if(top==9)
{
printf("Stack is full.\n");
return;
}
top++;
stack[top]=item;
}
int pop()
{
printf("\npop called.\n");
if(top==-1)
{
printf("stack is empty\n");
return -1;
}
int item;
item=stack[top];
top--;
return item;
}
void display()
{
printf("\ndisplay called.\n");
int i;
for(i=top;i>=0;i--)
{
printf("%d ",stack[i]);
}
}
/*
display called.
50 40 30 20 10
pop called.
pop called.
display called.
30 20 10
*/
//stack to reverse a string
//Stack appalication to reverse a sentence
//read array elts. push into stack,pop,display
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 20
char stack[SIZE];
int top=-1;
int i;
        void push(char c);
        int pop();
int main()
{
        char arr[]="World is not enough";
        printf("org string is: %s\n",arr);
        for(i=0;i<SIZE-1;i++)
        {
                push(arr[i]);

        }
        for(i=0;i<SIZE-1;i++)
        {
                arr[i]=pop();

        }
        printf("rev string is: %s\n",arr);

return 0;
}
        void push(char c)
        {
                if(top==SIZE-1)
                printf("overflow");
                else
                stack[++top]=c;
        }
        int pop()
        {
                if(top==-1)
                printf("underflow\n");
              else
                return stack[top--];
        }
/*
org string is: World is not enough
rev string is: hguone ton si dlroW
*/