#include<iostream
>
#include<conio.h
>
#include<math.h
>
using namespace std;
int Octal_to_Binary(long n)
{
long binary_num = 0, decimal_num = 0, i = 0;
while(n!=0)
{
decimal_num = decimal_num + ((n%10) * pow(8,i));
++i;
n = n/10;
}
i = 1;
while(decimal_num!=0)
{
binary_num = binary_num + ((decimal_num % 2) * i);
decimal_num = decimal_num/2;
i = i*10;
}
return binary_num;
}
int main()
{
long num;
cout<<"Enter a octal number : ";
cin>>num;
cout<<"Octal Number "<<num
<<" is equal to "<<Octal_to_Binary(num)
<<" Binary Number."<<endl;
getch();
return 0;
}
Wednesday, 25 October 2017
Program to Convert Octal Number to Binary Number in c++
Program to Convert Binary Number to Octal Number in c++
#include<iostream
>
#include<conio.h
>
#include<math.h
>
using namespace std;
int Binary_to_Octal(long n)
{
long octal_num = 0, decimal_num = 0, i = 0;
while(n!=0)
{
decimal_num = decimal_num + ((n%10) * pow(2,i));
++i;
n = n/10;
}
i = 1;
while(decimal_num!=0)
{
octal_num = octal_num + ((decimal_num % 8) * i);
decimal_num = decimal_num/8;
i = i*10;
}
return octal_num;
}
int main()
{
long num;
cout<<"Enter a binary number : ";
cin>>num;
cout<<"Binary Number "<<num
<<" is equal to "<<Binary_to_Octal(num)
<<" Octal Number."<<endl;
getch();
return 0;
}
Output
Sunday, 22 October 2017
Program to Convert Decimal Number to Octal Number in c++
#include<iostream
>
#include<conio.h
>
#include<math.h
>
using namespace std;
int Decimal_to_Octal(long n)
{
int octal_number = 0, i = 1, remainder;
while(n!=0)
{
remainder = n%8;
n = n/10;
octal_number = octal_number + (remainder*i);
i = i*10;
}
return octal_number;
}
int main()
{
long num;
cout<<"Enter a decimal number : ";
cin>>num;
cout<<"Decimal Number "<<num
<<" is equal to "<<Decimal_to_Octal(num)
<<" Octal Number."<<endl;
getch();
return 0;
}
Output
Convert Octal Number to Decimal Number in c++
#include<iostream
>
#include<conio.h
>
#include<math.h
>
using namespace std;
int Octal_to_Decimal(long n)
{
int decimal_number = 0, i = 0, remainder;
while(n!=0)
{
remainder = n%10;
n = n/10;
decimal_number = decimal_number + (remainder*pow(8,i));
++i;
}
return decimal_number;
}
int main()
{
long num;
cout<<"Enter an octal number : ";
cin>>num;
cout<<"Octal Number "<<num
<<" is equal to "<<Octal_to_Decimal(num)
<<" Decimal Number."<<endl;
getch();
return 0;
}
Output
Program to Convert Decimal Number to Binary Number in c++
#include<iostream
>
#include<conio.h
>
#include<math.h
>
using namespace std;
int Decimal_to_Binary(long n)
{
long binary_number = 0;
int remainder = 0, i = 1, step = 1;
while(n!=0)
{
remainder = n%2;
cout<<"Step : "<<step++
<<"\t"<<n
<<"/2\tRemainder = "<<remainder
<<"\tQuotient = "<<n/2
<<endl;
n = n/2;
binary_number = binary_number + (remainder*i);
i = i*10;
}
return binary_number;
}
int main()
{
int num;
cout<<"Enter a decimal number : ";
cin>>num;
cout<<"Decimal Number "<<num
<<" is equal to "<<Decimal_to_Binary(num)
<<" Binary Number."<<endl;
getch();
return 0;
}
Output
Program Convert Binary Number to Decimal Number in c++
#include<iostream
>
#include<conio.h
>
#include<math.h
>
using namespace std;
int Binary_to_Decimal(long n)
{
int decimal_number=0, i=0, remainder;
while(n!=0)
{
remainder = n%10;
n = n/10;
decimal_number = decimal_number + (remainder*pow(2,i));
++i;
}
}
int main()
{
long num;
cout<<"Enter a binary number : ";
cin>>num;
cout<<"Binary Number "<<num
<<" is equal to "<<Binary_to_Decimal(num)
<<" Decimal Number."<<endl;
getch();
return 0;
}
Output
Program to find Quotient and Remainder in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int divisor, dividend, quotient, remainder;
cout<<"Enter dividend : ";
cin>>dividend;
cout<<"Enter Divisor : ";
cin>>divisor;
quotient = dividend / divisor;
remainder = dividend % divisor;
cout<<"Quotient : "<<quotient
<<endl;
cout<<"Remainder : "<<remainder<<
endl;
getch();
return 0;
}
Output
How to find Leap Year in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int year;
cout<<"Enter the year : ";
cin>>year;
if(year%4==0)
cout<<year
<<" is a leap year.";
else
cout<<year
<<" is not a leap year.";
getch();
return 0;
}
Output
Wednesday, 18 October 2017
Greater in three numbers in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int var1, var2, var3;
cout<<"Enter three numbers\n";
cin>>var1>>var2>>var3;
if(var1>var2 && var1>var3)
cout<<var1
<<" is greater in three numbers."<<endl;
else if(var2>var3)
cout<<var2
<<" is greater in three numbers."<<endl;
else
cout<<var3
<<" is greater in three numbers."<<endl;
getch();
return 0;
}
Output
Tuesday, 17 October 2017
Area of rectangle in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
float lenght, breath;
double area;
cout<<"Enter length and breath\n";
cin>>lenght>>breath;
area=lenght*breath;
cout<<"Area of rectangle : "<<area;
getch();
return 0;
}
Output
Area of circle in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
float radius;
double area;
cout<<"Enter radius of circle : ";
cin>>radius;
area=3.14*radius*radius;
cout<<"Area of circle : "<<area;
getch();
return 0;
}
Output
Average of five marks in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int a,b,c,d,e;
float avg;
cout<<"Enter marks of five marks\n";
cin>>a>>b>>c>>d>>e;
avg=(a+b+c+d+e)/5;
cout<<"Average Marks : "<<avg;
getch();
return 0;
}
Output
Compound Interest in c++
#include<iostream
>
#include<math.h
>
#include<conio.h
>
using namespace std;
int main()
{
double p, t, r, A, CI;
cout<<"Enter Principal Amount, Time and Rate\n";
cin>>p>>t>>r;
A=p*pow((1+(r/100)),t);
CI=A-p;
cout<<"Compound Interest : "<<CI;
getch();
return 0;
}
Output
Simple Interest in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
double p,t,r,SI;
cout<<"Enter Inverted Amount, Time and Interest Rate\n";
cin>>p>>t>>r;
SI=(p*t*r)/100;
cout<<"Simple Interest : "<<SI;
;
getch();
return 0;
}
Output
Sum of two numbers in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int var1, var2;
cout<<"Enter two numbers for sum\n";
cin>>var1>>var2;
cout<<"Sum of "<<var1
<<" and "<<var2
<<" is "<<var1+var2;
getch();
return 0;
}
Output
Monday, 16 October 2017
Python Reverse String
input_string=input("Enter a string : ")
output_string=input_string[::-1]
print("Reverse of string ",input_string," is ",output_string)
Output
Factorial in Python
num=int(input("Enter an integer to find factorial : "))
fact=1
if num<0
:
print("Factorial of negative numbers does not exist.")
elif num==0:
print("Factorial of 0 is 1.")
elif num==1:
print("Factorial of 1 is 1.")
else:
for i in range(1,num+1):
fact=fact*i
print("Factorial of ",num," is ",fact)
Output
Factorial using recursion in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int fact_recurse(int n)
{
if(n==1)
return n;
else
return n*fact_recurse(n-1);
}
int main()
{
int num;
cout<<"Enter an integer to find factorial : ";
cin>>num;
if(num<0
)
cout<<"Factorial of negative numbers does not exist.";
else if(num==0)
cout<<"factorial of 0 is 1.";
else
cout<<"Factorial of "<<num
<<" is "<<fact_recurse(num);
getch();
return 0;
}
Output
Factorial using Recursion in python
def fact_recurse(n):
if n==1:
return n
else:
return n*fact_recurse(n-1)
num=int(input("Enter an integer to find factorial : "))
if num<0
:
print("Factorial of negative numbers does not exist.")
elif num==0:
print("Factorial of 0 is 1.")
else:
print("Factorial of ",num," is ",fact_recurse(num))
Output
Series of prime numbers in python
num=int(input("Enter a number till which you want to print prime numbers : "))
for x in range(1,num) :
if x>1 :
for y in range(2,x) :
if (x%y) ==0 :
break
else:
print(x)
Output
Prime Number in Python
num=int(input("Enter an integer : "))
if num>1 :
for i in range(2,num):
if(num%i)==0:
print(num," is a prime number")
break
else:
print(num," is a prime number")
else:
print(num," is a prime number")
Output
Sunday, 15 October 2017
Factorial in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int num, fact=1;
cout<<"Enter a number to find factorial : ";
cin>>num;
for(int i=1;i<=num;i++)
{
fact=fact*i;
}
cout<<"Factorial of "<<num<<" is "<<fact;
getch();
return 0;
}
Output
Palindrome in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int n, num, reverse=0;
cout<<"Enter a number : ";
cin>>n;
num=n;
while(num!=0)
{
reverse=(num%10)+(reverse*10);
num=num/10;
}
if(n==reverse)
cout<<n
<<" is a Palindrome Number.";
else
cout<<n
<<" is not a Palindrome Number.";
getch();
return 0;
}
Output
Even or Odd in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int num;
cout<<"Enter number to check even or odd : ";
cin>>num;
if(num%2==0)
cout<<num
<<" is an even number.";
else
cout<<num
<<" is an odd number.";
getch();
return 0;
}
Output
Saturday, 14 October 2017
Swapping of numbers in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int num1, num2, temp;
cout<<"Enter two numbers for swapping\n";
cin>>num1>>num2;
cout<<"Numbers before swapping\n";
cout<<"Num1 = "<<num1<<
"\tNum1 = "<<num2;
temp=num1;
num1=num2;
num2=temp;
cout<<"\nNumbers sfter swapping\n";
cout<<"Num1 = "<<num1<<
"\tNum2 = "<<num2;
getch();
return 0;
}
Output
Binary Search in c++
#include<
iostream>
#include<conio.h
>
using namespace std;
int main()
{
int n,num,first=0,middle,last;
cout<<"Enter the size of your array : ";
cin>>n;
int *arr=new int[n];
cout<<"Enter element of array\n";
for(int i=0;i<
n;i++)
{
cin>>arr[i];
}
cout<<"\nElement of array\n";
for(int i=0;i<
n;i++)
{
cout<<arr[i]<<"\t";
}
cout<<"\nEnter number to search : ";
cin>>num;
last=n-1;
middle=(first+last)/2;
while(first<=last)
{
if(arr[middle]<
num)
{
first=middle+1;
}
else if(arr[middle]==num)
{
cout<<num
<<" is found at "<<middle+1<<" possition";
break;
}
else
{
last=middle-1;
}
middle=(first+last)/2;
}
if(first>last)
{
cout<<num
<<" is not found in the array list.";
}
getch();
return 0;
}
Output
Linear Search in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int n,num,flag=0,pos;
cout<<"Enter the size of array : ";
cin>>n;
int *arr=new int[n];
cout<<"Enter elements of array\n";
for(int i=0;i<
n;i++)
{
cin>>arr[i];
}
cout<<"Elements of array\n";
for(int i=0;i<
n;i++)
{
cout<<
arr[i]<<"\t"
;
}
cout<<"\nEnter element to search : ";
cin>>num;
for(int i=0;i<
n;i++)
{
if(arr[i]==num)
{
flag=1;
pos=i+1;
break;
}
}
if(flag==1)
{
cout<<num
<<" is found at "<<
pos<<
" possition.";
}
else
{
cout<<num
<<" is not found in the array list.";
}
getch();
return 0;
}
Output
Friday, 13 October 2017
Sorting of Array in Ascending Order in c++
#include<
iostream
>
#include<
conio.h
>
using namespace std;
int main()
{
int i,j,n,temp;
cout<<"Enter size of array : ";
cin>>n;
int *arr=new int[n];
cout<<"Enter elements of array\n";
for(i=0;i<
n
;i++)
{
cin>>arr[i];
}
cout<<"Elements of array\n";
for(i=0;i<
n
;i++)
{
cout<<
arr[i]<<"\t";
}
cout<<"\nSorted elements of array\n";
for(i=0;i<
n
;i++)
{
for(j=0;j<
n-i-1
;j++)
{
if(arr[j]>
arr[j+1]
)
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(i=0;i<
n
;i++)
{
cout<<
arr[i]
<<"\t";
}
getch();
return 0;
}
Sorting of Array in c++
#include<iostream
>
#include<conio.h
>
using namespace std;
int main()
{
int i,j,n,temp;
cout<<"Enter size of array : ";
cin>>n;
int *arr=new int[n];
cout<<"Enter elements of array\n";
for(i=0;i<n
;i++)
{
cin>>arr[i];
}
cout<<"Elements of array\n";
for(i=0;i<n
;i++)
{
cout<<arr[i]<<"\t";
}
cout<<"\nSorted elements of array\n";
for(i=0;i<n
;i++)
{
for(j=0;j<n-i-1
;j++)
{
if(arr[j]<arr[j+1]
)
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
for(i=0;i<n
;i++)
{
cout<<arr[i]
<<"\t";
}
getch();
return 0;
}
Thursday, 12 October 2017
Omitting any line which starts from #,*,@,&, etc
f1=open("OldFile.txt","r") //Opening old file from which we will fetch the data
f2=open("NewFile.txt","w") //Opening new file to store the remaining data of old filewhile True:
text=f1.readline() //fetching data to a variable from old file
if text[0]=="#": //here you can change the comparing condition #,*,@,&
continue
f2.write(text)
f2.close()
f1.close()
f1=open("NewFile.txt","r")
print(f1.read())
f1.close()
Copying content of one file to another file
f1=open("OldFile.txt","w") //Opening of old file in write mode for writing the data
f1.write("This is old file") //Writing to the file
f1.close() //Closing of old file
f1=open("OldFile.txt","r") //Opening new file in read mode
text=f1.read() //Reading from file and storing data into another variable
f1.close() //Closing of old file
f2=open("NewFile.txt","w") //Opening new file in write mode for copying the data
f2.write(text) //Writing data means copying the of old file to new file
f2.close() //Closing of new file
f2=open("Newfile.txt","r") //Opening new file in read mode
print(f2.read()) //Reading copied data from new file
f2.close() //Closing new file
Take user input data
Creating, Reading and Writing into File
How to print half upper diamond in c++?
How to print diamond shape in c++?
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
int k,c,num,space=1;
cout<<"How many rows do you want ? ";
cin>>num;
space=num-1;
for (k = 1; k <= num; k++)
{
for (c = 1; c <=space; c++)
{
cout<<" ";
}
space--;
for (c = 1; c <= 2*k-1; c++)
{
cout<<"*";
}
cout<<endl;
}
space=1;
for(k=1;k<=num-1;k++)
{
for(c=1;c<=space;c++)
{
cout<<" ";
}
space++;
for(c=1;c<=2*(num-k)-1;c++)
{
cout<<"*";
}
cout<<endl;
}
getch();
return 0;
}
Output
Python Program to find Fabonacci
fabtab={} def fabonacci(n): fabtab[0]=0 fabtab[1]=1 for i in range(2,n+1): fabtab[i]=fabtab[i-1]+fabtab[i-2] ...
-
#include stdio.h > #include stdlib.h > #include conio.h > int arr[100], size=0; void insert_beg(); void insert_pos(); void inser...
-
#include iostream> #include conio.h > using namespace std; int main() { int n,num,first=0,middle,last; ...
-
#include iostream > #include conio.h > using namespace std; int main() { float radius; double area; ...