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;
}

No comments:

Post a Comment