Friday, 13 December 2013

C++ program to change all lower case letters into Uppercase


//Program to convert from lower case to upper case

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char string[30];
int i,n=0;
cout<<"\nEnter the string : ";
gets(string);
cout<<"Before conversion : "<<string<<endl;
for(i=0;string[i]!='\0';i++)
    n++;
for(i=0;i<n;i++)
{
    if(string[i]>=97 && string[i]<=122)
    string[i]=string[i]-32;
}
cout<<"After conversion :"<<string<<endl;
getch();
}

OUTPUT


C++ Temperature conversion program.

// Temperature conversion program.

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
float celsius,fahrenheit;
int choice;
choice=0;
cout<<"-----TEMPERATURE CONVERSION--------\n";
cout<<"1. Celsius\n2. Fahrenheit\n";
cout<<"Enter starting temperature choice : ";
cin>> choice;
switch(choice)
{
case 1: //if choice==1 then
        cout<<"Enter temperature in Celsius:";
        cin>> celsius;
        cin.ignore();
        fahrenheit = celsius*9/5+32;
        cout<<""<<celsius<<" degrees Celsius="<<fahrenheit<<" degrees Fahrenheit\n";
        break;

case 2: //if choice==2 then
        cout<<"Enter temperature in Fahrenheit:";
        cin >> fahrenheit;
        cin.ignore ();
        celsius= (fahrenheit-32)*5/9;
        cout<<""<<fahrenheit<<" degrees Fahrenheit ="<<celsius<<" degrees Celsius\n";
        break;

default: //if user enter wrong input choice
         cout<<"Please Enter Proper Choice.."<<endl;

}
getch();
return 0;
}

OUTPUT


C++ PROGRAM TO CHECK WHETHER THE INPUTTED WORD IS PALINDROME OR NOT WITHOUT USING ANY BUILT IN FUNCTION

//Program to check for palindrome


#include <iostream>
#include <string.h>
#include<conio.h>
using namespace std;
int main()
{

char x[100],y[100];
int temp1,temp2=0,j;

cout << "Enter Name/Digits :";
cin >> x;
temp1 = strlen(x)-1;
for(j= 0;j<strlen(x);j++)
y[j] = x[temp1 - j];
y[j] = '\0';
x[j] = '\0';
for(int k =0; k<strlen(x); k++)
{
    if (x[k]==y[k])
     temp2 = temp2 + 1;
}
if (strlen(x) == temp2)
 cout<<"Palindrome";
else
  cout<<"Not a palindrome";
getch();
}


OUTPUT



Thursday, 12 December 2013

Program to reverse a string.

//Program to reverse a string.


#include<iostream>
#include<string.h>
#include<conio.h>
using namespace std;
int main()
{
    char str[70];
    char temp;
    int i,n=0;
    cout<<"\nEnter the string:";
    gets(str);
    for(i=0;str[i]!='\0';i++)
      n++; //To find the length of string.
    for(i=0;i<n/2;i++)
    {
        temp=str[i];
        str[i]=str[n-i-1];
        str[n-i-1]=temp;
    }
    cout<<"\nThe reversed string is:";
    cout<<str;
    getch();

}


OUTPUT



Program of string operations(length,concat and compare) without using the built in functions.

/*Program of string operations(length,concat and compare) without using the built in functions.*/

#include<iostream>

#include<string.h>
#include<conio.h>
using namespace std;
int main()
{
        char str[100],str1[100];
        int i=0,ch=1,icnt=0,icnt2=0,flag=0;

        

                cout<<"\nPlease enter a string:";
                cin>>str;
                cout<<endl<<"----LENGHT----"<<endl;
                
                //Length calculation
                while(str[i++]!='\0')
                {
                        icnt++;
                       
                }
                cout<<"\nThe given string contains "<<icnt<<" character(s) "<<endl;
                cout<<endl<<"----STRING COMPARE----"<<endl;
                cout<<"\nPlease enter string 2:";
                cin>>str1;
                i=0;

                //String comparision

                while(str1[i++]!='\0')
                {
                        icnt2++; 
                }
                for(i=0;str[i]!='\0';i++)
                        {
                                if(str[i]==str1[i])
                                        flag=1;
                                else
                                {
                                        cout<<"\nStrings are not equal";
                                        flag=0;
                                        break;
                                }

                        }

                if(flag==1)
                        cout<<"\nBoth strings are equal"<<endl;
                
                cout<<endl<<"----CONCATINATION----"<<endl;
                
                //String Conctination
                
                for(i=0;i<icnt2;i++)
                {
                        str[icnt++]=str1[i];                               
                }
                str[icnt]='\0';
                cout<<"\nThe concatinated string is "<<str;

               getch();

}



OUTPUT





PROGRAM OF GENERATING FIBONACCI SERIES

//PROGRAM OF GENERATING FIBONACCI SERIES

# include<iostream>
# include<conio.h>
using namespcae std;

int main()
{
int iLimit,a,b,c;
a=0;
b=1;

cout<<"Enter the limit for Fibonacci Series:";
cin>>iLimit;

cout<<Required Fibonaccis Series:";
cout<<a;
while(iLimit>=b)
{
cout<<b<<",";
c=a+b;
a=b;
b=c;
}
getch();
}


Basic concepts to write a program in C++


    Before proceed to further, let us go through some basic requirements which we have to use in our programs.
Some main concepts we have to concentrate are
1. Header Files
2. Screen I/O in C++.

Header Files
•Header files are used for declarations
•Header files can be included for using their corresponding .cpp files
•Header files can be included in a nested structure
•Standard C header files have been renamed and placed in namespaces

–e.g. <cstdlib>, <cctype>,<cstdio>

How this header file works?

Have a look at this.


Header files are linked to supported library automatically when its called during runtime.

library includes 
1. A header file that contains declarations for everything the library wishes to expose to users.
2. A pre-compiled object that contains all of the implementation code compiled into machine language. 

Now a question may arise.
Why are libraries precompiled? 
The reasons are
1. Since libraries rarely change, they do not need to be recompiled. It would be a waste of time to compile them every time you wrote a program that used them.
2. Since precompiled objects are in machine language, it prevents people from accessing or changing the source code.

Note: You can write your own header files and save it in a library so that you can able to access that in your program.

Hope this is sufficient regarding header files.

2. Screen I/O in C++.



Have a look at the above figure,

•It is perfectly valid to use the same I/O statements in C++ as in C .
        The very same printf, scanf, and other stdio.h functions that have been used in C.
•However, C++ provides an alternative with the new stream input/output features. The header file is named iostream and the stream I/O capabilities are accessible when you use the pre-processor declaration:

     #include <iostream>                  // No “.h” on std headers
     using namespace std;                 // To avoid things like
                                                    // std::cout and std::cin

•Several new I/O objects available when you include the iostream header file. Two important ones are:
     –cin // Used for keyboard input (std::cin)
     –cout // Used for screen output (std::cout)

•Since cin and cout are C++ objects, they are somewhat "intelligent":
    –They do not require the usual format strings and conversion specifications.
    –They do automatically know what data types are involved.
    –They do not need the address operator, &.
    –They do require the use of the stream extraction ( >> ) and insertion ( << ) operators.

•Both cin and cout can be combined with other member functions for a wide variety of special I/O capabilities in program applications.

So, streams in <iostream> mainly includes
 •cout
 •cin
 •Operator <<
 •Operator >>

Hope this is sufficient to proceed further to implement bit complex codes.