0% found this document useful (0 votes)
8 views28 pages

2 ND

Uploaded by

SRDC CDE
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)
8 views28 pages

2 ND

Uploaded by

SRDC CDE
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/ 28

2015

PROGRAMING IN ‘C’ (IInd B.COM)

1. Write a C program to print second biggest among four given numbers.


2. Write a C program to print mth to nth multiplication tables from 1 to 10 times to each table
by reading m and n values ,where m<=n.
3. Write a C program to find the factorial of a number using recursion.
4. Write a C program to read two strings and print concatenated string without using
standard functions.
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.
6. Write a C program for the multiplication of two rectangular matrices if possible, otherwise
display appropriate message.
7. Write a C 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 with following criteria.
Grade A : all pass and avg >=75
Grade B : all pass and avg >=60 and avg <75
Grade C : all pass and avg >=50 and avg <60
Grade D : all pass and avg >=40 and avg<50
Grade E : if fails in one or more subjects.
1.Print Second Biggest Number

1. /*Write a C program to print second biggest among four given numbers.*/

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

Second largest no.is: 24

2.Print mth to nth multiplication table

2.Write a C program to print mth to nth multiplication tables from 1 to 10 times to each table

by reading m and n values ,where m<=n.

#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:

Enter an integer to find multiplication table: 9


9 * 1 = 9

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

3.Factorial of a number using recursion

3. /*Write a C program to find the factorial of a number using recursion.*/

Aim: to find the factorial of a number using recursion by using C programming.

Source code:

#include<stdio.h>

#include<conio.h>

void main()

int n;

Clrscr();

print f(“enter a number:”);

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.Print the concatenated string without standard functions

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()

char string 1[51],string 2[26],temp[26];

int counter 1,counter 2;

Clrscr();

printf(“enter first string[max:25]:”);


gets(string 1);

printf(“enter second string[max:25]:”);

gets(string 2);

strcpy(temp,string1)

strcat(string 1,strng 2);

printf(“concatenated string [using string function]:%s”,string 1);

strcpy(string 1,temp)

for(counter 1=0;string 1[counter 1]:= “10”;counter1++);

for(counter 2=0;string 2[counter 2]:= “10”;counter1++,counter 2++);

string 1[counter]=string 2[counter 2];

string 1[counter]= “10”;

printf(“concatenated string[without using string function]:%s”, string 1);

Out put:

enter first string[max:25]: physics

enter second string[max:25]: chemistry

concatenated string [using string function]: physics.chemistry

concatenated string[without using string function]: physics.chemistry

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.

Aim: to calculate circumference of a circle,which takes radius of the circle as a parameter.


Read radius in print circumference in the main function using with C program.

Source code:

#include<stdio.h>
#include<conio.h>

void main()

Int r;

Float pi=3.14,area,ci;

Clrscr();

Printf(“enter radius of circle:”);

Scanf(“%d”,&r);

area=pi*r*r;

printf(“area of circle=%f”,area);

ci=2*pi*r;

printf(“circumference=%f”,ci);

getch();

Out put:

Enter radius of circle:5

Area of circle=78.000

Circumference=31.4

6.the multiplication of two rectangular matrices

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()

int matrix 1[10][10],matrix 2[10][10],row1,col1,row2,col2,row,col,i,j,k,matrix3[10][10];

Clrscr();

printf(“enter order of first matrix:”\n);

printf(“enter row size[max:10]:”);

scanf(“%d”,&row1);

printf(“enter column size[max:10]:”);

scanf(“%d”,&col 1);

printf(“enter order of second matrix:”\n);

printf(“enter row size[max:10]:”);

scanf(“%d”,&row2);

printf(“enter column size[max:10]:”);

scanf(“%d”,&col 2);

if(col 1==row2);

printf(“enter elements for first matrix:”);

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)

printf(“enter elements for second matrix:”);

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(“product of two matrices is:”);

for(i=0 row++;i row1;i row)

for(j=0 col 1;j col2;j col)

goto xy(col row)

printf(“%d”,matrix 3[i][j]);

else

printf(“matrix manipulation is not possible for given order:”);


}

Out put:

enter order of first matrix:

enter row size[max:10] : 2

enter column size[max:10]:2

enter order of second matrix:

enter row size[max:10] : 2

enter column size[max:10]:2

enter elements for first matrix:

3 4

2 -1

enter elements for second matrix:

2 8

5 4

product of two matrices is:

26 40

-1 12

7.Student information

7. /*Write a C 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 with following criteria*/.

Grade A : all pass and avg >=75


Grade B : all pass and avg >=60 and avg <75
Grade C : all pass and avg >=50 and avg <60
Grade D : all pass and avg >=40 and avg<50
Grade E : if fails in one or more subjects.

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()

struct Student inf si.no.,marks[6]total;

char name[26]

float avg;

struct student s[100];

int i,j,num;

clrscr();

printf(“how many student information do you want to enter[max:100]:”);

scanf(“%d”,&num);

for(i=0;i<num;i++)

printf(“enter %d student details:”i+1\n);

printf(“enter student number:”);

scanf(“%d”,&s[i],si.no);

printf(“enter student name:”);

flush(stdin);
gets(s[i].name);

s[i].tot=0;

for(j=0;j<6;j++);

printf(“marks in %d subject:”j+1);

scanf(“%d”,&s[i] marks[j]s[i] total+=s[i] marks[j]);

s[i]avg=s[i]total/6.0;

printf(“press any key to continue………..”);

getch();

for(i=0;i<num;i++);

printf(“\n marks in %d subject :%d”j+1s[i]marks[j]);

