Академический Документы
Профессиональный Документы
Культура Документы
Program to find the day of the week of the date entered by the user when
the day of the New Year is given or inputted.
#include<iostream.h> //inclusion of preprocessor directives
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main() //definition of main() function
{
char date[11],day[7][10]={"sunday","monday","tuesday","wednesday",
"thursday","friday","saturday"};
char wd[10]; //initialization of variables
int f,i,p,a[3],j=0,n,nd[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int d=0,m=1,b;
do //do-while loop
{ f=0;
cout<<"Date: ";gets(date);
for(i=0;date[i]!='\0';i++) //for loop
{ n=0;
while(date[i]!='/' && date[i]!='\0') //while loop
{ n=n*10+date[i]-48; i++; }
a[j++]=n;
}
if(a[2]%400==0||a[2]%4==0 && a[2]%100!=0) //if statement
nd[2]=29;
if(a[2]<1||a[1]<1||a[1]>12||a[0]<1||a[0]>nd[a[1]])
f=1;
}while(f==1); //do-while loop closes
do //do-while opens
{
cout<<"Day on 1st jan :";
gets(wd);
for(p=0;p<7;p++) //for loop
{ if(strcmp(wd,day[p])==0)//if statement
{ break;}
}
}while(p==7); //loop closes
p--;
do //do-while loop
{
d++;p++; //calculation
if(p==7)
p=0;
if(d>nd[m]) //if statement
{ d=1;m++;}
}while(d!=a[0]||m!=a[1]);
cout<<"Day on "<<date<<" :"<<day[p];
}
OUTPUT
Date: 6/6/2007
Day on 1st jan :monday
Day on 6/6/2007 :wednesday
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. main() function declaration and definition.
4. Initialization and declaration of program variables.
5. do-while loops to calculate the days and dates to produce the
output.
6. Printing the output onto the screen.
7. main() function closes and termination of program.
VARIABLE LIST
1. date[] Char Array to store the days
2. days[][] Char To store the days in a
week
3. wd[] Char Acts as a sort of counter
of the days
4. a[] Int Array to act as counter
5. I Int Loop variable
6. f Int Counter
7. j Int Loop variable
8. nd[] Int Stores the number of
days of each month.
9. d Int Counter
10. m Int Counter
11. b Int Counter
12. p Int Loop variable
OUTPUT
What is the size of the system(n)
3
Enter coefficients a(I,j)rowwise
2 1 3
4 4 7
2 5 9
Input vector b
1 1 3
Solution vector X
-0.5 -1 1
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration of member functions.
4. Initialization and declaration of program variables.
5. Obtaining solution to the matrix entered by user through gauss
elimination method by calling member functions in the main()
function definition.
6. main() function closes and function definition starts.
7. Mechanism for obtaining solution through gauss elimination
method.
8. Program terminates along with termination of functions.
VARIABLE LIST
1. n Int Parameter variable
2. a[][] Float Parameter variable
3. b[] Float Parameter variable
4. x[] Float Parameter variable
5. status Int Counter
6. I Int Loop variable
7. j Int Loop variable
8. n Int Stores the size of the
system
9. k Int Loop variable
10. sum Float Stores the sum of
elements inside a loop.
OUTPUT
More elements?(y/n)
N
The largest element of the array is: 8
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of member functions.
4. Mechanism to obtain the largest element in a given array.
5. Function closes.
6. main() function definiton.
7. Initialization and declaration of program variables.
8. Obtaining the largest element of the array that is entered by the
user.
9. Printing the largest element.
10.main() function closes.
11.Program terminates along with termination of main() function.
VARIABLE LIST
1. n Int Parameter variable
2. arr[] Float Parameter variable
3. ch Char Stores the answer
entered by user
4. I Int Loop variable
5. amount[] Float Stores the array entered
by user
4. Program to find whether saddlepoint for a user-entered matrix exists or
not.
}
c=0;
for(j=0;j<n;j++) //for loop
{
max=A[0][j];
for(i=0;i<m;i++)
{
if(A[i][j]>max) //calculating maximum of each
column
{
max=A[i][j];
}
}
b[1][c++]=max; //storing maximum
}
max=b[0][0];
min=b[1][0];
for(i=0;i<m;i++) //for loop
{
for(j=0;j<n;j++)
{
if(i==0)
{
if(b[i][j]>max) //calculations
max=b[i][j];
}
if(i==1) //if statement
{
if(b[i][j]<min)
min=b[i][j];
}
}
}
if(max==min) //if statement
cout<<"Saddle point exists at "<<max;
else
cout<<"Saddle point doesn’t exist.";
getch();
}
OUTPUT
Enter the number of rows: 2
Enter the number of columns: 2
2 3
4 5
Saddle point exists at 4
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Inputting the matrix on which the operation is to be done.
5. Calculating whether saddle point exists in the matrix or not.
6. Printing whether saddle point exists and if it exists, the position of
the point.
7. main() function closes.
8. Program terminates along with termination of main() function.
VARIABLE LIST
1. A[][] Int Array on which
operation is to be
performed.
2. I Int Loop variable
3. j Int Loop variable
4. m Int Stores the no of rows of
the matrix
5. n Int Stores the number of
columns of the matrix.
6. b[][] Int Stores the maximum
and minimum of each
row and column.
7. max Int Stores the maximum of
each column
8. min Int Stores the minimum of
each row.
9. c Int Counter.
OUTPUT
Enter an octal number.: 65
The binary equivalent is: 110101
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Mechanism to convert from a given octal number to its binary
equivalent.
5. Function closes.
6. Definition of main() function.
7. inputting the octal number from the user and printing the binary
equivalent by callin function octobin().
8. main() function closes.
9. Program terminates along with termination of main() function.
VARIABLE LIST
1. oct Int Parameter variable
2. binn Long Stores the binary
equivalent.
3. a[] Int Counter.
4. d1 Int Stores the last digit of
the octal number
5. d2 Int Stores the first digit of
the octal number.
6. q Int Stores the quotient of
d1, d2 divided by 2.
7. r Int Stores the remainder of
d1, d2 divided by 2.
8. i Int Loop variable.
#include<iostream.h>
#include<conio.h> //including preprocessor directives
#include<stdio.h>
void main() //main() function
{
int a[2][2],rs[2],cs[2]; int i,j; //initialization of variables
cout<<"Enter elements of matrix ";
for(i=0;i<2;i++) //for loop
{
for(j=0;j<2;j++)
{
cin>>a[i][j]; //inputting each element
}
}
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Initialization and declaration of program variables.
5. Mechanism to find row sum and column sum of a matrix.
6. Printing the matrix along with the row sum and column sum.
7. main() function closes.
8. Program terminates along with termination of main() function.
VARIABLE LIST
1. a[][] Int Matrix on which
operation is to be
performed.
2. rs[] int Stores the row sum of
the matrix.
3. cs[] Int Stores the column sum
of the matrix..
4. I Int Loop variable.
5. j Int Loop variable
7.Program to find embedded or hidden primes in a number entered by the
user.
OUTPUT
Enter a number :
5681
Embedded prime is : 5
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Initialization and declaration of program variables.
5. Mechanism to find embedded prime in the number.
6. Printing the embedded prime.
7. main() function closes.
8. Program terminates along with termination of main() function.
VARIABLE LIST
1. a[] Int Stores the digits of the
number.
2. f[] int Stores the frequency of
each digit.
3. cs[] Int Stores the column sum
of the matrix..
4. I Int Loop variable as well as
stores the embedded
prime.
5. j Int Loop variable
6. d Int Counter
7. n Int Stores the number of
digits of the number.
8. dig Int Counter
9. r Int Counter.
OUTPUT
Enter the string:
HAPPY
Y H
PY HA
PPY HAP
APPY HAPP
HAPPY HAPPY
APPY HAPP
PPY HAP
PY HA
Y H
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Initialization and declaration of program variables.
5. Inputting the string on which the operation is to be performed.
6. Mechanism to produce the given pattern.
7. Printing the pattern.
8. main() function closes.
9. Program terminates along with termination of main() function.
VARIABLE LIST
1. a[] Char Stores the string
2. i int Loop variable
3. j Int Loop variable
4. l Int Stores the length of
string
5. k Int Stores the limit of the
loop
}
}
}
getch();
}
OUTPUT
Enter number:
5
Possible combinations
5
41
32
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Initialization and declaration of program variables.
5. Inputting the number on which the operation is to be performed.
6. Mechanism to produce the combinations and print them.
7. main() function closes.
8. Program terminates along with termination of main() function.
VARIABLE LIST
1. n Int Stores the number.
2. i int Loop variable
3. j Int Loop variable
4. copy Int Stores the copy of the
number of iterations.
5. k Int Loop variable.
6. s Int Counter.
10. Program to find lowest and highest factors of a number entered by
the user and to find the sum of all primes between the lowest and
highest factors.
getch();
}
OUTPUT
Enter number: 14
Lowest and highest factors are:
2 7
The sum is: 17
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration and definition of main() function.
4. Initialization and declaration of program variables.
5. Inputting the number on which the operation is to be performed.
6. Printing the lowest and highest factors of the number.
7. Printing the sum of the prime numbers between the lowest and
highest factors.
8. main() function closes.
9. Program terminates along with termination of main() function.
VARIABLE LIST
1. n Int Stores the number.
2. i int Loop variable
3. j Int Loop variable
4. l Int Stores the lowest factor
5. k Int Loop variable.
6. s Int Stores the sum.
7. h Int Stores the highest
factor.
8. p Int Counter.
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration of member functions.
4. Initialization and declaration of program variables.
5. Opening link to file.
6. Displaying initial contents of the file.
7. Mechanism to append information to the file.
8. Printing the revised file.
9. Closing the link to the file.
10.main() function closes.
11.Program terminates along with termination of function.
VARIABLE LIST
1. ch Char Stores content of file.
2. fin Link to file for input.
3. fout Link to file for output.
OUTPUT
The contents of stunames file are given below
Deepti dhamija
Mahndra chaurasia
Lokesh Vasvani
The contents of stumarks file are given below
78.92
72.17
69.33
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration of member functions.
4. Initialization and declaration of program variables.
5. Opening link to file.
6. Displaying contents of the file.
7. Opening link to file.
8. Displaying contents of the file
9. Closing the link to the file.
10.main() function closes.
11.Program terminates along with termination of function.
VARIABLE LIST
1. line[] Char Stores each line of text
file.
2. filout Link to file for output.
3. filin Link to file for input.
OUTPUT
Deepa Mehta has the balance Rs. 21310.75
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a structure customer.
4. Declaration of variables.
5. Structure closes.
6. Definition of main() function.
7. Mechanism to print the balance of the customer.
8. Closing the links to the file.
9. main() function closes.
10.Program terminates along with termination of function.
VARIABLE LIST
1. name[] Char Stores name of
customer.
2. balance Float Stores the balance of
the customer.
3. fin Link to file for input.
4. fout Link to file for output.
4.Program to add, modify,and display records from a file.
1. Add record.
2. Modify record.
3. Display record.
4. Exit.
Enter your choice…(1-4)…
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a class student.
4. Declaration of variables.
5. Declaration of member functions in the public section of the class.
6. Class closes.
7. Definition of member functions.
8. main() function definition.
9. Initialization and declaration of variables.
10.Printing a menu using switch statement for users to add, modify,
and delete records to the file.
11.main() function closes.
12.Program terminates along with termination of function.
VARIABLE LIST
1. name[] Char Stores name of student.
2. marks Int Stores the marks of a
student.
3. grade Char Stores the grade of a
student.
4. ch Char Stores each character of
the file.
5. count Int Counter.
6. I Int Loop variable
7. nm[] Char Prototype of name[]
8. gr Char Prototype of grade
9. mk Char Prototype of marks
10. choice Char Stores user’s choice in
switch statement.
#include<iostream.h> //including
preprocessor directives
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
int main() //main() function definition
{
clrscr();
char filename[13]; //variable declaration
cout<<”Enter filename:\n”;
cin.getline(filename,13);
ifstream infile(filename); //open file
if(!infile)
{
cout<<”Sorry cannot open “<<filename<<” file\n”;
exit(-1);
}
int no_bytes=0; //initializing variables
char ch;
while(cin.get(ch))
no_bytes++;
cout<<”File size is “<<no_bytes<<”bytes”; //printing the size of
file
return 0;
}
OUTPUT
Enter filename:
Test.dat
This is test run
File size is 32 bytes
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Declaration of variables.
4. Creation of link to open file.
5. Mechanism to calculate the size of the file when the file has been
opened.
6. Printing the file size.
7. main() function closes.
8. Program terminates along with termination of function.
VARIABLE LIST
1. filename[] Char Stores name of file.
2. ch Char Stores each character of
file.
3. no_bytes Int Stores the size of the
file.
4. infile Link to file for reading.
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a structure employee.
4. Declaration of variables.
5. Structure closes.
6. Definition of main() function.
7. Declaration of variables.
8. Creation of link to open file.
9. Opening file.
10.Reading file and printing the contents.
11.Closing the links to the file.
12.main() function closes.
13.Program terminates along with termination of function.
VARIABLE LIST
1. name[] Char Stores name of
employee.
2. empcode Int Stores employee code.
3. hrsworked Int Stores hours worked by
each employee.
4. rate Int Stores rate of payment.
5. wages Float Stores wages earned per
week by each
employee.
6. fin Link to file for reading
the contents.
OUTPUT
1 2 3 4 5
2 4 6 8 10
3 5 9 12 15
4 8 12 16 20
5 10 15 20 25
VARIABLE LIST
1. width Int Stores the width of the
tab.
2. row Int Stores the row.
3. col Int Stores the column.
OUTPUT
Student 1
Roll no: 125 Percentage: 72.5 Grade: B
Student 2
Roll no: 569 Percentage: 80.5 Grade: A
Student 3
Roll no: 856 Percentage: 92 Grade: A
Student 4
Roll no: 253 Percentage: 60 Grade: B
Student 5
Roll no: 225 Percentage: 85.6 Grade: A
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a structure stu.
4. Declaration of variables.
5. Structure closes.
6. Declaration of second structure res.
7. Declaration of variables.
8. Structure closes.
9. Definition of main() function.
10.Creation of link fin and fout to read data from ‘studata’ and writing
data to ‘result’.
11.Opening file ‘studata’ and creating file ‘result’.
12.Printing the contents ‘result’.
13.Closing the links to the file.
14.main() function closes.
15.Program terminates along with termination of function.
VARIABLE LIST
1. name[] Char Stores name of studentr.
2. rollno Int Stores roll no of
student.
3. class Int Stores class of student.
4. perc Float Stores percentage of
student.
5. grade Char Stores grade of student
6. marks[] Int Stores marks of
student.
7. ifin Link to file for reading
contents from ‘studata’.
8. fout Link to file for writing
to file ‘result’.
9. I Int Loop variable.
9.Program to display the contents of a file using get() function.
OUTPUT
THIS IS A TEST RUN OF THE ABOVE PROGRAM.
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of main() function.
4. Declaration of variables.
5. Creation of link to open file.
6. Printing the contents of the file..
7. Closing the links to the file.
8. main() function closes.
9. Program terminates along with termination of function.
VARIABLE LIST
1. ch Char Stores each character of
the file.
2. fin Link to read contents
from the file.
#include<iostream.h>
//including preprocessor
#include<fstream.h>
//directives
#include<conio.h>
class student{
//declaring class
char name[5];char grade; //declaring
variables
float marks;
public:
void getdata(void) //declaring
member functions
voide display(void);
};
void student::getdata(void) //member function defintion
{
char ch; //declaring variable
cin.get(ch);
cout<<”Enter name:”; cin.getlin(name,40); //prompting user
cout<<”Enter grade:”; cin>>grade;
cout<<”Enter marks:”; cin>>marks;
cout<<’\n”;
} //function closes
OUTPUT
Enter details for 3 students
ALGORITHM
1. Start.
2. Inclusion of preprocessor directives.
3. Definition of a class student.
4. Declaration of class variables.
5. Declaration of member functions in public section of class.
6. Class closes.
7. member functions definitions.
8. main() function starts.
9. Declaration of variables.
10.Mechanism to read data entered by user, writing them onto the file
and displaying them.
11.Closing the links to the file.
12.main() function closes.
13.Program terminates along with termination of function.
VARIABLE LIST
1. name[] Char Stores name of student.
2. grade Char Stores the grade of
student.
3. marks Float Stores the marks of
student.
4. arts[] Student Object array of class to
access member
functions.
5. filin Link to file for reading
data from file.
6. I Int Loop variable.