Documente Academic
Documente Profesional
Documente Cultură
C++ program to add, subtract, multiply and divide two complex numbers
using structures
#include<iostream.h>
#include<conio.h>
#include<math.h>
struct complex
float rel;
float img;
}s1,s2;
void main()
clrscr();
float a,b;
cin>>s1.rel>>s1.img;
cin>>s2.rel>>s2.img;
//Addition
a=(s1.rel)+(s2.rel);
b=(s1.img)+(s2.img);
cout<<nAddition: <<(<<a<<)<<+<<(<<b<<)<<i;
//Subtraction
a=(s1.rel)-(s2.rel);
b=(s1.img)-(s2.img);
cout<<nSubtraction: <<(<<a<<)<<+<<(<<b<<)<<i;
//Multiplication
a=((s1.rel)*(s2.rel))-((s1.img)*(s2.img));
b=((s1.rel)*(s2.img))+((s2.rel)*(s1.img));
cout<<nMultiplication: <<(<<a<<)<<+<<(<<b<<)<<i;
//Division
a=(((s1.rel)*(s2.rel))+((s1.img)*(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));
b=(((s2.rel)*(s1.img))-((s1.rel)*(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));
cout<<nDivision: <<(<<a<<)<<+<<(<<b<<)<<i;
getch();
}
2.Write a C program to check whether a date is valid or not.
#include <stdio.h>
#include <string.h>
#define MONTH 12
int main() {
char str[100];
str[strlen(str) - 1] = '\0';
/* i/p day, month and year from the above string from user */
printf("Invalid Date!!\n");
return 0;
printf("Invalid Date!!\n");
return 0;
if (month == 2) {
if (year % 100 == 0) {
if (year % 400 == 0) {
flag = 1;
} else if (year % 4 == 0) {
flag = 1;
return 0;
printf("Invalid Date!!\n");
return 0;
printf("Valid Date!!\n");
return 0;
OUTPUT;
jp@jp-VirtualBox:~/$ ./a.out
Invalid Date
#include<iostream>
using namespace std;
int area(int);
int area(int,int);
float area(float);
float area(float,float);
int main()
int s,l,b;
float r,bs,ht;
cin>>s;
cin>>l>>b;
cin>>r;
cin>>bs>>ht;
int area(int s)
return(s*s);
}
int area(int l,int b)
return(l*b);
float area(float r)
return(3.14*r*r);
return((bs*ht)/2);
OUTPUT:
Sample Output
Area of rectangle is 18
Area of triangle is 8
#include<stdio.h>
#include<process.h>
#define MAXSIZE 10
void push();
int pop();
void traverse();
int stack[MAXSIZE];
int Top=-1;
int main()
int choice;
char ch;
do
scanf("%d",&choice);
switch(choice)
case 1: push();
break;
break;
case 3: traverse();
break;
}
printf("nDo You Wish To Continue (Y/N)");
fflush(stdin);
scanf("%c",&ch);
while(ch=='Y' || ch=='y');
return 0;
void push()
int item;
if(Top == MAXSIZE - 1)
exit(0);
else
scanf("%d",&item);
Top= Top+1;
stack[Top] = item;
int pop()
int item;
if(Top == -1)
exit(0);
else
item = stack[Top];
Top = Top-1;
return(item);
void traverse()
int i;
if(Top == -1)
exit(0);
else
for(i=Top;i>=0;i--)
printf("%dn",stack[i]);
}
OTutput:
#include<iostream.h>
#include<conio.h>
class queue
{
public:
int q[5],front,rear,x,result;
void enq();
void dque();
void disp();
queue()
front=0;
rear=0;
};
void queue::enq()
if(rear>=5)
cout<<"\nQueue overflow!!\n";
else
cin>>x;
rear++;
q[rear]=x;
void queue::dque()
{
if(rear==0)
cout<<"\nQueue underflow!!\n";
else
if(front==rear)
front=0;
rear=0;
else
front++;
result=q[front];
cout<<result;
void queue::disp()
if(rear==0)
cout<<"\nQueue underflow!!\n";
else
for(int i=front+1;i<=rear;i++)
cout<<q[i]<<"\t";
void main()
{
int c;
queue qu;
clrscr();
// cout<<"\n*****";
// cout<<"\nQUEUE";
// cout<<"\n*****";
do
cout<<"\n1.Insertion\n2.Deletion\n3.Display\n";
cin>>c;
switch(c)
case 1:
qu.enq();
break;
case 2:
qu.dque();
break;
case 3:
qu.disp();
break;
default:
cout<<"\nInvalid choice!!\n";
}
}
while(c<4);
getch();
OUTPUT
#include<iostream>
struct node
{
int data;
};
class stack
public:
stack() // constructure
top=NULL;
};
// PUSH Operation
void stack::push()
int value;
cout<<"nPUSH Operationn";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
// POP Operation
void stack::pop()
if(top==NULL)
temp=top;
top=top->next;
delete temp;
// Show stack
void stack::show()
{
while(ptr1!=NULL)
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
cout<<"NULLn";
// Main function
int main()
stack s;
int choice;
while(1)
cout<<"n-----------------------------------------------------------";
cout<<"1:PUSHn2:POPn3:DISPLAY STACKn4:EXIT";
cin>>choice;
switch(choice)
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
return 0;
break;
default:
break;
return 0;
OUTPUT:
-----------------------------------------------------------
1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
PUSH Operation
-----------------------------------------------------------
1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
PUSH Operation
-----------------------------------------------------------
1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
POP Operation........
Poped value is 5
-----------------------------------------------------------
1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
The stack is
12 ->NULL
-----------------------------------------------------------
1:PUSH
2:POP
3:DISPLAY STACK
4:EXIT
Enter your choice(1-4): 4
*/
#include<iostream>
#include<stdio.h>
#include<conio.h>
struct node
int data;
node *next;
void push(int x)
np = new node;
np->data = x;
np->next = NULL;
if(front == NULL)
rear->next = NULL;
else
rear->next = np;
rear = np;
rear->next = NULL;
int remove()
int x;
if(front == NULL)
cout<<"empty queue\n";
else
p = front;
x = p->data;
front = front->next;
delete(p);
return(x);
int main()
cin>>n;
while (c < n)
{
cin>>x;
push(x);
c++;
cout<<"\n\nRemoved Values\n\n";
while(true)
if (front != NULL)
cout<<remove()<<endl;
else
break;
getch();
Output
Removed Values
#include <stdio.h>
int main()
char line[150];
scanf("%[^\n]", line);
line[i]=='U')
++vowels;
++consonants;
++digits;
++spaces;
}
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
return 0;
OUTPUT
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2
#include<conio.h>
#include<iomanip.h>
#include<stdlib.h>
#include<ctype.h>
#include<fstream.h>
void main( )
ofstream outfile;
ifstream infile;
char fname1[10],fname2[20];
char ch,uch;
clrscr( );
cin>> fname1;
cin>>fname2;
infile.open(fname1);
if( infile.fail( ) )
getch();
exit(1);
outfile.open( fname2);
if(outfile.fail( ))
getch();
exit(1);
while( !infile.eof( ) )
ch = (char) infile.get( );
uch = toupper(ch);
outfile.put(uch);
}
infile.close( );
outfile.close( );
getch( );
OUTPUT:
C:\text1.txt
D:\new.txt
Input file
Asbcdefghijklmnopqrstuvwxyz
Output file
ASBCDEFGHIJKLMNOPQRSTUVWXYZ
10.WAP TO CREATE TEXT FILE AND FIND THE AVERAGE WORD SIZE
#include"fstream.h"
#include"conio.h"
void calculate()
fstream tfile;
clrscr();
tfile.open("Report.txt",ios::in);
char arr[20];
char ch;
int i=0,sum=0,n=0;
while(tfile)
tfile.get(ch);
arr[i] = ch;
i++;
i--;
sum = sum + i;
i = 0;
n++;
void main()
calculate();
int main()
{
printf("Enter information:\n");
printf("Displaying Information:\n");
printf("Name: ");
puts(s.name);
return 0;
}
Output
Enter information:
Enter name: Jack
Enter roll number: 23
Enter marks: 34.5
Displaying Information:
Name: Jack
Roll number: 23
Marks: 34.5
12.#include<iostream>
int fibonacci(int n)
{
if((n==1)||(n==0))
{
return(n);
}
else
{
return(fibonacci(n-1)+fibonacci(n-2));
}
}
int main()
{
int n,i=0;
cout<<"Input the number of terms for
Fibonacci Series:";
cin>>n;
cout<<"\nFibonacci Series is as
follows\n";
while(i<n)
{
cout<<" "<<fibonacci(i);
i++;
}
return 0;
}
13.#include <stdio.h>
void main()
{
int i, num, odd_sum = 0, even_sum = 0;
$ a.out
Enter the value of num
100
Sum of all odd numbers = 2500
Sum of all even numbers = 2550
14.#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *a,*b,*temp;
cout<<Enter value of a and b:;
cin>>*a>>*b;
temp=a;
a=b;
b=temp;
cout<<nAfter
swapingna=<<*a<<nb=<<*b;
getch();
}
1
5.
#include <stdio.h>
int main()
{
int m, n, c, d, first[10][10], second[10]
[10], sum[10][10];
return 0;
}
16.
Create the following tables with the given
structures and insert sample data as
specified: -
A) SALESPEOPLE
Snum number(4)
Sname varchar2(10)
city varchar2(10)
Comm number(3,2)
Answer:
CREATE TABLE salespeople ( snum
number(4), sname varchar2(10), city
varchar2(10), comm number(3,2) );
17.
SQL Query to find second highest salary of
Employee?
answer:select MAX(Salary) from Employee
WHERE Salary NOT IN (select MAX(Salary)
from Employee
18.Write an SQL Query to print the name
of the distinct employee whose DOB is
between 01/01/1960 to 31/12/1975.
answer:SELECT DISTINCT EmpName FROM
Employees WHERE DOB BETWEEN
01/01/1960 AND 31/12/1975
19.
= AA BC+ABBC+ABCC+ABC
// TABE AB as common term //
= AA BC+ABBC+AB(CC+C )
// CC+C = 1 //
= AA BC+ABBC+AB
// take A as common term //
= AA BC+A(BBC+B) //
B+BC = B+C //
= AABC+A(BB+C)
= AABC+AB+AC
= C(AB+A)+AB+AC
= C(B+A)+AB+AC
= CB+AC+AB+AC // AC+AC =
AC //
= AB+BC+AC
= RHS
LHS = RHS
20.
Simplify the Equation:
1. (A + C)(AD + AD) + AC + C
(A + C)A(D + D) + AC + C //
Distributive rule //
(A + C)A + AC + C //
Complementary Identity. //
A((A + C) + C) + C //
Commutative & Distributive //
A(A + C) + C
AA + AC + C //
simplify //
A+(A+1)C // A+1 = 1
//
A+C
21.Bhartiya Connectivity Association is
planning to spread their offices in four
major cities in India to provide regional IT
infrastructure support in the field of
Education & Culture. The company has
planned to setup their head office in New
Delhi in three locations and have named
their New Delhi offices as Front Office
,Back Office and Work Office .The
company has three more regional offices
as South Office ,East Offficeand West
Office located in other three major cities
of India. A rough layout of the same is as
follows:
4m
Approximate distance between these
offices as per network survey team is as
follows
Place From
Place To Distance
Back Office Front Office 10 KM
Back Office Work Office 70 KM
Back Office East Office1291KM
Back Office West Office 790 KM
Back Office South Office 1952KM
In continuation of the above, the company
experts have planned to install the
following number of computers in each of
their offices: