Private members of class worker
wno integer
wname 25 characters
hrwk, wgrate float (hour worked and wage rate per hour)
totwage float (hrwk * wgrate )
calcwg ( ) A function to find hrwk * wgrate with float return type.
Public members of class worker
In_data ( ) a function to accept values for wno, wname, hrwk, wgrate and invoke calcwg ( ) to calculate netpay.
Out_data ( ) a function to display all the data members on the screen
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class worker {
private:
int wno;
char name[25];
char subject[10];
float hrwk;
float wgrate;
float totwage;
float calcwg();
public:
void In_data();
void Out_data();
};
float worker::calcwg() {
return hrwk*wgrate;
}
void worker::In_data() {
cout<<"\nEnter worker number: ";
cin>>wno;
cout<<"\nEnter your name: ";
gets(name);
cout<<"\nEnter hour worked: ";
cin>>hrwk;
cout<<"\nEnter wage per hour : ";
cin>>wgrate;
totwage=calcwg();
}
void worker::Out_data() {
cout<<"\nYour worker number: "<<wno;
cout<<"\nYour name: "<<name;
cout<<"\nhours worked: "<<hrwk;
cout<<"\nWage per hour: "<<wgrate;
cout<<"\nYour total wages: "<<calcwg();
}
void main() {
worker w1;
w1.In_data();
w1.Out_data();
getch();
}
Monday, May 5, 2008
Using class in c++
Using class in c++
Private members :
Name 20 characters
Subject 10 characters
Basic, DA, HRA float
Salary float
Calculate ( ) function computes the salary and returns it.
Public members :
Readdata ( ) function accepts the data values and invokes the calculate function.
Displaydata ( ) function prints the data on the screen.
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
class student {
private:
char name[20];
char subject[10];
float basic;
float da;
float hra;
float salary;
float calculate();
public:
void readdata();
void displaydata();
};
float student::calculate() {
return basic+da+hra;
}
void student::readdata() {
cout<<"\nEnter your name: ";
gets(name);
cout<<"\nEnter your basic salary: ";
cin>>basic;
cout<<"\nEnter your daily allowance: ";
cin>>da;
cout<<"\nEnter your Hourse Rent Allowance: ";
cin>>hra;
salary=calculate();
}
void student::displaydata() {
cout<<"\nYour name: "<<name;
cout<<"\nYour total salary: "<<calculate();
}
void main() {
clrscr();
student s1;
s1.readdata();
s1.displaydata();
getch();
}
using nested structure in c++
Name, Roll No., Date of Birth (Day, Month, Year)
Assign value to all the members of the structure.
PROGRAM
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
struct date{
int day;
int month;
int year;
};
struct student {
char name[50];
int rollno;
date dob;
};
void main()
{
clrscr();
student s1;
cout<<"\nEnter your name: ";
gets(s1.name);
cout<<"\nEnter your roll No: ";
cin>>s1.rollno;
cout<<"\nEnter birth date: ";
cin>>s1.dob.day;
cout<<"\nEnter birth month: ";
cin>>s1.dob.month;
cout<<"\nEnter birth year: ";
cin>>s1.dob.year;
cout<<"\nYour Name is "<<s1.name;
cout<<"\nYour Roll No is "<<s1.rollno;
cout<<"\nYour Date of Birth is "<<s1.dob.day<<"-"<<s1.dob.month<<"-"<<s1.dob.year;
getch();
}
program to sort an array in c++ using selection sort
PROGRAM:
#include <iostream.h>
#include <conio.h>
const int N=10;
void main() {
int a[N],temp,i,j,first;
clrscr();
cout<<"Enter values to be sorted:\n";
for(i=0;i<N;i++) {
cout<<"Enter Value "<<i+1<<" : ";
cin>>a[i];
}
for(i=0;i<N-1;i++) {
first=i;
for(j=i+1;j<N;j++){
if(a[first]>a[j]) {
first=j;
}
}
temp=a[i];
a[i]=a[first];
a[first]=temp;
}
cout<<"\nThe sorted array is: \n";
for(i=0;i<N;i++) {
cout<<"\t"<<a[i];
}
getch();
}
sorting an array in c++ using bubble sort
PROGRAM:
#include <iostream.h>
#include <conio.h>
const int N=10;
void main() {
int a[N],temp,i,j;
clrscr();
cout<<"Enter ten values to be sorted:\n";
for(i=0;i<N;i++) {
cout<<"Enter Value "<<i+1<<" : ";
cin>>a[i];
}
for(i=0;i<N-1;i++)
for(j=0;j<N-1;j++)
if(a[j]>a[j+1]) {
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
cout<<"\nThe sorted array is: \n";
for(i=0;i<N;i++) {
cout<<"\t"<<a[i];
}
getch();
}
searching array using binary search in c++
PROGRAM
#include <iostream.h>
#include <conio.h>
void main() {
int a[10],data,i,initial,final,mid;
initial=0; final=9;
mid=(initial+final)/2;
clrscr();
cout<<"Enter ten values IN ascending order:\n";
for(i=0;i<10;i++) {
cout<<"Enter Value "<<i+1<<" : ";
cin>>a[i];
}
cout<<"\nEnter data to be searched: ";
cin>>data;
while((initial<=final) && (a[mid]!=data)) {
if(a[mid]>data)
final=mid-1;
else
initial=mid+1;
mid=(initial+final)/2;
}
if(a[mid]==data)
cout<<"\ndata is present";
if(initial>final)
cout<<"\ndata not present in the list";
getch();
}
searching array using linear search in c++
Using Linear search
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
int a[10],data,i,flag=0;
clrscr();
cout<<"Enter ten values:\n";
for(i=0;i<10;i++) {
cout<<"Enter Value "<<i+1<<" : ";
cin>>a[i];
}
cout<<"\nEnter data to be searched: ";
cin>>data;
for(i=0;i<10;i++) {
if(a[i]==data){
flag=1;
break;
}
}
if(flag==1)
cout<<"\ndata present";
else
cout<<"\ndata not present";
getch();
}//end of main
Using two diamensional array to find sum of diagonal in c++
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
int a[10][10];
int r,c,i,j,sum=0;
do {
clrscr();
cout<<"\nEnter the number of rows for the matrix: ";
cin>>r;
cout<<"\nEnter the number of cols for the matrix: ";
cin>>c;
} while(c!=r);
cout<<"\nEnter values for matrix a: \n";
for(i=0;i<r;i++) {
for(j=0;j<c;j++) {
cout<<"a["<<i<<"] ["<<j<<"] : ";
cin>>a[i][j];
if(i==j) {
sum+=a[i][j];
}
}
}
cout<<"sum of the diagonal of two dimensional array: "<<sum;
getch();
}
program to find numbers greater, less or equal to zero using an array in C++
a. Number of values greater than zero.
b.Number of values equal to zero.
c. Number of values less than zero.
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
float a[20];
int gz=0,lz=0,z=0;
for(int i=0;i<20;i++) {
cout<<"\nenter value "<<i<<" : ";
cin>>a[i];
if(a[i]>0)
gz++;
else if(a[i]<0)
lz++;
else
z++;
}
cout<<"\nTotal numbers greater than zero: "<<gz;
cout<<"\nTotal numbers less than zero: "<<lz;
cout<<"\nTotal numbers that are zero: "<<z;
getch();
}
program to find the largest number using user defined function
PROGRAM:
#include <iostream.h>
#include <conio.h>
int largest(int,int);
void main() {
clrscr();
int a,b;
cout<<"PROGRAM TO FIND LARGEST NUMBER\n";
cout<<"Enter first value : ";
cin>>a;
cout<<"Enter second value: ";
cin>>b;
cout<<largest(a,b)<<" is largest";
getch();
}
int largest(int a,int b) {
if(a>b)
return a;
else
return b;
}
program to compute cube, Using inline function in c++
PROGRAM:
#include <iostream.h>
#include <conio.h>
inline int cube(int r) {
return r*r*r;
}
void main() {
clrscr();
int r;
cout<<"PROGRAM TO COMPUTE CUBE\n";
cout<<"Enter value to compute cube: ";
cin>>r;
cout<<"cube of the number: "<<cube(r);
getch();
}
program to count vowel alphabets using c++
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <ctype.h>
void main() {
clrscr();
int a=0,e=0,i=0,o=0,u=0;
char ch;
cout<<"Program to count number of a,e,i,o,u in a sentence \n";
cout<<"Enter your sentence: \n";
while((ch=getche())!='\r') {
switch(tolower(ch)) {
case 'a': a++;
break;
case 'e': e++;
break;
case 'i': i++;
break;
case 'o': o++;
break;
case 'u': u++;
break;
}//end of switch
}//end of while
cout<<"\nTotal Number of a: "<<a;
cout<<"\nTotal Number of e: "<<e;
cout<<"\nTotal Number of i: "<<i;
cout<<"\nTotal Number of o: "<<o;
cout<<"\nTotal Number of u: "<<u;
getch();
}//end of program
program to count number of words in c++
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int wrdcnt=1;
char ch;
cout<<"Program to count number of WORDS entered by the user: \n";
cout<<"Enter your sentence: \n";
while((ch=getche())!='\r')
{
if(ch==' ')
wrdcnt++;
}
cout<<"\ntotal words in above sentence: "<<wrdcnt;
getch();
}
program to count number of characters
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <string.h>
void main() {
clrscr();
int len;
char ch[200];
cout<<"Program to count number of characters entered by the user: \n";
cout<<"Enter your sentence: \n";
gets(ch);
len=strlen(ch);
cout<<"\ntotal number of characters: "<<len;
getch();
}
program to check alphabet or digit, uppercase or lowercase
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
void main() {
clrscr();
char ch;
cout<<"Enter a characher: ";
ch=getchar();
if(isalpha(ch)) {
cout<<ch<<" is an alphabet\n";
if(islower(ch))
cout<<ch<<" is in lowercase\n";
else
cout<<ch<<" is in uppercase\n";
}else
cout<<ch<<" is not an alphabet\n";
getch();
}//end of the program
program to convert uppercase to lowercase and vice versa
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
void main() {
clrscr();
int len,chr;
char ch[50];
cout<<"Program to conver lowercase to uppercase and uppercase to lowercase:\n";
cout<<"Enter Your Name: ";
gets(ch);
len=strlen(ch);
for(int i=0;i<len;i++) {
chr = ch[i];
if(isupper(chr))
chr = tolower(chr);
else
chr = toupper(chr);
putchar(chr);
}
getch();
}//end of the program
program to find alphabet, digit or symbol
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
void main() {
clrscr();
char ch;
cout<<"Enter either digit, alphabet or special symbol: ";
ch=getchar();
if(isalpha(ch)) {
cout<<"You entered alphabet\n";
}
else if(isdigit(ch)) {
cout<<"You entered digit\n";
}
else
cout<<"You entered special symbol";
getch();
}//end of the program
program to find positive, negative and zero integers
Write a program that will ask the user to enter N integers. Find out the following:
(i) Total number of positive integers
(ii) Total number of negative integers
(iii) Total number of integers equal zero
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int terms,num,pi=0,ni=0,z=0;
cout<<"how many integers do you want to enter: ";
cin>>terms;
for(int i=1;i<=terms;i++) {
cout<<"enter num "<<i<<" : ";
cin>>num;
if(num==0)
z++;
else if(num>0)
pi++;
else
ni++;
}
cout<<"\nTotal Positive Integers: "<<pi;
cout<<"\nTotal Negative Integers: "<<ni;
cout<<"\nTotal Integers which are zero: "<<z;
getch();
}//end of the program
program to find even or odd number, prime number or not
Write a program that will find out whether the given number is even or odd. If odd number then whether it is prime or not.
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int num,flag=0;
cout<<"Enter the number to find odd or even: ";
cin>>num;
if((num%2)==0) {
cout<<num<<" is even\n";
}//end of if
else {
cout<<num<<" is odd\n";
for(int i=3;i<=7;i+=2) {
if(num%i==0){
flag=1;
break;
}
}//end of for
if(flag==1)
cout<<num<<" is not a prime number\n";
else
cout<<num<<" is a prime number\n";
}//end of else
getch();
}//end of the program
program to find factorial in C++ upto a given term
Write a program that will take a number N as input and print the factorial of all numbers from 1 to N
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
long int fact;
int num, temp;
cout<<"Enter the number upto which you wan to generate factorial: ";
cin>>num;
cout<<”Program Factorial: “;
for(int i=1;i<=num;i++) {
temp=i;
fact=1;
while(temp>0) {
fact*=temp;
temp--;
}
cout<<"\n"<<i<<" ! = "<<fact;
}//end of for loop
getch();
}//end of the program
program to find sum of the series in c++
Write a program to find out the sum of the series
1 + x + x2 + x2 ................ xn
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main() {
clrscr();
int term,num;
long int sum=1;
cout<<"Enter the number to take the series of: ";
cin>>num;
cout<<"Enter the term for which the series is to be taken: ";
cin>>term;
for(int i=2;i<=term;i++) {
sum+=pow(num,i);
}
cout<<"The sum of the series: "<<sum;
getch();
}//end of the program
quadratic equation in c++
Determine the roots of the quadratic equation
ax2 + bx + c = 0
using the formula of quadratic equation
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
void main() {
clrscr();
double a,b,c,d;
cout<<"Enter the values of A, B and C for the given quadratic equation";
cout<<"\nEnter value A: ";
cin>>a;
cout<<"\nEnter value B: ";
cin>>b;
cout<<"\nEnter value C: ";
cin>>c;
if(sqrt(pow(b,2)-(4*a*c)<0)) {
cout<<"\ndeterminant is less than 0, \nroots of the quadratic equation cannot be found";
getch();
exit(0);
}
d=sqrt(pow(b,2)-(4*a*c));
cout<<"\nThe roots of the quadratic equation are: \n";
cout<<"\nRoot 1: "<<-b+d/(2*a);
cout<<"\nRoot 2: "<<-b-d/(2*a);
getch();
}//end of the program
pythagorean triplets in c++
Write a program that will find pythagorean triplets between 1 to 100.
(Pythagorean triplets are such that the square of one number is equal to the
sum of the squares of the other two numbers).
Example : 3, 4, 5
52 = 32 + 42
25 = 9 + 16 = 25
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main() {
clrscr();
cout<<"pythagorean triplets between 1 to 100 are:\n";
for(int i=1;i<=100;i++) {
for(int j=i+1;j<=100;j++) {
for(int k=j+1;k<=100;k++) {
if(pow(i,2) + pow(j,2) == pow(k,2))
cout<<i<<" "<<j<<" "<<k<<"\n";
}
}
}
getch();
} //end of the program
printing table in c++
Write a program to print all the tables between 2 and 20 upto 10.
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
for(int j=2;j<=20;j++) {
for(int i=1;i<=10;i++) {
cout<<j<<" X "<<i<<" = "<<j*i<<"\n";
}
cout<<"\n";
}
getch();
}//end of the program
factorial in C++
Write a program that will print the factorial of any number.
input : 5
Output : 5 x 4 x 3 x 2 x 1 = 120
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
long int fact=1;
int num;
cout<<"Enter the number to generate factorial: ";
cin>>num;
while(num>0) {
fact*=num;
num--;
}
cout<<"The factorial is "<<fact;
getch();
}//end of the program
print a table in c++ using definite loop
Write a program that will print the table of any number upto any number.
input :
Enter the table of which number : 4
Enter upto which number : 5
Output
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
Continue (Y/N) ?
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
long int num,term;
cout<<"Enter the number for the table: ";
cin>>num;
cout<<"Enter the term upto which the table is to be generated: ";
cin>>term;
cout<<"The table is:\n";
for(int i=1;i<=term;i++)
cout<<num<<" X "<<i<<" = "<<num*i<<" \n";
getch();
}//end of the program
reverse a number in c++, indefinite loop
Write a program to reverse a given number.
PROGRAM
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
long int num1,num2=0,r;
cout<<"Enter number to reverse: ";
cin>>num1;
cout<<"\nThe reversed number: ";
while(num1>0) {
r=num1%10;
num2=num2*10+r;
num1=num1/10;
}
cout<<num2;
getch();
}//end of the program
fibonacci series in C++
Write a program to generate the members of fibonacci series upto 500.
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
double fib1=0, fib2=1,fib3;
cout<<"The fibonacci series for 500 numbers is given below:\n";
cout<<fib2;
for(int i=0;i<100-1;i++) {
fib3=fib1+fib2;
cout<<"\t"<<fib3;
fib1=fib2;
fib2=fib3;
}
getch();
}//end of the program
for loop in C++, iteration
Write a program that will print sum of N integers entered by the user.
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int terms,num,sum=0;
cout<<"Please enter the number of terms to add: ";
cin>>terms;
for(int i=1;i<=terms;i++) {
cout<<"enter number "<<i<<": ";
cin>>num;
sum+=num;
}
cout<<"Your sum is: "<<sum;
getch();
}//end of the program
conditional statement in C++
Write a program that will accept a mark and assign letter grade according to
the following table.
Grade Mark
A > = 90
B > = 80 but < 90
C > = 70 but < 80
D > = 60 but < 70
F < 60
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
clrscr();
int mark, grade;
cout<<"Enter your mark: ";
cin>>mark;
if(mark>=90)
cout<<"\nYour grade is A";
else if(mark>=80 && mark<90)
cout<<"\nyour grade is B";
else if(mark>=70 && mark<80)
cout<<"\nyour grade is C";
else if(mark>=60 && mark<70)
cout<<"\nyour grade is D";
else
cout<<"\nyour grade is F";
getch();
}//end of the program
Using Case Statement in C++, menu driven program in C++
MENU
1. Create a data file
2. Display a data file
3. Edit a data file
4. Exit
Choice :
PROGRAM:
#include <iostream.h>
#include <conio.h>
void main() {
int choice;
clrscr();
cout<<"MENU";
cout<<"1. Create a data file\n";
cout<<"2. Display a data file\n";
cout<<"3. Edit a data file\n";
cout<<"4. Exit\n\n";
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice) {
case 1: cout<<"you entered choice 1\n";
cout<<"Create a data file\n";
break;
case 2: cout<<"you entered choice 2\n";
cout<<"Display a data file\n";
break;
case 3: cout<<"you entered choice 3\n";
cout<<"3. Edit a data file\n";
break;
case 4: cout<<"you entered choice 4\n";
cout<<"Exit";
break;
default: cout<<"Please enter a valid choice";
}
getch();
}//end of program