printf(“\n total marks:%d” s[i],total);

printf(“\n average is :%f”,s[i]avg);

if(s[i.marks[0]<35!! s[i]marks[1]<35!! S[i] marks[2]<35!! S[i] marks s[3]<35!!s[i]


marks[4]<35!!s[i] marks[5]<35)

printf(“\n grade:E”);

else if (s[i]avg>=75)

printf(\n grade :A);

else if(s[i]avg>=60);

printf(“\n grade :B”);

else if(s[i]avg>=50)

print f(“\n grade :C”);

else
print f(“\ngrade :D”);

print f(“\n press any key to continue……………..”);

getch ( );

Out put:

How many student information do you want to enter[max:100]:q

Enter 1student details:

Enter student number :1

Enter student name: Rubina

Marks in 1subject :87

Marks in 2subject :69

Marks in 3subject:98

Marks in 4subject:96

Marks in 5subject:76

Marks in 6subject:98

Total marks :527

Averge is:87.833336

Grade:A

Press any key to continue……………

Enter 2student details:

Enter 2student number:2

Enter student name :Haripriya

Marks in 1subject :89


Marks in 2subject:87

Marks in 3subject:97

Marks in4subect:76

Marks in5subect:85

Marks in 6subject:85

Total marks:519

Avg is :86.500000

Grade:A

Press any key to continue……………


Programming in C++

8.Write a C++ program by using switch statement to calculate depreciation


using
i. Straight line method
ii. Double declining
iii. Sum of years digits method
9.Write a C++ program to explain the concept of inline functions.
10. Write a C++ program to explain the function over loading.
11. Write a C++ program to explain the concept of friend function using two
different classes.
12. Write a C++ program to explain the concept of Multipath Inheritance
using virtual base Classes.
13. Write a C++ program to explain the concept of Hybrid inheritance.
14. Write a C++ program to explain the concept of template functions.
15. Write a C++ program to store employee details in a data file and calculate
tax (10% of Basic ).Write a separate function
i. Straight line method
8.Write a C++ program by using switch statement to calculate depreciation
using
ii. Straight line method
iii. Double declining
iv. Sum of years digits method
Aim: Straight line method by using switch statement to calculate depreciation
Source code:
#include<iostream>
#include<string>
using namespace std;

int main()
{
double asset_cost, salvage_val, deprec;
double percentage, acc_deprec;
const int YEARS = 10;

cout<<"Asset cost: ";


cin>>asset_cost;

cout<<"Salvage value: ";


cin>>salvage_val;

percentage = 100.0 / YEARS;


cout << "straight-line depreciation percentage " <<
percentage << endl;
acc_deprec = 0.0;

for(int y = 1; y <= 10 ; y++)


{
deprec = 2 * percentage / 100.0 *
(asset_cost - acc_deprec);
acc_deprec += deprec;
cout<<deprec<<endl;
}
ii.Double declining
#include<iostream>
#include<string>
using namespace std;

int main()
{
double asset_cost, salvage_val, deprec;
double percentage, acc_deprec;
const int YEARS = 10;

cout<<"Asset cost: ";


cin>>asset_cost;

cout<<"Salvage value: ";


cin>>salvage_val;

percentage = 100.0 / YEARS;


cout << "double declining depreciation percentage "
<< percentage << endl;

acc_deprec = 0.0;

for(int y = 1; y <= 10 ; y++)


{
deprec = 2 * percentage / 100.0 *
(asset_cost - acc_deprec);
acc_deprec += deprec;
cout<<deprec<<endl;
}
iii.Sum of years digits method
#include<iostream>
#include<string>

using namespace std;

int main(){
double asset_cost, salvage_val, deprec;
float percentage, rate, nextyear;
int year = 10, i; //years

cout<<"Asset cost: ";


cin>>asset_cost;

cout<<"Salvage value: ";


cin>>salvage_val;
for(i = 1; i<10 ; i++){
deprec = 2 * asset_cost/year;
cout<<deprec<<endl;
}
return 0;
}

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

10.function over loading


10. Write a C++ program to explain the function over loading.
Aim: to explain the concept of function over loading using C++ program.
Source code:
#include<iostream.h>
#include<conio.h>
int sum(int,int);
int sum(int,int,int);
float sum(float,int,float);
double sum(double,float);
double sum(int,float,double);
long double sum(long double,double);
void main()
{
int a=sum(4,6);
int b=sum(2,4,6);
float c=sum(3.5f,7,5.6f);
double d=sum(7,8,1.2f);
double e=sum(1,2.2f,8.6);
long double f=sum(100,80);
clrscr();
cout<<‖sum(int,int) =‖<<a<<endl;
cout<<‖ sum(int,int,int) =‖<<b<<endl;
cout<<‖sum(float,int,float) =‖<<c<<endl;
cout<<‖ sum(double,float) =‖<<d<<endl;
cout<<‖ sum(int,float,double) =‖<<e<<endl;\
cout<<‖sum(long double,double) = ―<<f;
getch();
}
int sum(int x,int y)
{
return(x+y);
}
int sum(int x,int y,int z)
{
return(x+y+z);
}
float sum(float x,int y,float z)
{
return(x+y+z);
}
double sum(double x,float y)
{
return(x+y);
}
double sum(int x,float y,double z)
{
return(x+y);
}
long double sum(long double x,double y)
{
return(x+y);
}
Output:
sum(int,int) =10
sum(int,int,int) =12
sum(float,int,float) =16.1
sum(double,float) =9
sum(int,float,double) =11.8
sum(long,double,double) =180

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:

Enter number: 014

Enter name: john

Enter salary: 1000

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

You might also like