Вы находитесь на странице: 1из 30

16.

/*Write a function in C which returns the sum of digits of a


number*/

#include<iostream.h>
#include<conio.h>
void main()
{
int sum(int n);
int a,r;
clrscr();
cout<<"Enter number= " ;
cin>>a;
r=sum(a);
cout<<r;
getch();
}
int sum(int n)
{
int i,d,s=0;
for(i=1;i<=4;i++)
{
d=n%10;
s=s+d;
n=n/10;
}
return s;
}

Output:

Enter number= 5643


17. /*WAP in C to add two 3*3 matrices */

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the mat1& mat2: ");
scanf("%d%d",&a[i][j],&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=(a[i][j]+b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",c[i][j]);
}
printf("\n");
}
getch();
}
Output:

Enter the mat1& mat2: 2


3
Enter the mat1& mat2: 4
5
Enter the mat1& mat2: 3
4
Enter the mat1& mat2: 5
4
Enter the mat1& mat2: 3
4
Enter the mat1& mat2: 2
3
Enter the mat1& mat2: 4
3
Enter the mat1& mat2: 4
3
Enter the mat1& mat2: 2
3
597
975
775
18. /*Write a function in C power (a,b), to calculate the value of a
raised to b */

#include<iostream.h>
#include<conio.h>
void main()
{
int power(int a,int b);
int x,y,r;
clrscr();
cout<<"Enter base and power= " ;
cin>>x>>y;
r=power(x,y);
cout<<r;
getch();
}
int power(int a, int b)
{
int i;int m=1;
for(i=1;i<=a;i++)
{
m=m*b;
}
return m;
}

Output:

Enter base and power= 4


5
625
19. /*WAP to find the length of a string without using library
function*/
.
#include<stdio.h>
#include<conio.h>
void main()
{
char a[20];
int i, length=0;
clrscr();
gets(a);
for(i=0;a[i]!='\0';i++)
{
length++;
}
printf("%d",length);
getch();
}

Output:

Computer table
14
21. /*Write an OOP Program in C++ to find the factorial of a
given number*/

#include<iostream.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
clrscr();
cout<<"Enter the no= ";
cin>>n;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"Factorial of given no= "<<fact;
getch();
}

Output:

Enter the no= 5


Factorial of given no= 120
22. /*Write an OOP in C++ to read any five numbers and print the
average*/

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c,d,e;
float sum,avg;
clrscr();
cout<<"Enter five no= ";
cin>>a>>b>>c>>d>>e;
{
sum=a+b+c+d+e;
avg=sum/5;
}
cout<<"Avg of no= "<<avg;
getch();
}

Output:

Enter five no= 3


5
2
4
6
Avg of no= 4
24. /*Write an OOP in C++ to print 10 members of Fibonacci
series using a copy constructor*/

#include<iostream.h>
#include<conio.h>
class fibo
{
private:
long int f0,f1,fib;
public:
fibo()
{
f0=0;
f1=1;
fib=f0+f1;
}
fibo (fibo &ptr)
{
f0=ptr.f0;
f1=ptr.f1;
fib=ptr.fib;
}
void increment()
{
f0=f1;
f1=fib;
fib=f0+f1;
}
void display()
{
cout<<fib<<"\t";
}
};
void main()
{
int i;
fibo f;
clrscr();
for(i=1;i<=10;i++)
{
f.display();
f.increment();
}
}

Output:

1 2 3 5 8 13 21 34 55 89
27. /*Create a structure to specify data of customers in a bank.
The data to be stored is: Account number, Name and Balance in
account*/
#include<stdio.h>
#include<conio.h>
struct bank
{
long int account_no;
char name[20];
long int balance;
};
void main()
{
struct bank b1;
printf("Enter the account no.:");
scanf("%ld",&b1.account_no);
printf("Enter the name:");
scanf("%s",&b1.name);
printf("Enter balance:");
scanf("%ld",&b1.balance);
printf("%ld %s %ld",b1.account_no, b1.name, b1.balance);
getch();
}

Output:

Enter the account no.:192


Enter the name:amit
Enter balance:450
192 amit 450
28. /*WAP to perform simple arithmetic operations of two
rational numbers using operator overloading*/

