Don't miss

Monday 9 September 2013

A Colection Of Basic Codes


So hello to all, I'll  write a few elementary codes for beginners,to  provide a basic insight of C++ , this blog post will see additions from time to time ,so stay tuned.


Current Number Of Codes In Blog : 8
To jump to the code you need press "Ctrl + G" and type heading or use the navbar.
------------------------------------------------------------------------------------------------------
ABC Section


To Show Average of Five Entries
Program showing average of 5 subjects












#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a,b,c,d,e,f;   //Here float is used to include numbers with decimal points
cout<<"Enter Values":
cin>>a>>b>>c>>d>>e;
f=a+b+c+d+e/5;
cout<<"Average Marks is";
cout<<f;
getch();
}
------------------------------------------------------------------------------------------------------------

To Find ASCII Code for the given character

Program to find ASCII code










Note: Almost all characters on your keyboard have a ASCII code in C++ ranging from 0 to 265. >Eg 'A' has ASCII code 65

#include<iostream.h>
#include<conio.h>
void main()
{
 clrscr()
  char n;
  int x;
   cout<<"Enter a character";
   cin>>n;
  x=n;
   cout<<"ASCII Code is "<<x;
 getch()
}
---------------------------------------------------------------------------------------------------
Largest Among Four Numbers Entered

Note- An improvement of this code is in the loop section

void main()
{
clrscr();
int a,b,c,d,e=0,f=0;
cout<<"Enter 4 numbers";
cin>>a>>b>>c>>d;
if (a>e)
e=a;
if (b>e)
e=b;
if (c>e)
e=c;
if (d>e)
e=d;
cout<<"Largest Number Is"<<e;
if((a!=e)&&(a>f))
f=a;
if((b!=e)&&(b>f))
f=b;
if((c!=e)&&(c>f))
f=c;
if((d!=e)&&(d>f))
f=d;
cout<<"2nd Largest Number Is"<<f;
getch();
}
----------------------------------------------------------------------------------------------------
Loop-A-Loop Section

Fibonacci Series Till N Numbers

 #include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a=0,b=1,c,n;
cin<<n;                                              //For The Range
cout<<a<<"\n"<<b<<"\n";
do
{                                                       //You can also use 'for' loop
c=a+b;
a=b;
b=c;
cout<<c<<"\n";
} while(c<n);
getch();
}
-----------------------------------------------------------------------------------------------------

To Reverse The Digits Of A Number

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c=0;
cin>>a;   //The Digit We Want To Reverse
do
{  b=a%10;
   a=a/10;
   c=c*10+b;
} while(a!=0);
cout<<c;
getch();
}

-------------------------------------------------------------------------------------------------

HCF Among Two Numbers

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,r,k,x;
cin>>a>>b;
if(a>b)
{
k=b;
}
else
{
k=a;
}
for(r=1;r<=k;r++)
{
if((a%r==0)&&(b%r==0))
x=r;
}
cout<<"HCF"<<x;
getch();

}
--------------------------------------------------------------------------------------------------

Largest Even And Odd Numbers Among The Numbers Entered

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b=0,c=1;
cout<<"Enter Number"<<"\t";  //Observers may note that this code is for positive numbers
cout<<"0 ends the program";
do
{
cin>>a;
if(a%2==0)
{
if (a>b)
b=a;
}
else
if(a>c)
c=a;
} while(a!=0);
cout<<"Largest Even Number"<<b;
cout<<"Largest Odd Number"<<c;
getch();
}
Continue Reading...

Sunday 8 September 2013

Restarting A Program


Restarting a programme is a very good way to indirectly ask user not to close your program. Restarting your may help user now opening the file to run your program again and again. User can save time with this.
Restarting A program

After completing your programs , many of you may have wondering if you can restart your program or not.
Here is the answer - yes, you can.

We are now sharing you the little secret with you....

All you have to do is to put the whole program in a while loop with a condition that (n!=y), where n is a variable

For example
include<iostream.h>

void main()
{
char n;
 do
  {
  .
  .
  .
  (your programme)
  .
  .
  cout<<"Do you want to restart your program ? (Enter y to exit)";
  cin>>n;
  }while(n!=y)
}
(ssshhhh!!!)
Don't tell it to anyone else, its a secret between us and astonish your other fellows with it who do not know how to restart a program.
Continue Reading...

Sunday 1 September 2013

Colour Scheme For Codes


Coloring Scheme
You must have noticed that we use different colours/colors in our codes.Its basically to differentiate between the "common code" necessary in every program and the "target code" specifically for the purpose we are programming for.In a layman terms"Different colours ,different purpose"

So here they are

  1. Blue     
This is for libraries. These are basically components(parts) of C++ that store different types of operator. This can be explained by an example :-  A construction worker requires a toolkit containing screws,wrenches,hammers etc. Similarly a medical officer requires a first-aid kit with bandages,medicine etc.
Here the kit can be taken as a library and the kits content ,the operators.

So there are libraries for input and output command , math commands ,clearing screen etc.
Note that here three main libraries are used;-
#include<iostream.h> ==> Necessary for input and output 
#includes<conio.h>     ==> To clear the Dosbox of previous writing when running programs(Clear-screen)
#include<math.h>       ==> For mathematical operators like square root.

    2. Green

For main() function that tells that the program is starting from there, and other basic functions that have the bracket () sign.

    3. Red

For the comments of-course , and some practical jokes too.

That's it.
P.S: Right ,in a "long" post we (me and DW) don't colour after the first 2-3 codes,as colours are just for a basic feel.
------------------------------------------------------------------------------------------------------------

Continue Reading...

Saturday 17 August 2013

C++: Alphabet Staircase


Program to show Alphabetical Staircase










Output will be
A
AB
ABC
ABCD
ABCDE

Code is :
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
for(int i=65; i<=69; i++)  //To make a bigger staircase , just increase max limit of i from 69 to what you need
{cout<<"\n";
for(char j=65; j<=i; j++)
cout<<j;
}
getch();
}
Continue Reading...

Friday 16 August 2013

C++ : Change Case Of An Alphabet


The following code is for a C++ Programme to change the case of a given alphabet.

That is :
If "a" is input , the output is "A" [ Small letter to Capital letter]
If "A" is input , the output is "a" [ Capital letter to Small letter]

Program to show change of case of Alphabet









#include<iostream.h>
#include<conio.h>

void main()

{

clrscr();

char a,c;
int b,d;

cout<<"Enter a alphabet\n";
cin>>a;
b=a;

if(b<91)
  {
    d=b+32;
    c=d;
    cout<<c;
  }
else
{
   d=b-32;
   c=d;
   cout<<c;
 }

getch();
}
Continue Reading...