c
- Write a C program to read an integer array and display the maximum element.
#include<stdio.h>
int main(){
int a[50],size,i,big;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ”, size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
printf(“The elements are”);
for(i=0;i<size;i++)
printf("%d",a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("\nBiggest: %d",big);
return 0;
}
Output:
Enter the size of the array: 5
Enter elements in to the array:3 4 9 6 7
The elements are 3 4 9 6 7
Biggest:9
- Write a C program to reverse the content of an array data structure.
Algorithm:
Start
1)Initialize start and end indexes.
start = 0, end = n-1
2) In a loop, swap arr[start] with arr[end] and change start and end as follows.
start = start +1; end = end – 1
start = start +1; end = end – 1
Stop
Program:
#include<stdio.h>
voidrvereseArray(intarr[], int start, int end)
{
inttemp;
while(start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
voidprintArray(intarr[], int size)
{
inti;
for(i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
intmain()
{
intarr[] = {1, 2, 3, 4, 5, 6};
printArray(arr, 6);
rvereseArray(arr, 0, 5);
printf("Reversed array is \n");
printArray(arr, 6);
getchar();
return0;
}
Output: 6 5 4 3 2 1
- Write a C program to demonstrate the use of structure.
ALGORITHM:
Step1: Start the program
Step2: Declare the Structure
Step3: Declare the variable and initialize
Step4: Display
Step5: Stop the program
PROGRAM:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
int main()
{
struct student record = {0}; //Initializing to null
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
printf(" Id is: %d \n", record.id);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}
Output:
Id is:1
Name is: Raju
Percentage: 86.5
- Write a program in C to demonstrate the difference between call-by-value and call-by-reference.
Call by value
#include
void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
}
void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
}
Output:
enter value for a&b: 12 55
after swapping the value for a & b is :55 12
Call by reference
#include <stdio.h>
void swap(int*, int*);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *b;
*b = *a;
*a = temp;
}
Output:
enter value for a&b: 4 8
after swapping the value for a & b is :8
…………………………………………………
C++
- Write a C++ program to illustrate the use of function overloading
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the class name as fn with data members and member functions.
STEP 3: Read the choice from the user.
STEP 4: Choice=1 then go to the step 5.
STEP 5: The function area() to find area of circle with one integer argument.
STEP 6: Choice=2 then go to the step 7.
STEP 7: The function area() to find area of rectangle with two integer argument.
STEP 8: Choice=3 then go to the step 9.
STEP 9: The function area() to find area of triangle with three arguments, two as Integer and one as float.
STEP 10: Choice=4 then stop the program.
PROGRAM:
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int); //circle
void area(int,int); //rectangle
void area(float ,int,int); //triangle
};
void fn::area(int a)
{
cout<<"Area of Circle:"<<pi*a*a;
}
void fn::area(int a,int b)
{
cout<<"Area of rectangle:"<<a*b;
}
void fn::area(float t,int a,int b)
{
cout<<"Area of triangle:"<<t*a*b;
}
void main()
{
int ch;
int a,b,r;
clrscr();
fn obj;
cout<<"\n\t\tFunction Overloading";
cout<<"\n1.Area of Circle\n2.Area of Rectangle\n3.Area of Triangle\n4.Exit\n:”;
cout<<”Enter your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Enter Radious of the Circle:";
cin>>r;
obj.area(r);
break;
case 2:
cout<<"Enter Sides of the Rectangle:";
cin>>a>>b;
obj.area(a,b);
break;
case 3:
cout<<"Enter Sides of the Triangle:";
cin>>a>>b;
obj.area(0.5,a,b);
break;
case 4:
exit(0);
}
getch();
}
Output:
Function Overloading
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 2
Enter the Sides of the Rectangle: 5 5
Area of Rectangle is: 25
1. Area of Circle
2. Area of Rectangle
3. Area of Triangle
4. Exit
Enter Your Choice: 4
- Write a C++ program to illustrate the use of operator Overloading
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the class.
Step 3: Declare the variables and its member function.
Step 4: Using the function getvalue() to get the two numbers.
Step 5: Define the function operator +() to add two complex numbers.
Step 6: Define the function operator –()to subtract two complex numbers.
Step 7: Define the display function.
Step 8: Declare the class objects obj1,obj2 and result.
Step 9: Call the function getvalue using obj1 and obj2
Step 10: Calculate the value for the object result by calling the function operator + and operator -.
Step 11: Call the display function using obj1 and obj2 and result.
Step 12: Return the values.
Step 13: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:
void getvalue()
{
cout<<"Enter the value of Complex Numbers a,b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
cout<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex obj1,obj2,result,result1;
obj1.getvalue();
obj2.getvalue();
result = obj1+obj2;
result1=obj1-obj2;
cout<<"Input Values:\n";
obj1.display();
obj2.display();
cout<<"Result:";
result.display();
result1.display();
getch();
}
Output:
Enter the value of Complex Numbers a, b
4 5
Enter the value of Complex Numbers a, b
2 2
Input Values
4 + 5i
2 + 2i
Result
6 + 7i
2 + 3i
- Write a C++ program to illustrate the use inline function
ALGORITHM:
Step 1: Start the pogram.
Step 2: Declare the class.
Step 3: Declare and define the inline function for multiplication and cube.
Step 4: Declare the class object and variables.
Step 5: Read two values.
Step 6: Call the multiplication and cubic functions using class objects.
Step 7: Return the values.
Step 8: Display.
Step 9: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class line
{
public:
inline float mul(float x,float y)
{
return(x*y);
}
inline float cube(float x)
{
return(x*x*x);
}
};
void main()
{
line obj;
float val1,val2;
clrscr();
cout<<"Enter two values:";
cin>>val1>>val2;
cout<<"\nMultiplication value is:"<<obj.mul(val1,val2);
cout<<"\n\nCube value is :"<<obj.cube(val1)<<"\t"<<obj.cube(val2);
getch();
}
Output:
Enter two values: 5 7
Multiplication Value is: 35
Cube Value is: 25 and 343
8. Write a C++ program to illustrate the use of virtual base class.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare the base class student.
Step 3: Declare and define the functions getnumber() and putnumber().
Step 4: Create the derived class test virtually derived from the base class student.
Step 5: Declare and define the function getmarks() and putmarks().
Step 6: Create the derived class sports virtually derived from the base class student.
Step 7: Declare and define the function getscore() and putscore().
Step 8: Create the derived class result derived from the class test and sports.
Step 9: Declare and define the function display() to calculate the total.
Step 10: Create the derived class object obj.
Step 11: Call the function get number(),getmarks(),getscore() and display().
Step 12: Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class student
{
int rno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"\n\n\tRoll No:"<<rno<<"\n";
}
};
class test:virtual public student
{
public:
int part1,part2;
void getmarks()
{
cout<<"Enter Marks\n";
cout<<"Part1:";
cin>>part1;
cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<"\tMarks Obtained\n";
cout<<"\n\tPart1:"<<part1;
cout<<"\n\tPart2:"<<part2;
}
};
class sports:public virtual student
{
public:
int score;
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore()
{
cout<<"\n\tSports Score is:"<<score;
}
};
class result:public test,public sports
{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"\n\tTotal Score:"<<total;
}
};
void main()
{
result obj;
clrscr();
obj.getnumber();
obj.getmarks();
obj.getscore();
obj.display();
getch();
}
Output:
Enter Roll No: 200
Enter Marks
Part1: 90
Part2: 80
Enter Sports Score: 80
Roll No: 200
Marks Obtained
Part1: 90
Part2: 80
Sports Score is: 80
Total Score is: 250
9. Write a C++ program to illustrate the use of single inheritance (Public/Private)
Algorithm:
Step 1: Start the program.
Step 2: Declare the base class emp.
Step 3: Define and declare the function get() to get the employee details.
Step 4: Declare the derived class salary.
Step 5: Declare and define the function get1() to get the salary details.
Step 6: Define the function calculate() to find the net pay.
Step 7: Define the function display().
Step 8: Create the derived class object.
Step 9: Read the number of employees.
Step 10: Call the function get(),get1() and calculate() to each employees.
Step 11: Call the display().
Step 12: Stop the program
Program:
#include<iostream.h>
#include<conio.h>
class emp
{
public:
int eno;
char name[20],des[20];
void get()
{
cout<<"Enter the employee number:";
cin>>eno;
cout<<"Enter the employee name:";
cin>>name;
cout<<"Enter the designation:";
cin>>des;
}
};
class salary:public emp
{
float bp,hra,da,pf,np;
public:
void get1()
{
cout<<"Enter the basic pay:";
cin>>bp;
cout<<"Enter the Humen Resource Allowance:";
cin>>hra;
cout<<"Enter the Dearness Allowance :";
cin>>da;
cout<<"Enter the Profitablity Fund:";
cin>>pf;
}
void calculate()
{
np=bp+hra+da-pf;
}
void display()
{
cout<<eno<<"\t"<<name<<"\t"<<des<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<np<<"\n";
}
};
void main()
{
int i,n;
char ch;
salary s[10];
clrscr();
cout<<"Enter the number of employee:";
cin>>n;
for(i=0;i<n;i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout<<"\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for(i=0;i<n;i++)
{
s[i].display();
}
getch();
}
Output:
Enter the Number of employee:1
Enter the employee No: 150
Enter the employee Name: ram
Enter the designation: Manager
Enter the basic pay: 5000
Enter the HR allowance: 1000
Enter the Dearness allowance: 500
Enter the profitability Fund: 300
E.No E.name des BP HRA DA PF NP
150 ram Manager 5000 1000 500 300 6200
10. Write a C++ program to illustrate the use of file open() and close() operation.
ALGORITHM:
STEP 1: Start the program.
STEP 2: Declare the variables.
STEP 3: Read the file name.
STEP 4: open the file to write the contents.
STEP 5: writing the file contents up to reach a particular condition.
STEP 6: Stop the program.
PROGRAM:
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<fstream.h>
void main()
{
char c,fname[10];
ofstream out;
cout<<"Enter File name:";
cin>>fname;
out.open(fname);
cout<<"Enter contents to store in file (Enter # at end):\n";
while((c=getchar())!='#')
{
out<<c;
}
out.close();
getch();
}
Output:
Enter File name: one.txt
Enter contents to store in file (enter # at end)
Master of Computer Applications#
11. Write a C++ program to illustrate the use of Class template
#include <iostream>
using namespace std;
template <typename T>
class Number {
private:
T value;
public:
Number(T value) { this->value = value; };
T getValue() { return value; }
void setValue(T value) { this->value = value; };
};
int main() {
Number<int> i(55);
cout << i.getValue() << endl;
Number<double> d(55.66);
cout << d.getValue() << endl;
Number<char> c('a');
cout << c.getValue() << endl;
Number<string> s("Hello");
cout << s.getValue() << endl;
}
12. Write a C++ program to illustrate the use of function template.
#include <iostream>
using namespace std;
template <typename T>
void mySwap(T &a, T &b);
// Swap two variables of generic type passed-by-reference
// There is a version of swap() in <iostream>
int main() {
int i1 = 1, i2 = 2;
mySwap(i1, i2); // Compiler generates mySwap(int &, int &)
cout << "i1 is " << i1 << ", i2 is " << i2 << endl;
char c1 = 'a', c2 = 'b';
mySwap(c1, c2); // Compiler generates mySwap(char &, char &)
cout << "c1 is " << c1 << ", c2 is " << c2 << endl;
double d1 = 1.1, d2 = 2.2;
mySwap(d1, d2); // Compiler generates mySwap(double &, double &)
cout << "d1 is " << d1 << ", d2 is " << d2 << endl;
//mySwap(i1, d1);
// error: no matching function for call to 'mySwap(int&, double&)'
// note: candidate is:
// note: template<class T> void mySwap(T&, T&)
}
template <typename T>
void mySwap(T &a, T &b) {
T temp;
temp = a;
a = b;
b = temp;
}
……………………………………………………………
JAVA
13.Write a Java Program to add two numbers and display the result
Program:
importjava.util.Scanner;
classAddNumbers
{
publicstaticvoidmain(Stringargs[])
{
intx, y, z;
System.out.println("Enter two integers to calculate their sum ");
Scanner in =newScanner(System.in);
x =in.nextInt();
y =in.nextInt();
z =x +y;
System.out.println("Sum of entered integers = "+z);
}
14. Write a Java program to reverse a number.
Program:
import java.util.Scanner;
public classReverseNumberExample {
publicstatic voidmain(Stringargs[]) {
System.out.println("Please enter number to be reversed using Java program: ");
intnumber = newScanner(System.in).nextInt();
intreverse = reverse(number);
System.out.println("Reverse of number: " + number + " is " + reverse(number));
}
publicstatic voidmain(Stringargs[]) {
System.out.println("Please enter number to be reversed using Java program: ");
intnumber = newScanner(System.in).nextInt();
intreverse = reverse(number);
System.out.println("Reverse of number: " + number + " is " + reverse(number));
}
publicstatic intreverse(intnumber){
intreverse = 0;
intremainder = 0;
do{
remainder = number%10;
reverse = reverse*10 + remainder;
number = number/10;
}while(number > 0);
returnreverse;
}
}
Output:
Output:
Please enter number to be reversed using Java program:
1234
Reverse of number: 1234 is 4321
- Write a Java program to find out the factorial of a number.
Program:
importjava.util.Scanner;
classFactorial
{
publicstaticvoidmain(String args[])
{
intn, c, fact = 1;
System.out.println("Enter an integer to calculate it's factorial");
Scanner in = newScanner(System.in);
n = in.nextInt();
if( n < 0 )
System.out.println("Number should be non-negative.");
else
{
for( c = 1 ; c <= n ; c++ )
fact = fact*c;
System.out.println("Factorial of "+n+" is = "+fact);
}
}
}
Output:
Enter an integer to calculate it’s factorial 6
Factorial of 6 is = 720
No comments:
Post a Comment