#include<iostream.h>
#include<conio.h>
class rational
{
int a, b;
public:
void input()
{
cout<<"Enter a & b= ";
cin>>a>>b;
}
void output()
{
cout<<a<<"/";
cout<<b;
}
rational operator+(rational x);
};
rational rational::operator+(rational x)
{
rational r;
r.a=(a*x.b)+(b*x.a);
r.b=b*x.b;
return r;
}
void main()
{
rational m,n,s;
clrscr();
m.input();
n.input();
s=m+n;
m.output();
cout<<"+";
n.output();
cout<<"=";
s.output();
getch();
}

Output:

Enter a & b= 2
3
Enter a & b= 4
5
2/3+4/5=22/15
31. /*WAP to copy the contents of a file without white spaces on
another file*/
#include<fstream.h>
#include<conio.h>
#include<ctype.h>
void main() {
ifstream in;
ofstream out;
char c,ch;
clrscr();
out.open("aa.txt");
out<<"Welcome to c++";
out.close();
out.open("bb.txt");
in.open("aa.txt");
while(!in.eof()) {
c=in.get();
if(c==' '|| c=='\t'|| c=='\n');
else
out.put(c);
}
in.close();
out.close();
in.open("bb.txt");
while(!in.eof()) {
c=in.get();
cout.put(c);
}
in.close();
}
Output:
Welcometoc++  
18. /WAP which uses a base class reference to access virtual
function*/

#include<iostream.h>
#include<conio.h>
class fab
{
protected:
int a,b;
public:
void input();
virtual void output();
};
class fab1:public fab
{
int c,i;
public:
void output();
};
void fab::input()
{
cout<<"Enter a & b= ";
cin>>a>>b;
}
void fab ::output()
{
cout<<a<<b;
}
void fab1::output()
{
for(i=1;i<=10;i++)
{
c=a+b;
cout<<c<<"\n";
a=b;
b=c;
}
}
void main()
{
fab *b;
fab1 d;
b=&d;
clrscr();
b->input();
b->output();
getch();
}

Output:

Enter a & b= 0
1
1
2
3
5
8
13
21
34
55
89
1. /* Program to swap two variables without using third
variables.*/

#include<stdio.h>
#include<conio.h>
Void main()
{
int a,b;
a=5;
b=10;
clrscr();
a=a+b;
b=a-b;
a=a-b;
printf(“a=%d”,a);
printf(“b=%d”,b);
getch();
}

Output:-

a=15b=20
22. /*Write an OOP in C++ to read any five numbers and print the average*/
#include<iostream.h>
#include<conio.h>
class avg
{
private:
int a,b,c,d,e;
public:
void input();
void output();
};
void avg::input()
{
cout<<”Enter five no = “;
cin>>a>>b>>c>>d>>e;
}
void avg::output()
{
float sum,avg;
sum=a+b+c+d+e;
avg=sum/5;
Cout<<”avg=”<<avg;
}
void main()
{
avg a;
clrscr();
a.input();
a.output();
getch();
}
Output:-
Enter five no:4
5
6
7
3
avg=5
7. /*WAP in C++ to find the roots of a quadratic equation*/
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,x,x1,x2;
clrscr();
cout<<"enter no:";
cin>>a>>b>>c;
d=(b*b)-(4*a*c);
cout<<d<<endl;
if(d>0)
{
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
cout<<"roots are not equal",x1,x2;
}
if(d<0)
{
cout<<"roots are imaginary";
}
if(d==0)
{
x1=-b/(2*a);
x2=-b/(2*a);
cout<<"roots are equal",x1,x2;
}
getch();
}

Output:-
enter no:3
5
2
1
roots are not equal
25 /*WAP to demonstrate how the this pointer is used to access
the member data of a class.*/

#include<iostream.h>
#include<conio.h>
class sample
{
private:
int n;
Public:
inline void output();
};
inline void sample::output()
{
this->value=20;
cout<<”contents of the value=”<<this->value;
}
void main()
{
sample obj1;
obj1.output();
}

Output:-

contents of the value=20


29 /*WAP to overload assignment operator*/

#include<iostream.h>
#include<conio.h>
class sample
{
private:
int x;
float y;
public:
sample(int,float);
void operator=(sample abc);
void display();
};
sample::sample(int one,float two)
{
x=one;
y=two;
}
void sample::operator=(sample abc)
{
x=abc.x;
y=abc.y;
}
void sample::display()
{
cout<<”integer number(x)= ”<<x<<endl;
cout<<”floating value(y)= “<<y<<endl;
cout<<endl;
}
void main()
{
sample obj1(10,-22.55);
sample obj2(20,-33.44);
obj1.operator=(obj2);
cout<<”contents of the first object\n”;
obj.display();
cout<<”contents of the second object\n”
obj2.display();
}

Output:-

contents of the first object


integer number(x)= 10
floating value(y)= -22.549999

contents of the second object


integer number(x)= 10
floating value(y)= -22.549999
20 /*WAP to find out whether a string entered by the user is
palindrome or not*/

#include<stdio.h>
#include<conio.h>
void main()
{
char a[20];
int i,j,length=0,c=0;
clrscr();
puts("Entert any string :");
gets(a);
for(i=0;a[i]!='\0';i++)
{
length++;
}
j=length-1;
for(i=0;i<(j/2);i++)
{
if(a[i]!=a[j])
c=1;
j--;
}
if(c==0)
puts("It is palindrome");
else
puts("It is not Palindrome");
getch();
}
Output:-
Entert any string :
nitin
It is palindrome
36. /*WAP in C to write records to a file using structure*/

#include<fstream.h>
#include<conio.h>
struct sal
{
char name[20];
int age;
int salary;
};
void main()
{
struct sal s[2];
clrscr();
int i;
for(i=0;i<2;i++)
{
cout<<"Enter name : ";
cin>>s[i].name;
cout<<"Enter Age : ";
cin>>s[i].age;
cout<<"enter salary: ";
cin>>s[i].salary;
}
fstream a;
a.open("sal.txt",ios::in||ios::out);
a.open("sal.txt",ios::out);
for(i=0;i<3;i++)
{
a<<s[i].name<<"\t"<<s[i].age<<"\t"<<s[i].salary<<"\n";
}
a.close();
}

Output:-

Enter name : amit


Enter Age : 23
enter salary: 22450
Enter name : sandeep
Enter Age : 21
enter salary: 17000
26 /*WA OOP in c++ using multiple inheritance to display the
marksheet of student with the following items to read:
name, roll_no,s_name.s_code,i_marks,e_marks.*/

#include<iostream.h>
#include<conio.h>
class stud1
{
protected:
char name[20];
int roll_no;
public:
void input();
void output();
};
void stud1::input()
{
cout<<"enter name and roll no\n";
cin>>name>>roll_no;
}
void stud1::output()
{
cout<<name<<endl<<roll_no<<endl;
}
class stud2
{
protected:
char s_name[5];
int s_code;
public:
void in();
void out();
};
void stud2::in()
{
cout<<"enter s_name and s_code\n";
cin>>s_name>>s_code;
}
void stud2::out()
{
cout<<s_name<<endl<<s_code<<endl;
}
class stud3:public stud1,public stud2
{
private:
int e_marks;
int i_marks;
public:
void i();
void o();
};
void stud3::i()
{
cout<<"enter e_marks and i_marks\n";
cin>>e_marks>>i_marks;
}
void stud3::o()
{
cout<<e_marks<<endl<<i_marks;
}
void main()
{
stud3 s;
clrscr();
s.input();
s.output();
s.in();
s.out();
s.i();
s.o();
getch();
}

Output:-

enter name and roll no


vari
07
vari
7
enter s_name and s_code
com
101
com
101
enter e_marks and i_marks
89
89
89
89
38 /*Program in c++ to implement a stack*/

#include<iostream.h>
#include<conio.h>
#define MAX 11
class stack
{
private:
int arr[MAX];
int top;
public:
stack()
{
top=-1;
}
void push(int item)
{
if(top==MAX-1)
{
cout<<endl<<"stack is full";
return ;
}
top++;
arr[top]=item;
}
int pop()
{
if(top==-1)
{
cout<<endl<<"stack is empty";
return NULL;
}
int data=arr[top];
top--;
return data;
}
};
void main()
{
stack s;
clrscr();
s.push(11);
s.push(12);
s.push(13);
s.push(14);
s.push(15);
s.push(16);
s.push(17);
s.push(18);
s.push(19);
s.push(20);
s.push(21);

int i=s.pop();
cout<<endl<<"item is poped "<<i ;
i=s.pop();
cout<<endl<<"item is poped " <<i;
i=s.pop();
cout<<endl<<"item is poped " <<i;
i=s.pop();
cout<<endl<<"item is poped " <<i;
getch();
}

Output:-

stack is full
item is poped 20
item is poped 19
item is poped 18
item is poped 17

Вам также может понравиться