Monday, 20 April 2020

Program print following sequence

ZYXWU
ZYXW
ZYX
ZY
Z


public class ATS
{
public static void main(String[]args)
{
for (int i = 86; i <= 90; i++)
{
for (int j = 90; j >= i; j--)
{
System.out.print((char)j);           
} System.out.println();       
}   
}
}

Write a Program to allocate memory dynamically for an objects of a given class using class’s constructor

#include<iostream.h>
#include<string.h>
#include<conio.h>
class String
{ 
char *name; 
int length;
public: 
String() 
{ 
length = 0; 
name = new char[length +1]; 
} 
String (char *s) 
{ 
length = strlen(s); 
name= new char[length + 1]; 
strcpy(name, s); 
} 
void display(void) 
{ 
cout<<name<<"\n"; 
} 
void join(String &a, String &b);
};
void String :: join (String &a, String &b)
{ 
length = a.length + b.length;  delete name; 
name = new char [length + 1];
strcpy(name,a.name); 
strcat(name, b.name);
};
int main()
{     
char *first = "Joseph"; 
String name1(first), name2("Louis "), name3("Lagrange"),s1,s2; 
s1.join(name1, name2); 
s2.join(s1, name3); 
name1.display(); 
name2.display(); 
name3.display(); 
s1.display(); 
s2.display();   
return 0;
}

HTML Text Styles and List Tags

Friday, 17 April 2020

Program to calculate sum and percentage of 5 subject marks

#include<stdio.h>
#include<conio.h>
void main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;
clrscr();
printf(“enter marks of 5 subjects: ”);
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
printf(“sum=%d”,sum);
per=(sum*100)/total;
printf(“percentage=%f”,per);
getch();
}

Write a program to accept name and total marks of N number of students in two single subscript arrays name[ ] and totalmarks[ ].

Calculate and print: (i) The average of the total marks obtained by N number of students. [average = (sum of total marks of all the students)/N] (ii) Deviation of each student's total marks with the average. [deviation = total marks of a student - average]


import java.util.Scanner;
public class ATS
{
public static void main(String[]args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter number of students: ");
int n = in.nextInt();
String name[] = new String[n];
int totalmarks[] = new int[n];
int grandTotal = 0;
for (int i = 0; i < n; i++)
{           
in.nextLine();
System.out.print("Enter name of student " + (i+1) + ": ");
        name[i] = in.nextLine();
System.out.print("Enter total marks of student " + (i+1) + ": ");           
totalmarks[i] = in.nextInt();           
grandTotal += totalmarks[i];       
}
double avg = grandTotal / (double)n;
System.out.println("Average = " + avg);
for (int i = 0; i < n; i++)
{
System.out.println("Deviation for " + name[i] + " = " + (totalmarks[i] - avg));       
}   
}
}

Write a Program to overload operators

Write a Program to overload operators like *, <<, >> using friend function. The following overloaded operators should work for a class vector. 

#include<iostream.h>
#include<conio.h>
const size = 3;
class vector
{
int v[size];
public:
vector();
vector(int *x);
friend vector operator *(int a, vector b);
friend vector operator *(vector b, int a);  
friend istream & operator >>(istream &, vector &);
friend ostream & operator <<(ostream &, vector &);
};
vector ::vector()
{
for(int i=0;i<size;i++)
v[i]=0;
}
vector :: vector(int *x)
{
for(int i=0; i<size; i++)
v[i] = x[i];
}
vector operator *(int a, vector b)
{
vector c;
for(int i=0; i<size; i++)
c.v[i] = a * b.v[i];
return c;
}
vector operator *(vector b, int a)
{
vector c;
for(int i=0; i<size; i++)
c.v[i] = b.v[i] * a;  return c;
}
istream & operator >> (istream &din, vector &b)
{
for(int i=0; i<size; i++)
din>>b.v[i];
return(din);
}


ostream & operator << (ostream &dout, vector &b)
{
dout<<"("<<b.v [0];
for(int i=1; i<size; i++)
dout<<","<<b.v[i];
dout<<")";
return(dout);
}
int x[size] = {2,4,6};
int main()
{   
vector m;
vector n = x;
cout<<"Enter Elements of vector m"<<"\n";
cin>>m;
cout<<"\n";
cout<<"m="<<m<<"\n";
vector p,q;
p = 2 * m;
q = n * 2;
cout<<"\n";
cout<<"p="<<p<<"\n";
cout<<"q="<<q<<"\n"; 
return 0;
}