RECORDeditable
RECORDeditable
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
OUTPUT:
PROGRAM 5:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004
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
}
OUTPUT:
PROGRAM 6:
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004
!!!!!
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
#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;
}
int main() {
Duster duster1, duster2; // Declare two Duster objects
return 0;
}
OUTPUT:
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
#include<iostream>
using namespace std;
class A {
int a, b;
public:
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.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";
}
};
void putmarks() {
cout<< "Marks obtained:" << "\n";
cout<< "Part1 = " << part1 << "\n";
Ramakrishna Mission Vivekananda (Evening) College (Autonomous), Chennai – 600004
void putscore() {
cout<< "Sports weight = " << score << "\n\n";
}
};
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
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
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
return 0;
}
OUTPUT: