Sunteți pe pagina 1din 48

1.

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;

cout<<Enter real and imaginary part of 1st complex number:;

cin>>s1.rel>>s1.img;

cout<<Enter real and imaginary part of 2nd complex number:;

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 YEAR 2013

#define MONTH 12

int main() {

int day, month, year, flag = 0;

int daysInMonth[12] = {31, 28, 31, 30, 31, 30,

31, 31, 30, 31, 30, 31};

char str[100];

/* get the input date from the user */


printf("Enter your input date(DD/MM/YYYY):");

fgets(str, 100, stdin);

str[strlen(str) - 1] = '\0';

/* i/p day, month and year from the above string from user */

sscanf(str, "%d/%d/%d", &day, &month, &year);

/* year should not be greater than current year */

if (year > YEAR || year <= 0) {

printf("Invalid Date!!\n");

return 0;

/* only 12 months in a year */

if (month > MONTH || month <= 0) {

printf("Invalid Date!!\n");

return 0;

/* leap year check if month is february */

if (month == 2) {

if (year % 100 == 0) {

if (year % 400 == 0) {

flag = 1;

} else if (year % 4 == 0) {
flag = 1;

if (day > (daysInMonth[month - 1] + flag)) {

printf("Invalid Date !!\n");

return 0;

/* check whethe day is valid or not */

if (day > daysInMonth[month - 1]) {

printf("Invalid Date!!\n");

return 0;

/* print the result */

printf("Valid Date!!\n");

return 0;

OUTPUT;
jp@jp-VirtualBox:~/$ ./a.out

Enter your input date(DD/MM/YYYY):31/02/2001

Invalid Date

3.wap using function overloading to calculate area of circle rectangle square

#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;

cout<<"Enter side of a square:";

cin>>s;

cout<<"Enter length and breadth of rectangle:";

cin>>l>>b;

cout<<"Enter radius of circle:";

cin>>r;

cout<<"Enter base and height of triangle:";

cin>>bs>>ht;

cout<<"Area of square is"<<area(s);

cout<<"\nArea of rectangle is "<<area(l,b);

cout<<"\nArea of circle is "<<area(r);

cout<<"\nArea of triangle is "<<area(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);

float area(float bs,float ht)

return((bs*ht)/2);

OUTPUT:

Enter side of a square:2

Enter length and breadth of rectangle:3 6

Enter radius of circle:3

Enter base and height of triangle:4 4

Sample Output

Area of square is4

Area of rectangle is 18

Area of circle is 28.26

Area of triangle is 8

4.Stacks program in c++ :

#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

printf("n1. PUSH ");

printf("n2. POP ");

printf("n3. TRAVERSE ");

printf("nEnter your choice ");

scanf("%d",&choice);

switch(choice)

case 1: push();

break;

case 2: printf("nThe deleted element is %d ",pop());

break;

case 3: traverse();

break;

default: printf("n You Entered Wrong Choice");

}
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)

printf("nThe Stack Is Full");

exit(0);

else

printf("Enter the element to be inserted ");

scanf("%d",&item);

Top= Top+1;

stack[Top] = item;

int pop()

int item;
if(Top == -1)

printf("The stack is Empty");

exit(0);

else

item = stack[Top];

Top = Top-1;

return(item);

void traverse()

int i;

if(Top == -1)

printf("The Stack is Empty");

exit(0);

else

for(i=Top;i>=0;i--)

printf("Traverse the element ");

printf("%dn",stack[i]);
}

OTutput:

5.C++ PROGRAM TO IMPLEMENT QUEUE USING ARRAY.

#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

cout<<"\nEnter the number to be inserted: ";

cin>>x;

rear++;

q[rear]=x;

cout<<"\nNumber pushed in the queue:"<<q[rear];

void queue::dque()

{
if(rear==0)

cout<<"\nQueue underflow!!\n";

else

if(front==rear)

front=0;

rear=0;

else

front++;

cout<<"\nDeleted element is:";

result=q[front];

cout<<result;

void queue::disp()

if(rear==0)

cout<<"\nQueue underflow!!\n";

else

cout<<"\nContents of queue is:";

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";

cout<<"\nEnter your choice:";

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

6.WAP TO IMPLEMENT STACK AS A LINKED LIST

#include<iostream>

using namespace std;

// Creating a NODE Structure

struct node
{

int data;

struct node *next;

};

// Creating a class STACK

class stack

struct node *top;

public:

stack() // constructure

top=NULL;

void push(); // to insert an element

void pop(); // to delete an element

void show(); // to show the stack

};

// PUSH Operation

void stack::push()

int value;

struct node *ptr;

cout<<"nPUSH Operationn";

cout<<"Enter a number to insert: ";

cin>>value;
ptr=new node;

ptr->data=value;

ptr->next=NULL;

if(top!=NULL)

ptr->next=top;

top=ptr;

cout<<"nNew item is inserted to the stack!!!";

// POP Operation

void stack::pop()

struct node *temp;

if(top==NULL)

cout<<"nThe stack is empty!!!";

temp=top;

top=top->next;

cout<<"nPOP Operation........nPoped value is "<<temp->data;

delete temp;

// Show stack

void stack::show()
{

struct node *ptr1=top;

cout<<"nThe stack isn";

while(ptr1!=NULL)

cout<<ptr1->data<<" ->";

ptr1=ptr1->next;

cout<<"NULLn";

// Main function

int main()

stack s;

int choice;

while(1)

cout<<"n-----------------------------------------------------------";

cout<<"nttSTACK USING LINKED LISTnn";

cout<<"1:PUSHn2:POPn3:DISPLAY STACKn4:EXIT";

cout<<"nEnter your choice(1-4): ";

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:

cout<<"Please enter correct choice(1-4)!!";

break;

return 0;

OUTPUT:

-----------------------------------------------------------

STACK USING LINKED LIST

1:PUSH
2:POP

3:DISPLAY STACK

4:EXIT

Enter your choice(1-4): 1

PUSH Operation

Enter a number to insert: 12

New item is inserted to the stack!!!

-----------------------------------------------------------

STACK USING LINKED LIST

1:PUSH

2:POP

3:DISPLAY STACK

4:EXIT

Enter your choice(1-4): 1

PUSH Operation

Enter a number to insert: 5

New item is inserted to the stack!!!

-----------------------------------------------------------

STACK USING LINKED LIST

1:PUSH
2:POP

3:DISPLAY STACK

4:EXIT

Enter your choice(1-4): 2

POP Operation........

Poped value is 5

-----------------------------------------------------------

STACK USING LINKED LIST

1:PUSH

2:POP

3:DISPLAY STACK

4:EXIT

Enter your choice(1-4): 3

The stack is

12 ->NULL

-----------------------------------------------------------

STACK USING LINKED LIST

1:PUSH

2:POP

3:DISPLAY STACK

4:EXIT
Enter your choice(1-4): 4

7.WAP TO IMPLEMENT QUEUE AS A LINKED LIST

* C++ Program to Implement Queue using Linked List

*/

#include<iostream>

#include<stdio.h>

#include<conio.h>

using namespace std;

struct node

int data;

node *next;

}*front = NULL,*rear = NULL,*p = NULL,*np = NULL;

void push(int x)

np = new node;

np->data = x;

np->next = NULL;

if(front == NULL)

front = rear = np;

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()

int n,c = 0,x;

cout<<"Enter the number of values to be pushed into queue\n";

cin>>n;

while (c < n)
{

cout<<"Enter the value to be entered into queue\n";

cin>>x;

push(x);

c++;

cout<<"\n\nRemoved Values\n\n";

while(true)

if (front != NULL)

cout<<remove()<<endl;

else

break;

getch();

Output

Enter the number of values to be pushed into queue

Enter the value to be entered into queue

Enter the value to be entered into queue

Enter the value to be entered into queue

Enter the value to be entered into queue


2

Enter the value to be entered into queue

Enter the value to be entered into queue

Removed Values

8.WAP TO CREATE A TEXT FILE AND DISPLAY NO. OF WORDS


ALPHABET.VOWELS CONSONANT AND NO. OF LOWER AND UPPER CASE
LETTERS.

#include <stdio.h>

int main()

char line[150];

int i, vowels, consonants, digits, spaces;

vowels = consonants = digits = spaces = 0;


printf("Enter a line of string: ");

scanf("%[^\n]", line);

for(i=0; line[i]!='\0'; ++i)

if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||

line[i]=='o' || line[i]=='u' || line[i]=='A' ||

line[i]=='E' || line[i]=='I' || line[i]=='O' ||

line[i]=='U')

++vowels;

else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))

++consonants;

else if(line[i]>='0' && line[i]<='9')

++digits;

else if (line[i]==' ')

++spaces;

}
printf("Vowels: %d",vowels);

printf("\nConsonants: %d",consonants);

printf("\nDigits: %d",digits);

printf("\nWhite spaces: %d", spaces);

return 0;

OUTPUT

Enter a line of string: adfslkj34 34lkj343 34lk

Vowels: 1

Consonants: 11

Digits: 9

White spaces: 2

9.WAP TO COPY ONE FILE TO ANOTHER


.#include<iostream.h>

#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( );

cout<<"Enter a file name to be copied ";

cin>> fname1;

cout<<"Enter new file name";

cin>>fname2;

infile.open(fname1);

if( infile.fail( ) )

cerr<< " No such a file Exit";

getch();

exit(1);

outfile.open( fname2);

if(outfile.fail( ))

cerr<<"Unable to create a file";

getch();

exit(1);

while( !infile.eof( ) )

ch = (char) infile.get( );

uch = toupper(ch);

outfile.put(uch);
}

infile.close( );

outfile.close( );

getch( );

OUTPUT:

Enter a file name to be copied.

C:\text1.txt

Enter new file name

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"

// Function to calculate the average word size in a text file.

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++;

if (( ch == ' ') || (ch == '.'))

i--;

sum = sum + i;

i = 0;

n++;

cout << " Average word size is "<<(sum/n);

void main()

calculate();

11. Store Information and Display it Using


Structure
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;

int main()
{
printf("Enter information:\n");

printf("Enter name: ");


scanf("%s", s.name);

printf("Enter roll number: ");


scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);

printf("Displaying Information:\n");

printf("Name: ");
puts(s.name);

printf("Roll number: %d\n",s.roll);

printf("Marks: %.1f\n", s.marks);

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>

using namespace std;

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;

printf("Enter the value of num\n");


scanf("%d", &num);
for (i = 1; i <= num; i++)
{
if (i % 2 == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of all odd numbers =
%d\n", odd_sum);
printf("Sum of all even numbers =
%d\n", even_sum);
}
$ cc pgm12.c
$ a.out
Enter the value of num
10
Sum of all odd numbers = 25
Sum of all even numbers = 30

$ 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];

printf("Enter the number of rows and


columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first
matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter the elements of second


matrix\n");

for (c = 0; c < m; c++)


for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);

printf("Sum of entered matrices:-\n");

for (c = 0; c < m; c++) {


for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c]
[d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}

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.

Prove that LHS = RHS

AABC + ABBC + ABCC+ABC


= AB+BC+AC
Take the LHS ABC + ABC + ABC +
ABC

= 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:

Front Office 100 KM


Work Office 20 KM
East Office501KM
West Office 50 KM
SouthOffice 50KM

Suggest network type(out of


LAN,MAN,WAN) for connecting each of the
following set of their offices:
Back Office and Work Office
Back Office and south Office

Which device you will suggest to be


produced by the company for connecting
all the computers with in each of their
offices out of the following devices?
Switch/Hub
Modem
Telephone

Which of the following communication


medium, you will suggest to be produced
by the company for connecting their
offices in New Delhi for very effective and
fast communication?
Telephone Cable
Optical Fibre
Ethernet Cable

S-ar putea să vă placă și