0% found this document useful (0 votes)
14 views

RECORDeditable

Uploaded by

Finn McMissile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

RECORDeditable

Uploaded by

Finn McMissile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

Ex. No:

DATE:

AIM:

PROCEDURE:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

PROGRAM 1:
To find the sum of given number using
while statement.

#include<stdio.h>
#include<conio.h>
int main()
{
inta,b=0;
printf("\n Enter the number:");
scanf("%d",&a);
while(a!=0)
{
b=b+(a%10);
a=a/10;
}
printf("\n Sum of given number=%d",b);
getch( );
}

OUTPUT:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

PROGRAM 2:
To display the following output
1
2 2
3 3 3

#include"stdio.h"
#include"conio.h"
int main()
{
inti,j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i;j++)
{
printf(" %d",i);
}
printf("\n");
}
getch( );
}
OUTPUT:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

PROGRAM 3:
To count the number of vowels,
consonants, words, white spaces in a line
of text and array of lines.

#include"stdio.h"
#include"conio.h"
#include"string.h"
void main()
{
char str[50],ch;
inti,v=0,c=0,w=1,s=0,len;
clrscr( );
printf("\n Enter the string:");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
ch=str[i];
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
{
v++;
break;
}
case ' ':
{
s++;
break;
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

}
default:
{
c++;
break;
}
}
if(str[i]==' '&&str[i-1]!=' ')
{
w++;
}
}
printf("\n Number of vowels:%d",v);
printf("\n Number of consonants:%d",c);
printf("\n Number of words: %d",w);
printf("\n Number of white spaces: %d",s);
getch( );
}

OUTPUT:

PROGRAM 4:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

To check the reversed string is


palindrome or not.
#include"stdio.h"
#include"conio.h"
#include"string.h"
int main()
{
char a[100],b[100];
printf("\n Enter the string:");
gets(a);
strcpy(b,a);
strrev(b);
printf(" The reversed string is: %s",b);
if(strcmp(a,b)==0)
printf("\n Reversed string is a palindrome");
else
printf("\n Reversed string is not a palindrome");
getch();
}

OUTPUT:

PROGRAM 5:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

To find the matrix addition and


