Sunday, 12 April 2020

Program : use of file in C++

Write a program to implement I/O operations on characters. I/O operations includes inputing a string, Calculating length of the string, Storing the String in a file, fetching the stored characters from it, etc.



#include<iostream.h>
#include<fstream.h>
#include<string.h>
int main()
{ 
char string[80]; 
cout<<"Enter a String \n"; 
cin>>string; 
int len = strlen(string);
fstream file; 
file.open("TEXT", ios::in | ios::out);
for(int i=0;i<len;i++) 
file.put(string[i]);
file.seekg(0);
char ch; 
while(file) 
{ 
file.get(ch); 
cout<<ch; 
} 
return 0;
}

Program : concatenated the product and sum of series

Write a program to input a set of any 10 integer numbers. Find the sum and product of the numbers. Join the sum and product to form a single number. Display the concatenated number. [Hint: let sum=245 and product = 1346 then the number after joining sum and product will be 2451346]


import java.util.Scanner;
public class ATS
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.println("Enter 10 integers");
long sum = 0, prod = 1;
for (int i = 0; i < 10; i++)
{
int n = in.nextInt();           
sum += n;           
prod *= n;       
}
String s = Long.toString(sum) + Long.toString(prod);
System.out.println("Concatenated Number"+"\t"+s);
    }
}

Saturday, 11 April 2020

Program : find greatest among three three numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter value of a, b & c: ”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
printf(“a is greatest”);
if((b>c)&&(b>a))
printf(“b is greatest”);
if((c>a)&&(c>b))
printf(“c is greatest”);
getch();
}

Program : Copy all the element of 2 arrays into third array

import java.util.Scanner;
public class ATS
{
public  static void main(String[]args)
{
Scanner in = new Scanner(System.in);
int P[] = new int[6];
int Q[] = new int[4];
int R[] = new int[10];
int i = 0;
System.out.println("Enter 6 elements of array P:");
for (i = 0; i < P.length; i++)
{
P[i] = in.nextInt();
        }
System.out.println("Enter 4 elements of array Q:");
for (i = 0; i < Q.length; i++)
{
Q[i] = in.nextInt();       
}
        i = 0;
while(i < P.length)
{
R[i] = P[i];           
i++;       
}
int j = 0;
while(j < Q.length)
{
R[i++] = Q[j++];       
}
System.out.println("Elements of Array R:");
for (i = 0; i < R.length; i++)
{
System.out.print(R[i] + " ");       
}   
}
}

Program : display matrix using class in c++

Write a Program to design a class to represent a matrix. The class should have the functionality to insert and retrieve the elements of the matrix.



#include<iostream.h>
class matrix
{ 
int **p; 
int d1,d2;
public: 
matrix(int x, int y); 
void get_element(int i, int j, int value) 
{ 
p[i][j]=value; 
} 
int & put_element(int i, int j) 
{ 
return p[i][j]; 
}
};
matrix ::matrix(int x, int y)
{ 
d1 = x;  d2 = y; 
p = new int *[d1]; 
for(int i = 0; i < d1; i++) 
p[i] = new int[d2];
}
int main()
{ 
int m, n;
cout<<"Enter size of matrix"; 
cin>>m>>n; 
matrix A(m,n); 
cout<<"Enter Matrix Element row by row:"; 
int i,j,value;
for(i=0;i<m;i++) 
for(j=0;j<n;j++) 
{   
cin>>value;   
A.get_element(i,j,value); 
} 
cout<<"\n";
for(i=0;i<m;i++)
{ 
for(j=0;j<n;j++) 
cout<<A.put_element(i,j);
cout<<"\n";
} 
return 0;
}

Friday, 10 April 2020

WAP in Java to accept an integer number & display its corresponding letter

WAP in Java to accept an integer number N such that 0<N<27 Display the corresponding letter of the alphabet 


import java.util.Scanner;
public class ATS{
public static void main(String[]args)
{
    Scanner in = new Scanner(System.in);
    System.out.print("Enter integer: ");
    int n = in.nextInt();
    if (n > 0 && n < 27)
    {
        char ch =(char)(n + 64);
        System.out.println("Corresponding letter = " + ch);       
    }
    else
    {
        System.out.println("Please enter a number in 1 to 26 range");       
    }

}
}

Program: Write a Program using copy constructor to copy data of an object to another object.

#include<iostream.h>
#include<conio.h>
class code
{ 
int id;
public: 
code(){} 
code(int a) 
{ 
id = a; 
} 
code(code & x) 
{ 
id = x.id; 
} 
void display(void) 
{ 
cout<<id; 
}
};
int main()
{     
code A(100); 
code B(A); 
code C = A; 
code D; 
D = A; 
cout<<"\n id of A:"; 
A.display(); 
cout<<"\n id of B:"; 
B.display(); 
cout<<"\n id of C:"; 
C.display(); 
cout<<"\n id of D:";
D.display(); 
return 0;
}