Skip to main content

Posts

Showing posts from 2013

C++ Program to test whether a number is palindrome or not.

A number is called palindrome, if it reads the same on reading from both left and right. Thus, 121 is a palindrome, but 123 is not. Because, on reading from the right, it is 321 and not 123.



The steps followed are,

get the number to be checked.reverse the number.compare with the given number.conclude.The program would be as follows.
The Program
/palindrome 25/08/2013 #include<conio.h> #include<iostream.h>
class dom { int dgt,dum; int flag; public: void get() { clrscr(); cout<<"enter the digit:"; cin>>dgt; dum=dgt; flag=0; } void check(); void disp() { if (flag==1) cout<<endl<<"palindrome"; else cout<<endl<<"not palindrome"; } }; void dom::check() { flag=0; int sum=0,w; while (dum!=0) { w=dum%10; sum=sum*10+w; dum=dum/10; } cout<<"reversed:"<<sum; if (sum==dgt) flag=1; };
void main() { clrscr(); dom obj; obj.get(); obj.check(); obj.disp(); //***vaju** getch(); }

Dynamic initialization of variables.

The below described is a program that shows how variables are declared in a dynamic manner. We can also initialize these variables dynamically. Hope this would be so simple for you. By trying some another examples, you can master on it.!


The program// using simple dynamic initialization of variables
#include<iostream.h> #include<conio.h>
void main() { clrscr(); int b,c=sizeof("xloi");    //dynamic initialization for 'c' cout<<"\nvalue for b="; cin>>b; int q=b*1.3*c;  //dynamic initialization for 'q' cout<<"q="<<q; getch(); }

Using the key word "const" in cpp

The const key word creates symbolic constants in Cpp. The values declared as const cannot be modified by the program in any way. So it is a way of declaring constants throughout the program.


The program
//using const data type
#include<conio.h> #include<iostream.h> const int q=10;    // const are global in nature.
void main() { const char w[q]="\nhello\n"; const char e='k';
clrscr(); cout<<q<<w<<e;
getch(); }

Program to check weather a number is amstrong or not.

Hi guys,

This is a simple cpp program to check weather a number is amstrong or not.

A number is amstrong if the sum of the cubes of the digits of the number is the same number. Thus as a common example we use the number 153.

Then,
1^3=1
5^3=125
3^3=27
the total will be 1+125+27=153 ,the same number.

In the program, we:

input the number to be checked.extract each digits from the number.take cubes.add up.check the result with the given number.display the result.Now try the program given below:

//amstrong #include<conio.h> #include<iostream.h>
class dom { int dgt,dum,flag; public: void get() { cout<<"Enter the digit:"; cin>>dgt; cout<<"done!"; dum=dgt; } void check(); void disp() { if(flag==1) cout<<"\n Awesome! Amstrong"; else cout<<"\noops! not *_* TRY AGAIN"; }
}; void dom::check() { int q,sum=0; while (dum!=0) { q=dum%10; sum=sum+q*q*q; dum=dum/10; } cout<<"\n\ncubed:"<<sum; if (sum==dgt) flag=1; else flag=0; };
void main() { …

Object Orientation Enlarged by an Example!

This program illustrates how to implement the arithmetic operation in an object oriented way.
Here we have a class with 2 variables. For each operation an object of this class is created to do the operation instead of declaring such much variables. Therefore, this method is, for a large scale implementation, is quite efficient. so have a try on it.


Program:
//simple operations in object oriented method
#include<conio.h>
#include<iostream.h>

class dom
{
int a,b;
public:
void get()
{
cout<<"oops..!\nnumbers:";
cin>>a>>b;
cout<<"passed";
}
void add()
{
cout<<endl<<"a+b="<<a+b;
}
void diff()
{
cout<<endl<<"a-b="<<a-b;
}
void mult()
{
cout<<endl<<"a*b="<<a*b;
}
void div()
{
cout<<endl<<"a/b="<<a/b;
}

};

void main()
{
clrscr();
dom add, mult,diff,div;

cout<<"\n+";
add.get();

cout<<"\n-";
diff.get();

cout<<"\n*";
mult.get…

C++ Program to find the sum of matrices

We input two sets of matrices and the output is obtained simply adding the curresponding elements of the matrices. A game of simple logic.


Program:

//matrix
#include<iostream.h>
#include<conio.h>
class dom
{
int m,n,i,j,a[3][3],b[3][3],c[3][3];
public:
void get();
void check();
void disp();
};
void dom::get()
{
cout<<"enter the order of matrix\n";
cin>>m>>n;
cout<<"enter the elements of first matrix\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
cout<<"enter the elements of second matrix\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>b[i][j];
};
void dom::check()
{
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
};

void dom::disp()
{
cout<<"the sum of matrix is \n";
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<"\t"<<c[i][j];
}
cout<<"\n";
}
}





void main()
{
clrscr();
dom obj;
obj.get();
obj.check();
obj.disp();
getch();

}

The Language Evaluation Criteria

There are a vast amount of programming language that we are using. Some are designed to accomplish some specific task. Some are for common uses. Any way the below described is a short ‘summery’ of the criteria we followed to evaluate the language.



Overview
1.Readability a.Overall Simplicity b.Orthogonality c.Data Types d.Syntax Design 2.Writability a.Simplicity and Orthogonality b.Support for Abstraction c.Expressivity 3.Reliability a.Type Checking b.Exception Handling c.Aliasing d.Readability and Writability e. 4.Cost

1.Readability
It is the ease with which programs can be read and understood. ·Overall Simplicity.oA manageable set of features and constructs oFew feature multiplicity (means of doing the same operation) oMinimal operator overloading oFunction overloading ·OrthogonalityoA relatively small set of primitive constructs can be combined in a relatively small number of ways. oEvery possible combination is legal. ·

Object Oriented Programming (OOPs)- a funny starting

OOPs – Object Oriented Programming SystemObject-oriented programming (OOP) is a programming paradigm that uses “Objects “and their interactions to design applications and computer programs.

There are different types of OOPs are used, they are

ObjectClassData Abstraction & EncapsulationInheritancePolymorphismDynamic BindingMessage Passing

1) Object :Object is the basic unit of object-oriented programming. Objects are identified by its unique name. An object represents a particular instance of a class. There can be more than one instance of an object. Each instance of an object can hold its own relevant data.

An Object is a collection of data members and associated member functions also known as methods.

For example whenever a class name is created according to the class an object should be created without creating object can’t able to use class.

The class of Dog defines all possible dogs by listing the characteristics and behaviors they can have; the object Lassie is one particular dog, …

A program closely resembles to the one used by Adobe Reader

Ultimately, this is a program for searching strings.This program enables us to give a key string to be searched in given set of sentences. The output will be the position at which the given string is present.This can be improved in the C++ platform to exactly mention the string as selected. 
//A Search program closely resembles to the one used by Adobe Reader.

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<string.h>
class para
{
public:char ckey,skey[10],sent[40];
int i;
void get()
{
cout<<"Type the paragraph of maximum 40 characters, press enter to finish:\n------------------------"<<endl;
gets(sent);
cout<<"---------------------------------------------\nput the letter to be searched:";
cin>>ckey;
cout<<"type the string to be searched:";
gets(skey);
cout<<"processing...\n";
}
void check();

};
void para::check()
{
int l=strlen(sent);
cout<<"\n-------------------------------------\n…

C++ program to extract the elements in the odd positions of an array

C++ program to extract the elements in the odd positions of an array that are stored into an another array.This is very easy to do. 
//program to extract elements in the odd positions

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

class extract
{
public:char sent[25],extd[25];
int l;
void get()
{
cout<<"\nType the sentance:\n"<<endl;
gets(sent);
cout<<"OK"<<endl<<endl;
}
int filter();
void disp(int r);

};
int extract::filter()
{
int i,j;
l=strlen(sent);
for(j=0,i=0;i<l;i=i+2,j++)
{
extd[j]=sent[i];
}
return j;
};
void extract::disp(int r)
{
int i;
for(i=0;i<r;i++)
cout<<extd[i];
};
void main()
{
clrscr();
extract t;
t.get();
int g=t.filter();
t.disp(g);
getch();
}

__________________________________________________

try this and share it!

C++ [cpp] Program to find largest and smallest among 3 nnumbers.

Q) Write a C++ [cpp] program to find the largest and smallest among 3 numbers using the concepts of class, object, scope resolution operator & functions.ANSWER_____________________________________________

//find largest & smallest among 3 no:
#include<conio.h>
#include<iostream.h>

class domain
{
public:
int a[3],large,small;
void get();
void check();
void disp()
{
cout<<"\n  largest= "<<large<<endl<<"  smallest= "<<small<<endl<<"\nBYE!";
}
};
void domain::get()
{
int i;
cout<<"Enter 3 numbers:"<<endl;
for(i=0;i<3;i++)
cin>>a[i];
cout<<"OK\n";
};
void domain::check()
{
int i;
large=a[0];
small=a[0];
for(i=0;i<3;i++)
{
if(a[i]>large) large=a[i];
if(a[i]<small) small=a[i];
}
};
void main()
{
clrscr();
domain obj;
obj.get();
obj.check();
obj.disp();
getch();
}
__________________________________________________


Having trouble in executing the program? Suggestions? helps / need help? Shar…

Enumerated Datatypes AND #define

Ooops..! yes, this is an another post related to OOP.
I feel this is some thing nice. There are situations where we want to introduce new data variables. That's why enumerated data types are used. haha, but this is not like u expect! Enumerated DatatypesThese are user defined datatypesthe key-word enum is usedsyntax is enum var_name{var1,vae2,...,varn};what happens is : var1=0, var2=1, var3=2, var4=3,...try the prog. below in ur cpp compiler.#define#define key-word is used.Eg: #define x valueNo ";" is usedadvantage: for a large program, if we want to alter a constant that is repeatedly used throughout the programme.meaning x can be used insted of value.For the perfect understanding, please work on the example below. (Shhhh... it is very simple!) _____________________________________________
// using enumerated data types, #define

#include<conio.h>
#include<iostream.h>
#define num 60
#define name 0

void main()
{
clrscr();

enum b{q,w,e};

cout<<"num ="<<…