subtraction.
#include <stdio.h>
int main() {
int a[100][100], b[100][100], c[100][100];
inti, j, p, q, m, n, d;

while (1) {
printf("\n1. Addition of matrix");
printf("\n2. Subtraction of matrix");
printf("\n3. Exit");
printf("\nEnter your choice: ");
scanf("%d", &d);

if (d == 3) {
printf("\nExiting the program.");
break;
}
if (d == 1 || d == 2) {
// Taking dimensions of both matrices
printf("\nEnter the rows & columns of the matrices:");
printf("\nFirst matrix (rows columns): ");
scanf("%d %d", &m, &n);
printf("\nSecond matrix (rows columns): ");
scanf("%d %d", &p, &q);
if (m == p && n == q) {
printf("\nBinary operation is possible");
printf("\nEnter the elements of the first matrix:\n");
for (i = 0; i< m; i++) {
for (j = 0; j < n; j++) {
printf("a[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}
printf("\nEnter the elements of the second matrix:\n");
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

for (i = 0; i< p; i++) {


for (j = 0; j < q; j++) {
printf("b[%d][%d] = ", i, j);
scanf("%d", &b[i][j]);
}
}
if (d == 1) {
for (i = 0; i< m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] + b[i][j];
}
}
printf("\nThe addition matrix is:\n");
} else {
for (i = 0; i< m; i++) {
for (j = 0; j < n; j++) {
c[i][j] = a[i][j] - b[i][j];
}
}
printf("\nThe subtraction matrix is:\n");
}
for (i = 0; i< m; i++) {
for (j = 0; j < n; j++) {
printf("%d\t", c[i][j]);
}
printf("\n");
}
}
else {
printf("\nBinary operation is not possible. Thedimensions do not
match.\n");
}
} else {
printf("\nSorry, wrong option. Please try again.\n");
}
}
return 0;
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

}
OUTPUT:

PROGRAM 6:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

POINTERS AND FUNCTION


#include<stdio.h>
void swap(int *a, int *b);
int main() {
int m = 10, n = 20;
printf("Before swapping:\n");
printf("m = %d\n", m);
printf("n = %d\n\n", n);
swap(&m, &n);
printf("After swapping:\n");
printf("m = %d\n", m);
printf("n = %d\n", n);
return 0;
}
void swap(int *a, int *b) {
int temp;
temp = *a;
*a = *b;
*b = temp;
}
OUTPUT:

!!!!!

PROGRAM 7:
NESTED STRUCTURE
#include<stdio.h>
#include<conio.h>
struct address
{
char HouseNo[25];
char city[25];
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

char pincode[25];
};
struct Employee
{
int ID;
char name[25];
int salary;
struct address Add;
};
void main()
{
int i;
struct Employee E;
printf("\n\t Enter Employee ID: ");
scanf("%d",&E.ID);
printf("\n\t Enter Employee Name: ");
scanf("%s",&E.name);
printf("\n\t Enter Employee Salary: ");
scanf("%d",&E.salary);
printf("\n\t Enter Employee House No.: ");
scanf("%s",&E.Add.HouseNo);
printf("\n\t Enter Employee City: ");
scanf("%s",&E.Add.city);
printf("\n\t Enter Employee Pincode: ");
scanf("%s",&E.Add.pincode);
printf("\nDetails of Employee:\n");
printf("\n\t Employee ID: %d\n",E.ID);
printf("\n\t Employee Name: %s\n",E.name);
printf("\n\t Employee Salary: %d\n",E.salary);
printf("\n\t Employee House No: %s\n",E.Add.HouseNo);
printf("\n\t Employee City: %s\n",E.Add.city);
printf("\n\t Employee Pincode: %s\n",E.Add.pincode);
getch();
}

OUTPUT:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

PROGRAM 8:
Error Handling
#include <stdio.h>
#include <errno.h>
#include <string.h>

Int main() {
FILE *pf;
Int errnum;
Pf = fopen(“unexist.txt”, “rb”);
If (pf == NULL) {
Errnum = errno;
Fprintf(stderr, “Value of errno: %d\n”, errnum);
Perror(“Error printed by perror”);
Fprintf(stderr, “Error opening file: %s\n”,
strerror(errnum));
}
Return 0;
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

}
OUTPUT:

Working

#include <stdio.h>
#include <stdlib.h>

int main() {
int dividend = 20;
int divisor = 0;
int quotient;
if (divisor == 0) {
fprintf(stderr, "Division by zero! Exiting...\n");
exit(EXIT_FAILURE);
}
quotient = dividend / divisor;
fprintf(stderr, "Value of quotient: %d\n", quotient);
return EXIT_SUCCESS;
}

OUTPUT:
Division by zero! Exiting…
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

Program to demonstrate oops based


duster information.

#include<iostream>
using namespace std;

class Duster {
int id;
int length;
int breadth;
char color[10];
float price;

public:
// Function to input duster data
void getdata() {
cout<< "\nInput id, length, breadth of duster: ";
cin>> id >> length >> breadth;
cout<< "Input color and price of duster: ";
cin>> color >> price;
}

// Function to display duster data


void showdata() {
cout<< "\nDuster record is:";
cout<< "\nId = " << id;
cout<< "\nLength = " << length;
cout<< "\nBreadth = " << breadth;
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

cout<< "\nColor = " << color;


cout<< "\nPrice = " << price <<endl;
}
};

int main() {
Duster duster1, duster2; // Declare two Duster objects

// Get and display data for duster1


duster1.getdata();
duster1.showdata();

// Get and display data for duster2


duster2.getdata();
duster2.showdata();

return 0;
}

OUTPUT:

STATIC DATA MEMBER


#include <iostream>
using namespace std;

class Item {
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

int no;
static int count;

public:

void get(int x) {
no = x;
count++;
}

void put() {
cout<< "Number = " << no <<endl;
cout<< "Count = " << count <<endl;
}
};

int Item::count = 0;

int main() {
Item a, b, c;

a.get(100);
b.get(200);
c.get(300);

a.put();
b.put();
c.put();

return 0;
}
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

OUTPUT:

FRIEND FUNCTION
#include<iostream.h>
#include<conio.h>
class sports;
class student
{
int m1;
public:
void get(int x)
{
m1=x;
}
friend void max(student s,sports y);
};
class sports
{
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

int m2;
public:
void set(int x)
{
m2=x;
}
friend void max(student s,sports y);
};
void max(student s,sports y)
{
if(s.m1>y.m2)
cout<<"student is max";
else
cout<<"sports is max";
}

main()
{
student s1;
sports b1;
s1.get(10);
b1.set(50);
max(s1,b1);
getch();
}

OUTPUT:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

CONSTRUCTOR OVERLOADING
#include<iostream.h>
#include<conio.h>
class student
{
introllno;
char grade;
public:
student()
{
rollno=1;
grade='a';
}
student(intx,char y)
{
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

rollno=x;
grade=y;
}
student(student &s)
{
rollno=s.rollno;
grade=s.grade;
}
void put()
{
cout<<"rollno="<<rollno<<endl;
cout<<"grade="<<grade<<endl;
}
};

main()
{
student s1;
student s2(10,'c');
student s3(20,'o');
student s4(s2);
s1.put();
s2.put();
s3.put();
s4.put();
getch();
}

OUTPUT:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

FUNCTION OVERLOADING
#include<iostream.h>
#include<conio.h>
int area(int x);
float area(float x,int y);
int area(intx,inty,int z);
main()
{
intx,y,z,a;
float b;
cout<<"enter side value of cube\n";
cin>>x;
area(x);
cout<<"\nenter radius & height of cylinder\n";
cin>>a>>b;
area(b,a);
cout<<"\nenterlength,breadth& height of cuboid\n";
cin>>x>>y>>z;
area(x,y,z);
getch();
}
int area(int x)
{
cout<<x*x*x;
}
float area(float x,int y)
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

{
cout<<3.14*x*x*y;
}
int area(intx,inty,int z)
{
cout<<x*y*z;
}

OUTPUT:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

VIRTUAL FUNCTION
#include<iostream.h>
#include<conio.h>
class base
{
public:
virtual void display()
{
cout<<"display function in base class"<<endl;
}
virtual void show()
{
cout<<"show function in base class"<<endl;
}
};
class derived:public base
{
public:
void display()
{
cout<<"display function in derived class"<<endl;
}
void show()
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

{
cout<<"show fuction in derived class"<<endl;
}
};

main()
{
base b;
derived d;
base*ptr;
ptr=&b;
ptr->display();
ptr->show();
ptr=&d;
ptr->display();
ptr->show();
getch();
}

OUTPUT:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

Operator Overloading Binary


Arithmetic Operator

#include<iostream>
using namespace std;

class A {
int a, b;

public:

void getdata(int p1, int p2) {


a = p1;
b = p2;
}

void showdata() {
cout<< "\na = " << a;
cout<< "\tb = " << b;
}
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

A operator+ (A ob) {
A ot;
ot.a = a + ob.a; // Corrected 'Ot' to 'ot'
ot.b = b + ob.b;
return ot;
}
};

int main() {
A ob1, ob2, ob3;

ob1.getdata(10, 20);
ob2.getdata(1, 2);

ob3 = ob1 + ob2;

ob3.showdata();
return 0;
}

OUTPUT:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

HYBRID INHERITANCE
#include <iostream>
using namespace std;

class student {
protected:
introllno;
public:
void getnum(int a) {
rollno = a;
}

void putnum() {
cout<< "Roll No = " <<rollno<< "\n";
}
};

class test : virtual public student {


protected:
float part1, part2;
public:
void getmarks(float x, float y) {
part1 = x;
part2 = y;
}

void putmarks() {
cout<< "Marks obtained:" << "\n";
cout<< "Part1 = " << part1 << "\n";
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

cout<< "Part2 = " << part2 << "\n";


}
};

class sports : public virtual student {


protected:
float score;
public:
void getscore(float s) {
score = s;
}

void putscore() {
cout<< "Sports weight = " << score << "\n\n";
}
};

class result : public test, public sports {


float total;
public:

void display() {
total = part1 + part2 + score;
putnum();
putmarks();
putscore();
cout<< "Total score = " << total << "\n";
}
};

int main() {
result student1;

student1.getnum(678);
student1.getmarks(30.5, 25.5);
student1.getscore(7.0);
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

student1.display();

return 0;
}

OUTPUT:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

CLASS TEMPLATE WITH


MULTIPLE PARAMETER
#include<iostream>
using namespace std;

template<class t1, class t2>


class test {
t1 a;
t2 b;
public:
void put() {
cout<< a <<endl<< b <<endl;
}
void get(t1 x, t2 y) {
a = x;
b = y;
}
};

int main() {
test<int, float> m;
m.get(10, 30.2);
m.put();

test<char, int> n;
n.get('c', 30);
n.put();
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

return 0;
}

OUTPUT:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

EXCEPTION HANDLING WITH


MULTIPLE CATCH
#include<iostream>
using namespace std;

void test(int x) {
try {
if (x == 1)
throw x;
if (x == 0)
throw 'a';
if (x == -1)
throw 3.2;
cout<< "End of try block" <<endl;
}
catch (int i) {
cout<< "Caught an int" <<endl;
}
catch (char c) {
cout<< "Caught a char" <<endl;
}
catch (double d) {
cout<< "Caught a float" <<endl;
}
cout<< "End of function" <<endl;
}

int main() {
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004

cout<< "x = 1" <<endl;


test(1);

cout<< "x = 0" <<endl;


test(0); // Changed test(2) to test(0)

cout<< "x = -1" <<endl;


test(-1);

cout<< "x = 2" <<endl;


test(2); // This will not throw any exceptions

return 0;
}
OUTPUT:

You might also like