Professional Documents
Culture Documents
Day-1
Assignment 1
Q1. Accept the radius from user and compute the area and volume of a circle.
>>Source code
#include<stdio.h>
int main()
{
int radius,area,perimeter;
printf("enter the radius of circle\n");
scanf("%d",&radius);
area = 3.14 * radius*radius;
perimeter = 2* 3.14* radius;
printf("area of circle = %d and perimeter = %d\n",area,perimeter);
return 0;
}
Op –
Q2. Accept a character from user and display ASCII value of it.
>>Source code
#include<stdio.h>
int main()
{
char a;
printf("enter the character\n");
scanf("%c",&a);
printf("ASCII value of given character is = %d\n",a);
return 0;
}
Output –
Q3. Accept marks of 5 subjects (out of 100) of a student and display total marks and compute the
percentage also.
>>Source code
#include<stdio.h>
int main()
{
int m1,m2,m3,m4,m5;
float total=0.0,percentage=0.0;
printf("enter the marks of five subject out of 100\n");
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
total = m1+m2+m3+m4+m5;
printf("%f", total);
percentage =(total/500)*100;
printf("total marks = %f and percentage = %f",total,percentage);
return 0;
}
Output-
Q4 Accept the basic salary of an employee and compute the net salary after adding earnings and
subtracting deductions.
// PF is 2 % of basic
// Tax is 3 % of basic
// HRA is 5 % basic
// DA is 8 % of basic
>>Source Code
#include<stdio.h>
int main()
{
int basic_sal,pf,tax,hra,da,gross_salary;
printf("enter the salary\n");
scanf("%d",&basic_sal);
pf= (basic_sal*2)/100;
tax = (basic_sal*3)/100;
hra =(basic_sal*5)/100;
da= (basic_sal*8)/100;
gross_salary = basic_sal-pf-tax+hra+da;
printf("gross salary = %d",gross_salary);
return 0;
}
Output-
i) third variable
>>Source code -
#include<stdio.h>
int main()
{
int n1,n2,temp;
printf("enter the two numbers\n");
scanf("%d%d",&n1,&n2);
printf("before swapping n1 = %d, n2 = %d",n1,n2);
temp = n1;
n1=n2;
n2=temp;
printf("after swapping n1 = %d, n2 = %d",n1,n2);
return 0;
}
Output –
Output –
C Programming
Day-2
Assignment 2
Q1 Accept dimensions of a cylinder and print the surface area and volume (Hint: surface area = 2πr^2 +
2πrh, volume = π r^2 h).
Define a constant variable pi=3.14.
>>Source code-
#include<stdio.h>
int main()
{
float r,h,area,volume;
const float pi=3.14;
printf("enter the dimension of cylinder\n");
scanf("%f%f",&r,&h);
volume = 2*pi*r*r + 2*pi*r*h;
area = pi*r*r*h;
Q2.Accept temperatures in Fahrenheit (F) and print it in Celsius(C) and Kelvin (K) (Hint: C=5/9(F-32), K =
C + 273.15) doubt not showing result
>>Source code
#include<stdio.h>
int main()
{ int temp;
float celcius=0.0,k=0.0;
printf("enter the temperature\n");
scanf("%d",&temp);
celcius=((temp-32)*5)/9;
k = celcius + 273.15;
printf("Temperature in celsius = %f , kelvin = %f",celcius,k);
return 0;
}
Output –
>>Source code
#include<stdio.h>
int main()
{
int num;
printf("enter the num\n");
scanf("%d",&num);
if(num%5==0&&num%7==0)
printf("num is divisible by 5 and 7\n");
else
printf("num is not divisible by 5 and 7\n");
return 0;
}
Output-
Q5.Write a program, which accepts annual basic salary of an employee and calculates and displays the
Income tax as per the following rules. Basic: < 1,50,000 Tax = 0 ; 1,50,000 to 3,00,000 Tax = 20%
> 3,00,000 Tax = 30%
>>Source code
#include<stdio.h>
int main()
{
int basic_sal,tax;
printf("enter the salary\n");
scanf("%d",&basic_sal);
if(basic_sal<150000)
printf("Tax = 0");
else if(150000<basic_sal>300000)
{
tax = (basic_sal*20)/100;
printf("Tax = %d",tax);
}
else
{
tax = (basic_sal*30)/100;
printf("Tax = %d",tax);
}
return 0;
}
Output –
Q6. Accept a lowercase character from the user and check whether the character is a vowel or
consonant. (Hint: a,e,i,o,u are vowels)
>>Source code
#include<stdio.h>
int main()
{
char ch;
printf("enter the character\n");
scanf("%c",&ch);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
printf("it is vowel\n");
else
printf("it is consonant\n");
return 0;
}
Output-
Q7. Write a C program to input angles of a triangle and check whether triangle is valid or not
>>Source code
#include<stdio.h>
int main()
{
int a1,a2,a3,total;
printf("enter the angles of triangle\n");
scanf("%d%d%d",&a1,&a2,&a3);
total = a1+a2+a3;
if(total==180)
printf("valid triangle\n");
else
printf("invalid triangle\n");
return 0;
}
Output –
Q8. Write a C program to check whether a entered character is uppercase or lowercase alphabet.
>>Source code
#include<stdio.h>
int main()
{
char ch;
printf("enter the character\n");
scanf("%c",&ch);
if(ch>='a'&&ch<='z')
printf("character is lower case\n");
else
printf("character is upper case\n");
return 0;
}
Output –
Q9.
>>Source code
//9. Write a C program to accept a character and invert the case of it.
#include<stdio.h>
// doubt
int main()
{
char ch,up,lo;
printf("enter the character\n");
scanf("%c",&ch);
if(ch>='a'&&ch<='z')
{
printf("character is lower case\n");
up = ch-32;
printf("equivalent character = %c\n",up);
}
else
{
printf("character is upper case\n");
lo = ch+32;
printf("equivalent character = %c\n",up);
}
return 0;
}
Output –
Q10. Write a program to accept 3 numbers and compute minimum and maximum from them.
>>Source code
#include<stdio.h>
int main()
{
int n1,n2,n3;
printf("enter the number\n");
scanf("%d%d%d",&n1,&n2,&n3);
if(n1>n2)
{
if(n1>n3)
printf("max = %d",n1);
else
printf("max = %d",n3);
}
else
{
if(n2>n3)
printf("max = %d",n2);
else
printf("max = %d",n3);
}
if(n1<n2)
{
if(n1<n3)
printf("min = %d",n1);
else
printf("min = %d",n3);
}
else
{
if(n2<n3)
printf("min = %d",n2);
else
printf("min = %d",n3);
}
return 0;
}
Output –
Day-3
Assignment 3
Q1. Accept a single digit from the user and display it in words. For example, if digit entered is 9, display
Nine.
>>Source code-
#include<stdio.h>
int main()
{
int a;
printf("enter the single digit number\n");
scanf("%d",&a);
switch(a)
{
case 1:
printf("one\n");
break;
case 2:
printf("two\n");
break;
case 3:
printf("three\n");
break;
case 4:
printf("four\n");
break;
case 5:
printf("five\n");
break;
case 6:
printf("six\n");
break;
case 7:
printf("seven\n");
break;
case 8:
printf("eight\n");
break;
case 9:
printf("nine\n");
break;
}
return 0;
}
Output –
Q2. Write a program, which accepts two integers and an operator as a character (+ - * / ), performs the
corresponding operation and displays the result.
>>Source code-
#include<stdio.h>
int main()
{
int a,b,result;
char ch;
printf("enter the two numbers\n");
scanf("%d%d",&a,&b);
printf("enter the operation\n");
scanf(" %c",&ch);
switch(ch)
{
case '+':
result = a+b;
printf("addition = %d",result);
break;
case '-':
result = a-b;
printf("substraction = %d",result);
break;
case '*':
result = a*b;
printf("multiplication = %d",result);
break;
case '/':
result = a/b;
printf("divide = %d",result);
break;
default :
printf("invalid choice");
break;
}
return 0;
}
Output –
Q3.
>>Source code
#include<stdio.h>
// q3
int main()
{
int x,y,c,rem,d,temp;
float quo;
printf("enter the two numbers\n");
scanf("%d%d",&x,&y);
printf("enter the choice\n");
scanf(" %d",&c);
switch(c)
{
case 1:
if(x==y)
printf("x == y\n");
else
printf("x is not equal to y\n");
break;
case 2:
if(x<y)
printf("x is less than y\n");
else
printf("x is not less than y\n");
break;
case 3:
quo = (float)x/y;
rem = x%y;
printf("quotient = %f and reminder =%d\n",quo,rem);
break;
case 4:
printf("enter the number to check\n");//x=20 y=10
scanf("%d",&d);//15
if(x<d)
{
if(d<y)
printf(" %d is in the range\n",d);
}
else if(d<x)
{
if(d>y)
printf(" %d is in the range\n",d);
}
else
printf(" %d not in the range\n",d);
break;
case 5:
temp =x;
x=y;
y=temp;
printf("swapping x= %d and y=%d\n",x,y);
break;
default :
printf("invalid choice\n");
}
}
Output –
Q4
>>Source code –
#include<stdio.h>
//q4
int main()
{
float r,area,pi=3.14,circumference,volume,height;
int choice;
printf("enter the radius os cicle\n");
scanf("%f",&r);
printf("enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
area = pi*r*r;
printf("area of circle = %f\n",area);
break;
case 2:
circumference = 2*pi*r;
printf("circumference = %f\n",circumference);
break;
case 3:
printf("enter the height of sphere\n");
scanf("%f",&height);
volume = pi*r*r*height;
printf("volume = %f",volume);
break;
default :
printf("invalid choice\n");
}
return 0;
}
Output –
Q5
>>Source code
#include<stdio.h>
int main()
{
float n1,n2,d1,d2,a,b,c;
int choice;
printf("enter the numerators\n");
scanf("%f%f",&n1,&n2);
printf("enter the denominator\n");
scanf("%f%f",&d1,&d2);
printf("1.ADD\n 2.SUBCTRACT\n 3. MULTIPLY");
printf("enter the choice\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
a= (n1*d2+n2*d1);
b= d1*d2;
c= a/b;
printf("result = %f",c);
break;
case 2:
a= (n1*d2-n2*d1);
b= d1*d2;
c= a/b;
printf("result = %f",c);
break;
case 3:
a= (n1*n2);
b= d1*d2;
c= a/b;
printf("result = %f",c);
break;
default:
printf("invalid choice\n");
break;
}
return 0;
}
Output –
Day-4
Assignment 4
Q1. Write a program that accepts numbers continuously as long as the number is positive and prints the
sum of the given numbers.
>>Source code-
#include<stdio.h>
int main()
{
int a,total=0;
printf("enter the numbers\n");
for(;a!=0;)
{
scanf("%d",&a);
total = total +a;
}
printf("addition of numbers = %d\n",total);
return 0;
}
Output –
Q3.Write a program to accept a character, an integer n and display the next n characters.
>>Source code
#include<stdio.h>
int main()
{
int n,i;
char ch;
for(i=1;i<n;i++)
{
printf("character at %d is %c\n",ch+i,ch+i);
}
return 0;
}
Output-
Q4.Write a program to calculate factorial of a number.
>>Source code
#include<stdio.h>
int main()
{
int n,result=1,i;
printf("enter the integer\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
result = result *i;
printf("factorial of %d is %d",n,result);
return 0;
}
Output-
>>Source code
#include<stdio.h>
int main()
{
int n,result=1,i;
printf("enter the integer\n");
scanf("%d",&n);
printf("factors of %dis\t",n);
for(i=1;i<=n;i++)
{
if(n%i==0)
printf("%d,",i);
}
return 0;
}
Output –
Q7
>>Source code-
#include<stdio.h>
int main()
{
float r,area,pi=3.14,h,b;
int choice,len,wid;
do
{
printf("\n1.Compute area of circle\n");
printf("2.Compute area of rectangle\n");
printf("3.Compute area of triangle\n");
printf("4.exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch (choice)
{
case 1:
printf("enter the radius of circle\n");
scanf("%f",&r);
area = 2*pi*r*r;
printf("area of circle = %f\n",area);
break;
case 2:
printf("enter the width and length of rectagle\n");
scanf("%d%d",&len,&wid);
area = len*wid;
printf("area of rectangle = %d",area);
break;
case 3:
printf("enter the height and base of triangle\n");
scanf("%f%f",&h,&b);
area=(h*b)/2;
printf("area of triangle = %f\n",area);
break;
case 4:
printf("exit\n");
break;
default:
printf("invalid choice\n");
break;
}
} while (choice!=4);
return 0;
}
Output –
Day-5
Assignment 5
Q1.Write a program to accept n numbers in an array and display the largest and smallest number. Using
these values, calculate the range of elements in the array.
>>Source code-
#include<stdio.h>
int main()
{
int arr[100],i,n,min,max;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
min = max= arr[0];
for(i=0;i<n;i++)
{
if(min>arr[i])
min = arr[i];
}
for(i=0;i<n;i++)
{
if(max<arr[i])
max = arr[i];
}
printf("min = %d and max =%d\n",min,max);
printf("The range of element in array in between %d to %d",min,max);
return 0;
}
Output-
Q2. Write a program to accept an array of n elements and a number say key. Check whether key is
present in the array or not.
>>Source code
#include<stdio.h>
int main()
{
int arr[100],i,j=0,key,n;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
printf("enter the key value\n");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(key==arr[i])
j= arr[i];
}
if(j)
printf("key is present in array\n");
else
printf("key is not present in array\n");
return 0;
}
Output-
Q3. Write a program to accepts an integer array and an integer say num and counts the occurrences of
the num in the array.
>>Source code
#include<stdio.h>
int main()
{
int arr[100],i,count=0,n,key;
printf("enter the size of array\n");
scanf("%d",&n);
printf("enter the key value\n");
scanf("%d",&key);
//k=arr[0]; //10,20,35,5,40
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(arr[i]<arr[j])
{
k=arr[i];
arr[i]=arr[j];
arr[j]=k;
}
}
}
}
Output-
Q2. Write a program to accept a matrix A of size mXn and store its transpose in matrix B. Display matrix
B
>>Source code-
#include<stdio.h>
int main()
{
int arr[10][10],i,j,k,b[10][10];
int row,col;
printf("enter the row nd colum\n");
scanf("%d%d",&row,&col);
printf("enter the elements of matrix");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
scanf("%d",&arr[i][j]);
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
b[i][j] = arr[j][i];
}
printf("entered matrix is\n");
printf("\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}
printf("transpose of given matrix is\n");
printf("\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
return 0;
}
Output-
Q3. Write a program to add and multiply two matrices. Perform necessary checks before adding and
multiplying the matrices.
>>Source code-
#include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3];
int i,j,sum,multi;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
printf("\n");
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++)
c[i][j] = a[i][j] * b[i][j];
}
printf("Multiplication of matrix is\n");
printf("\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
return 0;
}
Output-
Q4-a
>>Source code-
#include<stdio.h>
int main()
{
int a[4][4],b[4][4],c[4][4];
int i,j,k;
printf("enter the 1st matrix element\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
b[i][j] =a[j][j];
}
printf("\n");
k=1;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(b[i][j] !=a[i][j])
{
k++;
break;
}
}
}
if(k==1)
printf("matrix is symmetric\n");
else
printf("matrix is not symmetric\n");
return 0;
}
Output-
Q4-b
>>Source Code-
#include<stdio.h>
int main()
{
int a[4][4],b[4][4],c[4][4];
int i,j,trace=0;
String
Q1.
#include<stdio.h>
#include<string.h>
}
}
int is_digit(char ch)
{
if(ch>=0)
{
if(ch<10)
return 1;
else
return 0;
}
else
return 0;
}
Output-
Q2. Write a program that accepts n strings and displays the longest string. Use array of strings.
>>Sources code-
#include<stdio.h>
#include<string.h>
int main()
{
char arr[100][100];
int i,j,k,num,len;
printf("enter the number of strings required\n");
scanf("%d",&num);
printf("enter the strings\n");
for(i=0;i<num;i++)
scanf("%s",&arr[i]);
len = strlen(arr[0]);
k=0;
for(i=0;i<num;i++)
{
if(len<strlen(arr[i]))
{
len = strlen(arr[i]);
k=i;
}
}
printf("longest string is %s its length = %d",arr[k],len);
return 0;
}
Output-
Day-6
Assignment 6
Q1
>>Souce code-
#include<stdio.h>
int main()
{
int arr[100],i,j,sum_even=0,num,sum_odd=0;
printf("enter the number of element in array\n");
scanf("%d",&num);
printf("enter the array element\n");
for(i=0;i<num;i++)
scanf("%d",&arr[i]);
int *ptr = &arr[0];
for(i=0;i<num;i++)
{
if(*ptr%2==0)
{
sum_even = sum_even + arr[i];
}
else
{
sum_odd = sum_odd +arr[i];
}
ptr++;
}
printf(" sum of all even no = %d\n sum of all odd no is %d\n",sum_even,sum_odd);
return 0;
}
Output-
Q2
>>Source code-
#include<stdio.h>
int iseven(int num)
{
if(num%2==0)
return 1;
else
return 0;
}
int main()
{
int num,i;
printf("enter the number\n");
scanf("%d",&num);
i = iseven(num);
if(i==1)
printf("number is even\n");
else
printf("number is odd\n");
return 0;
}
Output-
Q3
>>Soource code
#include<stdio.h>
int isprime(int num)//11
{
int i,j=0;
for(i=2;i<num/2;i++)
{
if(num%i!=0)
continue;
else
return 0;
}
return 1;
}
int main()
{
int num,i,j;
printf("enter the number\n");
scanf("%d",&num);
j= isprime(num);
if(j==1)
printf("number is prime\n");
else
printf("number is not prime\n");
return 0;
}
Output –
Q4.
>>Source code-
# include <stdio.h>
long factorial(int x)
{
int i;
long fact=1;
for(i=1;i<=x;i++)
fact=fact*i;
return fact;
}
double power(int x,int y)
{
double ans=1.0;
int i;
for(i=1;i<=y;i++)
ans=ans*x;
return ans;
}
void main()
{
int i,n,cnt=3;
float x;
double sum=0;
printf("\nEnter value for x : ");
scanf("%f",&x);
printf("\nEnter number of terms : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i==1)
sum=x;
else
{
if(i%2==0) // for even terms subtract
{
sum=sum - (power(x,cnt)/factorial(cnt));
}
else // for odd terms add
{
sum=sum + (power(x,cnt)/factorial(cnt));
}
cnt=cnt+2; // it makes cnt as 5,7,9,...
}
}
printf("\nsum of Taylor series of %d terms : %f",n,sum);
}
Output-
Q5
CPP PROGRAMMING
DAY-1
ASSIGNMENT NO-1
Q1
>>Source code
#include<iostream>
using namespace std;
int main()
{
int a,b,sum;
cout<<"enter the two number"<<endl;
cin>>a>>b;
sum = a+b;
cout<<"sum of two number is = "<<sum<<endl;
return 0;
}
Output-
return 0;
}
Output-
Q6.Sum of series :
1+2+3+….+n
>>Source code-
#include<iostream>
using namespace std;
int main()
{
int num,i,sum=0;
cout<<"enter the number"<<endl;
cin>>num;
for(i=1;i<=num;i++)
{
sum = sum +i;
}
cout<<"sum of number = "<<sum<<endl;
return 0;
}
Output-
Q8.Write a program to find sum of all even and odd numbers between 1 to n.
>>Souce code-
#include<iostream>
using namespace std;
int main()
{
int num,i,sum_even=0,sum_odd=0;
cout<<"enter the number"<<endl;
cin>>num;
for(i=1;i<=num;i++)
{
if(i%2==0)
sum_even= sum_even +i;
else
sum_odd= sum_odd+i;
}
cout<<"sum of all even no in between 1 and "<<num<<"is = "<<sum_even<<endl;
cout<<"sum of all odd no in between 1 and "<<num<<"is = "<<sum_odd<<endl;
return 0;
}
Output-
Q13.
>>Source code-
#include<iostream>
using namespace std;
int main()
{
int option,p=0;
cout<<"Welcome to Our Pizza Shop";
do
{
cout<<"Select Menu You love to order"<<endl<<"1]Greek Pizza-500"<<endl<<"2]California Pizza-
800"<<endl<<"3]Silician Pizza-750"<<endl<<"4]New-york style Pizza-1000"<<endl<<"5]Order Now-";
cin>>option;
switch (option)
{
case 1:
p+=500;
break;
case 2:
p+=800;
break;
case 3:
p+=750;
break;
case 4:
p+=1000;
break;
}
} while (option!=5);
cout<<"Your Order is Ready!!!"<<endl<<"Total Amount:"<<p<<" only";
return 0;
}
Output-
Q14.
>>Source code-
#include<iostream>
using namespace std;
int main()
{
int arr[100],i,key,num,choice,flag=0;
cout<<"enter the number of element in array"<<endl;
cin>>num;
cout<<"enter the elements in array"<<endl;
for(i=0;i<num;i++)
cin>>arr[i];
do
{
cout<<"1. print array"<<endl;
cout<<"2. search element array"<<endl;
cout<<"3. reverse array"<<endl;
cout<<"4. Even number from array"<<endl;
int main()
{
int i,j,num;
cout<<"enter the number"<<endl;
cin>>num;
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
cout<<"* ";
cout<<endl;
}
return 0;
}
Output-
DAY-2&3
ASSIGNMENT NO-2
Q1. Write a program to create student class with data members rollno, marks1,mark2,mark3.Accept
data (acceptInfo()) and display using display member function. Also display total,percentage and grade.
>>Source code
#include<iostream>
using namespace std;
class student
{
int roll_no,mark1,mark2,mark3;
public:
void acceptinfo()
{
cout<<"enter the roll no. and marks"<<endl;
cin>>roll_no>>mark1>>mark2>>mark3;
}
void printdata()
{
cout<<"roll no is "<<roll_no<<" mark1 = "<<mark1<<" mark2 = "<<mark2<<" mark3 =
"<<mark3<<endl;
cout<<"total = "<<mark1+mark2+mark3<<endl;
cout<<"percentage = "<< ((mark1+mark2+mark3)*100)/300<<endl;
}
};
int main()
{
student s1;
s1.acceptinfo();
s1.printdata();
return 0;
}
Output-
Q2.
>>Source code-
#include<iostream>
using namespace std;
class person
{
string name;
int age;
string city;
public :
person()
{
name = "abc";
age = 0;
city ="xyz";
}
void display()
{
cout<<"name = "<<name<<" age = "<<age<<" city = "<<city<<endl;
}
};
int main()
{
person p1;
p1.setname("shri");
p1.setcity("nanded");
p1.setage(25);
p1.display();
person p2;
p2.display();
person p3("ram",25,"cpur");
p3.display();
return 0;
}
Output-
Q3.
>>Source code-
#include<iostream>
using namespace std;
class date
{
int dd;
int mm;
int yy;
public :
date()
{
dd = 00;
mm = 00;
yy = 00;
}
date(int d,int m,int y)
{
this->dd = d;
this->mm = m;
this->yy = y;
}
void setdd(int d)
{
dd=d;
}
void setmm(int m)
{
mm=m;
}
void setyy(int y)
{
yy=y;
}
int getdd()
{
return dd;
}
int getmm()
{
return mm;
}
int getyy()
{
return yy;
}
void display()
{
cout<<"date is "<<dd<<"/"<<mm<<"/"<<yy<<endl;
}
};
int main()
{
date d1;
d1.setdd(17);
d1.setmm(9);
d1.setyy(2021);
d1.display();
date d2;
d2.display();
date d3(17,9,2021);
d3.display();
return 0;
}
Output-
Q4.
#include<iostream>
using namespace std;
class book
{
string bname;
int id;
string author;
int price;
public :
book()
{
bname = "none";
id = 00;
author = "none";
price = 00;
}
string getbname()
{
return bname;
}
int getid()
{
return id;
}
string getauthor()
{
return author;
}
int getprice()
{
return price;
}
void display()
{
cout<<"book name is **"<<bname<<"**"<<" id is "<<id<<" author is "<<author<<" mrp =
"<<price<<endl;
}
};
int main()
{
book b1;
b1.setbname("life amazing secreate");
b1.setid(101);
b1.setauthor("gaur gopal das");
b1.setprice(100);
b1.display();
book b2;
b2.display();
Q5.
>>Source code-
#include<iostream>
using namespace std;
class point
{
int x;
int y;
public :
point()
{
x=0;
y=0;
}
point(int x, int y)
{
this->x=x;
this->y=y;
}
void setx(int a)
{
x= a;
}
void sety(int b)
{
y= b;
}
int getx()
{
return x;
}
int gety()
{
return y;
}
void display()
{
cout<<"x = "<<x<<" y = "<<y<<endl;
}
};
int main()
{
point p1;
p1.setx(10);
p1.sety(20);
p1.display();
point p2;
p2.display();
point p3(100,200);
p3.display();
return 0;
}
Output-
Q6.
>>Source code-
#include<iostream>
using namespace std;
class ComplexNumber
{
int real;
int imaginary;
public :
ComplexNumber()
{
real=0;
imaginary=0;
}
ComplexNumber(int x, int y)
{
this->real=x;
this->imaginary=y;
}
void setreal(int a)
{
real= a;
}
void setimaginary(int b)
{
imaginary= b;
}
int getreal()
{
return real;
}
int getimaginary()
{
return imaginary;
}
void display()
{
cout<<"real = "<<real<<" imaginary = "<<imaginary<<endl;
}
};
int main()
{
ComplexNumber p1;
p1.setreal(10);
p1.setimaginary(20);
p1.display();
ComplexNumber p2;
p2.display();
ComplexNumber p3(100,200);
p3.display();
return 0;
}
Q7.
>>Source code-
#include<iostream>
using namespace std;
class date
{
int day;
int month;
int year;
public :
date()
{
day = 00;
month = 00;
year = 00;
}
date(int d,int m,int y)
{
this->day = d;
this->month = m;
this->year = y;
}
void setday(int d)
{
day=d;
}
void setmonth(int m)
{
month=m;
}
void setyear(int y)
{
year=y;
}
int getday()
{
return day;
}
int getmonth()
{
return month;
}
int getyear()
{
return year;
}
void display()
{
cout<<"date is "<<day<<"/"<<month<<"/"<<year<<endl;
}
};
int main()
{
date d1;
d1.setday(17);
d1.setmonth(9);
d1.setyear(2021);
d1.display();
date d2;
d2.display();
date d3(17,9,2021);
d3.display();
return 0;
}
Q8
>>Source code
#include<iostream>
using namespace std;
class date
{
int day;
int month;
int year;
public :
date()
{
day = 00;
month = 00;
year = 00;
}
class employee
{
int id;
string name;
date dobj;
public :
employee()
{
id = 00;
name ="none";
}
employee(int id,string name,int dd,int mm,int yy)
{
this->id = id;
this->name =name;
this->dobj = date(dd,mm,yy);
}
void acceptdata()
{
cout<<"enter the name and id"<<endl;
cin>>name>>id;
dobj.acceptdob();
}
void displaydata()
{
cout<<"name of employee is "<<name<<" id is "<<id<<endl;
dobj.displaydob();
}
};
int main()
{
employee e1;
e1.acceptdata();
e1.displaydata();
employee e2;
e2.displaydata();
return 0;
}
Output-
Q9.
>>Source code-
#include<iostream>
using namespace std;
class employee
{
int empid;
string empname;
double basic_sal;
double hra;
static double medical;
double pf,net_sal,gross_sal;
static double pt;
public :
employee()
{
empid =00;
empname = "none";
basic_sal =00;
net_sal = 00;
gross_sal = 00;
}
}
void display()
{
cout<<"employee id = "<<empid<<endl<<" emp name = "<<empname<<endl;
cout<<"gross salary = "<<gross_sal<<" net salary = "<<net_sal<<endl;
}
};
double employee :: medical = 1000;
double employee ::pt = 200;
int main()
{
employee e1(101,"shri",25000);
e1.display();
return 0;
}
Output-
Q10.
>>Source code-
#include<iostream>
using namespace std;
class employee
{
int empid,dep_id;
string empname;
public : double basic_sal;
public :
employee()
{
empid =00;
dep_id = 00;
empname = "none";
basic_sal =00;
}
employee(int id,int did,string name,int bsal)
{
this->empid =id;
this->dep_id = did;
this->empname = name;
this->basic_sal =bsal;
}
void display()
{
cout<<"employee id = "<<empid<<endl;
cout<<"department id = "<<dep_id<<endl;
cout<<"employee name = "<<empname<<endl;
cout<<"employee basic salary = "<<basic_sal<<endl;
}
};
};
public :
workerstate()
{
hourlyRate =0;
hoursWorked= 0;
net_sal =0;
}
workerstate(int empid,int did,string name, double bsal,int hw,int hrate):
employee(empid,did,name,bsal)
{
this->hoursWorked= hw;
this->hourlyRate = hrate;
}
int gethourlyRate()
{
return hourlyRate;
}
void compute_net_salary()
//void compute_net_salary()
{
net_sal = basic_sal + (hoursWorked * hourlyRate);
cout<<"net salary of employee = "<<net_sal;
}
void display()
{
cout<<"working hour of worker "<<hoursWorked<<endl;
cout<<"overtime rate = "<<hourlyRate<<endl;
}
};
int main()
{
workerstate w1(101,10,"shri",25000,10,120);
//workerstate w1;
w1.compute_net_salary();
mgrstate m1;
m1.compute_net_salary();
cout<<endl;
cout<<endl;
cout<<endl;
cout<<endl;
employee *arr[2];
mgrstate mag;
workerstate wok;
int flag=0;
while(flag!=1)
{
cout<<"enter your choice"<<endl;
int ch;
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter salary and bonus"<<endl;
cin>>mag.basic_sal;
cin>>mag.bonus;
arr[index]= &mag;
index++;
break;
case 2:
cout<<"enter working hour and its rate"<<endl;
cin>>wok.hoursWorked;
cin>>wok.hourlyRate;
cin>>wok.basic_sal;
arr[index]= &wok;
index++;
break;
case 3:
cout<<"All employee salary is"<<endl;
for(int i=0;i<3;i++)
{
arr[i]->compute_net_salary();
}
break;
case 4:
flag=1;
break;
}
}
return 0;
}
Output-
Q11
>>Source code-
#include<iostream>
using namespace std;
class BankAccount
{
int acc_no;
string customer_name;
double balance;
public :
BankAccount()
{
cout<<"enter the bank details"<<endl;
cin>>acc_no>>customer_name>>balance;
this->acc_no= acc_no;
this->balance= balance;
this->customer_name= customer_name;
}
BankAccount(int acc_no,string customer_name,double balance)
{
this->acc_no = acc_no;
this->customer_name = customer_name;
this->balance = balance;
}
void display()
{
cout<<"your balance is = "<<balance<<endl;
}
};
int main()
{
BankAccount b1;
b1.display();
b1.deposit(10000);
b1.withdraw(1000);
b1.display();
return 0;
}
Output-
Q12.
>>Source Code-
#include<iostream>
using namespace std;
class shape
{
public: virtual void area()=0;
};
public :
circle()
{
r=0;
}
circle(float r)
{
this->r=r;
}
void area()
{
cout<<"area of circle "<<(3.14*r*r)<<endl;
}
};
public :
square()
{
side = 0;
}
square(int side)
{
this->side = side;
}
void area()
{
cout<<"area of circle "<<(side*side)<<endl;
}
};
int main()
{
rectangle r1(20,30);
r1.area();
rectangle r2;
r2.area();
circle c1(10);
c1.area();
circle c2;
c2.area();
square s1(10);
s1.area();
square s2;
s2.area();
return 0;
}
Output-