2 ND
2 ND
Aim: to print second biggest among four given numbers using C program.
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d;
clrscr();
printf(" OUTPUT :\n");
printf("Enter any 4 no.s:");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b&&a>c&&a>d)
if(b>c)
printf("%d is the second largest no.",b);
else
printf("%d is the second largest no.",c);
if(b>a&&b>c&&b>d)
if(a>c)
printf("the second largest no. is % d",a);
else
printf(" the second largest no. is %d",c);
if(c>a&&c>b&&c>d)
if(b>a)
printf("second largest no. is %d",b);
else
printf("second largest no. is %d",a);
if(d>c)
printf("second largest no. is %d",d);
else
printf("second largest no. is %d",c);
getch();
}
Output:
Enter any 4 no.s: 15
20
24
26
2.Write a C program to print mth to nth multiplication tables from 1 to 10 times to each table
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer to find multiplication table: ");
scanf("%d",&n);
for(i=1;i<=10;++i)
{
printf("%d * %d = %d\n", n, i, n*i);
}
return 0;
}
Output:
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
Source code:
#include<stdio.h>
#include<conio.h>
void main()
int n;
Clrscr();
scanf(“%d”,&n);
if(n<0)
printf(“invalid number”);
else
printf(“%d!=%d”,n,fact(n));
getch();
}
int fact(int x)
if(x==0)
return 1;
else
return(x*fact(x-1));
Out put:
enter a number :5
5!= 120
4./*Write a C program to read two strings and print concatenated string without using standard
functions*/.
Aim: to read two strings and print concatenated string without using standard functions using
with C program.
Source code:
#include<stdio.h>
#include<conio.h>
void main()
Clrscr();
gets(string 2);
strcpy(temp,string1)
strcpy(string 1,temp)
Out put:
5.Circumference of a circle
5.Write a function in C to calculate circumference of a circle,which takes radius of the circle as a
parameter. Read radius in print circumference in the main function.
Source code:
#include<stdio.h>
#include<conio.h>
void main()
Int r;
Float pi=3.14,area,ci;
Clrscr();
Scanf(“%d”,&r);
area=pi*r*r;
printf(“area of circle=%f”,area);
ci=2*pi*r;
printf(“circumference=%f”,ci);
getch();
Out put:
Area of circle=78.000
Circumference=31.4
6. Write a C program for the multiplication of two rectangular matrices if possible, otherwise
display appropriate message.
Aim program for the multiplication of two rectangular matrices if possible, otherwise display
appropriate message using C.
Source code:
#include<stdio.h>
#include<conio.h>
void main()
Clrscr();
scanf(“%d”,&row1);
scanf(“%d”,&col 1);
scanf(“%d”,&row2);
scanf(“%d”,&col 2);
if(col 1==row2);
for(i=0,rows;i<row1;i++,row++);
for(j=o,col=1,j<col 1;j++,col+=8)
goto xy(col,row);
scanf(“%d”,&matrix[i][j]);
goto xy(1,row)
for(i=0,row++;i<row2;i++,row++)
for(j=0,col=1;j<col2;j++,col+=8)
goto xy(col,row);
scanf(“%d”,&matrix2[i][j]);
for(i=0;i<row1;i++)
for(j=0;j<col2;j++)
Matrix3[i][j]=0;
for(k=0;k<col 1;k++)
matrix3[i][j]+=matrix1[i][k]*matrix2[k][j];
goto xy(1row);
printf(“%d”,matrix 3[i][j]);
else
Out put:
3 4
2 -1
2 8
5 4
26 40
-1 12
7.Student information
Aim: program to process student information. Student structure consists of Sno, Sname,
marks in 6 subjects, total, average. calculate total and average of n students and assign
grade.
Source code:
#include<stdio.h>
#include<conio.h>
void main()
char name[26]
float avg;
int i,j,num;
clrscr();
scanf(“%d”,&num);
for(i=0;i<num;i++)
scanf(“%d”,&s[i],si.no);
flush(stdin);
gets(s[i].name);
s[i].tot=0;
for(j=0;j<6;j++);
printf(“marks in %d subject:”j+1);
s[i]avg=s[i]total/6.0;
getch();
for(i=0;i<num;i++);
printf(“\n grade:E”);
else if (s[i]avg>=75)
else if(s[i]avg>=60);
else if(s[i]avg>=50)
else
print f(“\ngrade :D”);
getch ( );
Out put:
Marks in 3subject:98
Marks in 4subject:96
Marks in 5subject:76
Marks in 6subject:98
Averge is:87.833336
Grade:A
Marks in 3subject:97
Marks in4subect:76
Marks in5subect:85
Marks in 6subject:85
Total marks:519
Avg is :86.500000
Grade:A
int main()
{
double asset_cost, salvage_val, deprec;
double percentage, acc_deprec;
const int YEARS = 10;
int main()
{
double asset_cost, salvage_val, deprec;
double percentage, acc_deprec;
const int YEARS = 10;
acc_deprec = 0.0;
int main(){
double asset_cost, salvage_val, deprec;
float percentage, rate, nextyear;
int year = 10, i; //years
9.Inline functions
9.Write a C++ program to explain the concept of inline functions.
Aim: to explain the concept of inline functions using C++ program.
Source code:
#include<iostream>
using namespace std;
inline float mul(float x,float y)
{
return(x*Y);
}
inline double div(double p,double q)
{
return(p/q);
int main()
{
float a=12.345;
float b=9.82;
cout<<mul(a,b)<<”\n”;
cout<<div(a,b)<<”\n”;
return 0;
}
Output:
121.228
1.25713
11.Friend function
11. Write a C++ program to explain the concept of friend function using two different
classes.
Aim: to explain the concept of friend function using two different classes using with C+
+ program.
Source code:
#include<iostream>
using namespace std;
class ABC;
class XYZ;
{
int data;
Public:
void setvalue(int value)
{
data=value;
}
friend void add(XYZ,ABC);
};
class ABC
{
int data;
Public:
void setvalue(set value)
{
data=value;
}
friend void add(XYZ,ABC);
};
void add(XYZ obj1,ABC obj2)
{
cout<<”sum of data values of XYZ and ABC objects using friend
function=”<<obj1.data+obj2.data;
}
int main()
{
XYZ X;
ABC A;
X.setvalue(5);
A.setvalue(50);
Add(X,A);
Return 0;
}
Output:
Sum of data values of XYZ and ABC objects using friend function=55
12.Multipath Inheritance
12. Write a C++ program to explain the concept of Multipath Inheritance using virtual
base Classes.
Aim: to explain the concept of Multipath Inheritance using virtual base Classes by using
C++.
Source code:
#include<iostream>
using namespace std;
class m
{
Protected;
int m;
Public;
void get_m(int);
};
class N
{
Protected;
int n;
Public;
void get_n(int);
};
class P:public M,public N
{
Public;
void display(void);
}
void M :: get_m(int x)
{
m=x;
}
void N ::get_n(int y)
n=y;
}
void P ::disply(void)
{
cout<<”m=”<<m<<”\n”;
cout<<”n=”<<n<<”\n”;
cout<<m*n=”<<m*n<<”\n”;
}
int main()
{
P p;
p.get_m(10);
p.get_n(20);
p.display();
return 0;
}
Output:
m=10
n=10
m*n=200
13.Hybrid inheritance
13. Write a C++ program to explain the concept of Hybrid inheritance.
Aim: to explain the concept of Hybrid inheritance using C++.
Source code:
#include<iostream>
using namespace std;
class student
{
Protected;
int roll_number;
Public:
void get_number(int a)
{
roll_number=a;
}
void put_number(void)
{
cout<<”roll no:”<<roll number<<”\n”;
}
};
class test : public student
{
Protected:
float part1,part2;
Public:
void get_marks(float x,float y)
{
part1=x;part2=y;
}
void put_marks(void)
{
cout<<”marks obtained:”<<”\n”
<<”part1=”<<part1<<”\n”
<<”part2=”<<part2<<”\n”;
}
};
class sports
{
Protected:
float score;
Public:
void get_score(float s)
{
score=s;
}
void put_score(void)
{
cout<<”sports wt:”<<score<<”\n\n”;
}
};
class result:public test ,public sports
{
float total;
Public:void display(void);
};
void result :: display(void)
{
total =part1+part2+score;
put_number();
put_marks();
put_score();
cout<<”total score:”<<total<<”\n”;
}
int main()
{
result student_1;
student_1.get_marks(1234);
student_1.get_marks(27.5,33.0);
student_1.get_score(6.0);
student_1.display();
return 0;
}
Output:
roll no:1234
marks obtained:
Part1=27.5
Part2=33
Sports wt:6
Total score:66.5
14.Template functions
14. Write a C++ program to explain the concept of template functions.
Aim: to explain the concept of template functions using C++.
Source code:
#include<iostream>
using namespace std;
template<class T>
void swap(T&x,T&y)
{
T temp=x;
x=y;
y=temp;
void fun(int m,int n,float a,float b)
{
cout<<”m and n before swap:”<<m< “ ”<<n<< “\n”;
swap(m,n);
cout<<”m and n after swap:”<<m<< “ ”<<n<< “\n”;
cout<<”a and b before swap:”<<a<< “ ”<<b<< “\n”;
swap(a,b);
cout<<”a and b after swap:”<<a<< “ ”<<b<< “\n”;
}
int main()
{
fun(100,200,11,22,33,44);
return 0;
}
Output:
m and n before swap: 100 200
m and n after swap: 200 100
a and b before swap: 11.22.33.439999
m and n after swap: 33.439999 11.22
15.Employee details
15. Write a C++ program to store employee details in a data file and calculate tax (10%
of Basic ).Write a separate function
Aim:
Source code:
#include <iostream.h>
#include <conio.h>
class employee
{
char name[10];
int no;
float basic;
float da;
float it;
float ns;
float gs;
public:
void input()
{
cout <<"Enter number:";
cin >> no;
cout <<"Enter name:";
cin >> name;
cout <<"Enter salary:";
cin >> basic;
}
void calculate()
{
da = 0.10 * basic;
gs = da + basic;
it = 0.3 * gs;
ns = gs - it;
}
void output()
footer
{
cout<<no<<'\t'<<name<<'\t'<<basic<<'\t'<<ns<<'\t'<<gs <<'\n';
}
};
void main()
{
employee emp[20];
int n,i;
clrscr();
cout << "Enter no of employees:";
cin >> n;
for(i=0;i<n;i++)
{
emp[i].input();
emp[i].calculate();
}
cout<<"NUMBER"<<'\t'<<"NAME"<<'\t'<<"BASIC"<<'\t'<<"NET"<<
'\t'<<"GROSS" << "\n";
for(i=0;i<n;i++)
{
emp[i].output();
}
getch();
}
Output:
No 014
Name john
Basic 1000
Ns 77
Gs 110
Enter no of employees:20
NUMBER 020
NAME Paul
BASIC 1000
NET 77
GROSS 110