C and C++ Batch 2
C and C++ Batch 2
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
#include
<stdio.h> void
main()
{
int n, reversedNumber = 0, remainder;
clrscr();
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10+
remainder; n /= 10;
}
printf("\nReversed Number = %d", reversedNumber);
getch();
}
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
1.Write a c program to display output using ID
array ALGORITHM:
Step 1:Start the program
Step 2: Declare the variables
Step 3:User to enter the number of elements in the array
Step 4:User to enter the array elements.
Step 5:Read the elements of the array from the
user. Step 6:Print the array elements.
Step 7:End the program
FLOWCHART:
START
Readn
Displayvalues
STOP
PROGRAM:
OUTPUT:
array ALGORITHM:
Step 1:Start the program
START
ReadI,j,n,m
Displaymatrix
STOP
PROGRAM:
OUTPUT:
array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of rows and columns of matrix A
Step 4:User to enter the number of rows and columns of matrix B
Step 5:If compatible, proceed with matrix addition, else print a message
indicating it's not possible.
Step 6:Read the elements of matrix A from the user.
Step 7:Read the elements of matrix B from the user.
Step 8: Addition corresponding elements of matrices A and B and store the
result in matrix C.
Step 9:End the program
FLOWCHART:
Start
ReadI,j,m,n
false
If((m==p)&
Matrixaddition
&(n==q))
is not possible
true
Fori=0tom
Forj=0ton
Reada[i][j],b[i][j]
C[i][j]=a[i][j]+b[i][j]
Displayc[i][j]
Stop
PROGRAM:
OUTPUT:
array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of rows and columns of matrix A
Step 4:User to enter the number of rows and columns of matrix B
Step 5:If compatible, proceed with matrix multiplication, else print a message
indicating it's not possible.
Step 6:Read the elements of matrix A from the user.
Step 7:Read the elements of matrix B from the user.
Step 8:Multiplication corresponding elements of matrices A and B and store
the result in matrix C.
Step 9:End the program
FLOWCHART:
Start
ReadI,j,m,n
false
If((m==p)&
Matrixmultiplicationi
&(n==q))
s not possible
true
Fori=0tom
Forj=0ton
Reada[i][j]
a[i][j],b[i][j]
C[i][j]=c[i][j]+a[i][k]*b[k][j]
DisplayC[i][j]
Stop
PROGRAM:
OUTPUT:
array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of rows and columns of matrix A
Step 4:User to enter the number of rows and columns of matrix B
Step 5:If compatible, proceed with matrix subtraction, else print a message
indicating it's not possible.
Step 6:Read the elements of matrix A from the user.
Step 7:Read the elements of matrix B from the user.
Step 8:Subtract corresponding elements of matrices A and B and store the
result in matrix C.
Step 9:End the program
FLOWCHART:
Start
ReadI,j,m,n
false
If((m==p)&
Matrixsubtractionis
&(n==q))
not possible
true
Fori=0tom
Forj=0ton
Reada[i][j]
a[i][j],b[i][j]
C[i][j]=a[i][j]-b[i][j]
Displayc[i][j]
Stop
PROGRAM:
OUTPUT:
array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the size of array
Step 4:Read the size of array
Step 5:User to enter the elements in the array
FLOWCHART:
START
Readn
Sum=sum+arr[i]
Displaysum
STOP
PROGRAM:
OUTPUT:
7.write a c program to find the max element from n
numbers ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of elements in the array
Step 4:Read the number of elements from the user
Step 5:User to enter the elements
START
Readn
Fori=0ton
If(a[i]>larg
Larg=a[0]
Displaylargest
number
STOP
PROGRAM:
OUTPUT:
array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of elements in the array
Step 4:Read the number of elements from the user
Step 5:User to enter the elements
Step 6:Read the elements from the user.
Step 7:Print the sorted array in ascending order
Step 8:End the program
FLOWCHART:
START
Readn
Fori=0ton
If(i==n)
j=i+1ton
If(arr[j]<arr[i])
temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
Displayascending
order
STOP
PROGRAM:
OUTPUT:
program ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of elements in the array
Step 4:Read the number of elements from the user
Step 5:User to enter the elements
Step 6:Read the elements from the user.
Step 7:User to enter the element to search
Step 8:Read the element to search from the user.
Step 8:Perform linear search
Step 9:End the program
FLOWCHART:
START
Readn
Fori=0ton
If(i==n)
If(a[i]==search) If(i==n)
Larg=a[0] Elementisnotinthearray
Displaylargest
number
STOP
PROGRAM:
OUTPUT:
array ALGORITHM:
Step 1:Start the program
Step 2: Declare variables
Step 3:User to enter the number of elements in the array
Step 4:Read the number of elements from the user
Step 5:User to enter the elements
START
Readn
Fori=0ton If(i==n)
j=i+1ton
If(arr[j]>arr[i])
temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
Display
descendingorder
STOP
PROGRAM:
#include<stdio.h>
#include<conio.h
> void main()
{
int arr[50];
int
i,n,j,temp;
clrscr();
printf("\n\t\t DESCENDING ORDER OF A PROGRAM USING ARRAY:");
printf("\n\t\t -- ");
printf("\nenter the no.of elements in the array");
scanf("%d",&n);
printf("\nEnter the elements");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[j]>arr[i])
{
temp=arr[i]
;
arr[i]=arr[j];
arr[j]=temp
;
}
}
}
printf("\nThe descending order is:");
for(i=0;i<n;i++)
{
printf("\n%d",arr[i]);
}
getch();
}
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
OUTPUT:
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
fputc(c, fptr2);
c=
fgetc(fptr1);
}
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;
}
INPUT:
OUTPUT:
EXP:5 PROGRAM USING FILE
Storing Employee information
Step 1: Start the program.
Step 2: Declare the file and integers.
Step 3: Open the file in witting mode.
Step 4: Prompt the user to enter an integer id.
Step 5: Prompt the user to enter a string name.
Step 6: Prompt the user to enter a float salary.
Step 7: Write id, name, and salary to the file using fprintf.
Step 8: Close the file.
Step 9: Open the file in read mode.
Step 10: Read the integer,string,float from the file until end of file.
Step 11: Display the integer,string,float and Close the file.
Step 12: Stop the program.
PROGRAM:
#include
<stdio.h> int
main()
{
FILE *fptr;
int id;
char name[30];
float salary;
fptr = fopen("E:student1.txt","w”);
scanf("%d",&id);
scanf("%s", name);
scanf("%f",
&salary);
fclose(fptr);
fptr = fopen("E:student1.txt","r");
printf("Id=%d\n",id);
printf("Name=%s\n",name);
printf("salary=%.2f\n",
salary); fclose(fptr);
}
OUTPUT:
1. simple C++ program to add subtract multiply and divide two
numbers ALGORITHM:
Step 1:Start the program
Step 2:Declare variables in integers.
Step 3:Read the value of num1,num2 entered by the user.
Step 4:Calculate the sum of num1 and num2.
Step 5:Display the result of the sum.
Step 6:Calculate the difference between num1 and num2.
Step 7:Display the result of the difference.
Step 8:Calculate the product of num1 and num2.
Step 9:Display the result of the product.
Step 10:Calculate the division of num1 and num2.
Step 11:Display the result of the division.
Step 12:Stop the program
FLOWCHART:
START
Declareintegers
Readnum1,num
2
num1+num2
Displayadd
num1-num2
DisplaySub
num1*num2
DisplayMulti
num1/num2
DisplayDiv
STOP
PROGRAM:
#include<iostream>
using namespace std;
int main()
{
// declare variables
int num1, num2; // take input from end-user
cin >> num1>>num2;
ALGORITHM:
Step 1:Start the program
Step 2:Declare variables in integers.
Step 3:Read the value of num1,num2 entered by the user.
Step 4: Call add method by passing num1 and num2 as arguments and store the result in a.
Step 5: Display the result of a with label "Addition".
Step 6: Call subtract method by passing num1 and num2 as arguments and store the result
in b.
Step 7: Display the result of b with label "Subtraction".
Step 8: Call muliply method by passing num1 and num2 as arguments and store the result in
c.
Step 9: Display the result of c with label "Muliplication".
Step 10: Call divide method by passing num1 and num2 as arguments and store the result in
d.
Step 11: Display the result of d with label "Division".
Step 12:Stop the program
FLOWCHART:
START
Declareintegers
Readnum1,num
2
a=ar.add(num1,num2)
DisplayAddition=
b=ar.subtracrt(num1,num2)
DisplaySubtraction=
c=ar.multiply(num1,num2)
DisplayMultiplication=
d=ar.divide(num1,num
DisplayDivision=
STOP
PROGRAM:
#include<iostream.h>
using namespace std;
class Arithmetic
{ public:
// function to add two numbers
int add(int n1, int n2)
{
return n1+n2;
}
// function to subtract two numbers
int subtract(int n1, int n2)
{
return n1-n2;
}
// function to multiply two numbers
int multiply(int n1, int n2)
{
return n1*n2;
}
// function to divide two numbers
int divide(int n1, int n2)
{
return n1/n2;
}
};
int main()
{
// declare variables
int num1, num2;
int a,b,c,d;
// declare object of Arithmetic class
Arithmetic ar;
cin >> num1 >> num2; a=ar.add(num1, num2);
b=ar.subtract(num1, num2);
c=ar.multiply(num1, num2);
d=ar.divide(num1, num2);
return 0;
}
OUTOUT:
START
Declareintegers
Readnum1,num
2
a=ar.add(num1,num2)
DisplayAddition=
b=ar.subtracrt(num1,num2)
DisplaySubtraction=
c=ar.multiply(num1,num2)
DisplayMultiplication=
d=ar.divide(num1,num
DisplayDivision=
STOP
PROGRAM:
#include<iostream.h>
using namespace std;
class Arithmetic
{ public:
// function to add two numbers
int add(int n1, int n2);
// function to subtract two numbers
int subtract(int n1, int n2);
// function to multiply two numbers
int multiply(int n1, int n2);
// function to divide two numbers
int divide(int n1, int n2);
};
int Arithmetic::add(int n1, int n2)
{
return n1+n2;
}
int Arithmetic::subtract(int n1, int n2)
{
return n1-n2;
}
int Arithmetic::multiply(int n1, int n2)
{
return n1*n2;
}
int Arithmetic::divide(int n1, int n2)
{
return n1/n2;
}
int main()
{
// declare
variables int num1,
num2; int a,b,c,d;
// declare object of Arithmetic class
Arithmetic ar;
cin >> num1 >> num2; a=ar.add(num1, num2);
// addition of two number
cout << "Addition="<<a<< endl;
b=ar.subtract(num1, num2);
// subtraction of two number
C++ ALGORITHM:
Step 1:Start the program
Step 2: Define the student class with private member variables: name, rollNo, total, and
perc.
Step 3:Define two public member functions: getDetails and putDetails.
Step 4:In the getDetails function, get the student's name, roll number, and total marks from
the user. Calculate the percentage and store it in the perc member variable.
Step 5:In the putDetails function, print the student's details to the console.
Step 6:In the main function, create an object of the student class.
Step 7:Call the getDetails function to get the student's details.
Step 8:Call the putDetails function to print the student's details.
Step 9:Return 0 to indicate successful execution of the program.
Step 10:End the program
FLOWCHART:
PROGRAM:
#include <iostream.h>
#include <iomanip.h>
using namespace;
class student
{ private:
char
name[30]; int
rollNo;
int total;
float
perc;
public:
// member function to get student's details
void getDetails(void);
// member function to print student's
details void putDetails(void);
};
// member function definition, outside of the class
void student::getDetails(void)
{
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number:
"; cin >> rollNo;
cout << "Enter total marks out of 500: ";
cin >> total;
perc = float(total) / 500 * 100;
}
// member function definition, outside of the class
void student::putDetails(void)
{
ALGORITHM:
Step 1:start the program
Step 2:Define a class Demo with private data members num1 and num2.
Step3:Inside the class Demo, define a public constructor to initialize num1 and num2.
Step 4:Inside the constructor, print "Inside Constructor" to indicate its execution and
initialize num1 with n1 and num2 with n2.
Step 5:Define a public member function display() to display the values of num1 and num2.
Step 6:Inside the display() function, print the values of num1 and num2.
Step 7:Define a destructor ~Demo() to perform cleanup tasks when an object of the class
Demo goes out of scope.
Step 8:Inside the destructor, print "Inside Destructor" to indicate its execution.
a. Create an object obj1 of class Demo with initial values 10 and 20.
b. Call the display() function to display the values of num1 and num2 for the object
obj1. Step 9:end the program
FLOWCHART:
PROGRAM:
}
OUTPUT:
PROGRAM:
#include <iostream>
using namespace std;
// base class
class
Vehicle
{
public:
Vehicle(
)
{
cout << "This is a Vehicle" << endl;
}
};
// sub class derived from the base classes
// although this class does NOTHING
class Car: public Vehicle
{
public:
Car()
{
cout << "This is a Car" << endl;
}
};
// main function
int main()
{
PROGRAM:
#include
<iostream.h> using
namespace std;
// first base class
class Vehicle
{
public:
Vehicle(
)
{
cout << "This is a Vehicle" << endl;
}
};
// second base class
class FourWheeler
{
public:
FourWheeler()
{
PROGRAM:
#include <iostream>
using namespace std;
// base class
class
Vehicle
{
public:
Vehicle(
)
{
}
OUTPUT:
PROGRAM:
#include <iostream>
using namespace std;
// base class
class
Vehicle
{
public:
Vehicle(
)
{
cout << "This is a Vehicle" << endl;
}
};
// first sub class
class Car: public Vehicle
{
public:
Car()
{
return 0;
}
OUTPUT:
PROGRAM:
#include
<iostream.h> using
namespace std;
// base class
class
Vehicle
{
public:
vehicle()
{
public:
Car()
{
START
CreateobjectofAddition
ObjectoftheAdditionclass
Callsum(intnum1,intnu
m2)
Intnum1+intnum2
Displaytheresult
Callsum(intnum1,int
num2,int num3)
Intnum1+intnum2+int
num3
Displaytheresult
STOP
PROGRAM:
#include
<iostream.h> using
namespace std; class
Addition
{
public:
int sum(int num1, int num2)
{ return num1+num2;
}
PROGRAM:
#include
<iostream.h> using
namespace std; class
Test
{
private:
int
num;
public:
Test(): num(8){}
void operator ++()
{ num = num+2;
}
void Print() {
cout<<"The Count is:"<<num;
}
};
int main()
{
Test tt;
++tt; // calling of a function "void operator ++()"
tt.Print();
return 0;
}
OUTPUT:
PROGRAM:
#include
<iostream.h> using
namespace std;
// An abstract class with constructor
class Base
{
protected:
int x;
public:
virtual void fun() = 0;
Base(int i) {
x = i;
cout<<"Constructor of base called\n";
}
};
class Derived: public Base
{
int
y;
public:
Derived(int i, int j):Base(i) { y = j; }
void fun() { cout << "x = " << x << ", y = " <<y<<'\n';
}
};
int main(void)
{
Derived d(4, 5);
d.fun();
//object creation using pointer of base class
Base *ptr=new Derived(6,7);
ptr-
>fun();
return 0;
}
OUTPUT: