Internal Ii Answer
Internal Ii Answer
1. Write a C++ program to remove all the characters from the given string that are not alphabet.
INPUT
pro$#&gra7m
OUTPUT
Program
PROGRAM
#include<stdio.h>
int main()
char s[100];
scanf("%s",&s);
for(int i=0;i<strlen(s);i++){
res[x]=s[i]; x++;
}
}
printf("%s",res);
2. The principal of the college wants to know who got the highest marks this semester. He has to
appreciate the students and also wants to offer a free course to that student. The topmost
company HR person will conduct the course. He or she can improve technical skills through this
course. The duration of the free course is about 3months. In this way, he wants to bring all other
students to get the highest marks. The class teachers of each and every class discussed and
submit the student's detail to the principal in the paper. Unfortunately, he missed the paper so he
needs to automate the process. Can you help him to find the student's details by developing the
code in the OOPS-c++ program.
INPUT
Kohli
24
480
OUTPUT
Name:
Age:
Gender:
Total Marks :
Grade:
Name: kohli
Age: 24
Gender: M
Percentage: 96
Grade: A
PROGRAM
#include <iostream>
class Student
public:
string name,gender,grade;
int age,tmarks;
cout<<"Name:"<<endl;
cout<<"Age:"<<endl;
cout<<"Gender:"<<endl;
cin>>n>>x>>g;
cout<<endl<<"Enter student's result information:"<<endl;
cout<<"Total Marks:"<<endl;
cout<<"Grade:"<<endl;
cin>>y>>r;
name=n;
age=x;
gender=g;
tmarks= y;
grade = r;
};
public:
void display()
int per=tmarks/5;
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Gender: "<<gender<<endl;
cout<<"Percentage: "<<per<<endl;
cout<<"Grade: "<<grade<<endl;
}
};
int main()
Marks m;
string n,g,r;
int x,y;
m.set(n,x,g,y,r);
m.display();
return 0;
-1
OUPUT
4321
#include <iostream>
struct node
int data;
}*head=0;
void push(int n)
newnode->data=n;
newnode->next=0;
if(head==0)
head=newnode;
temp=newnode;
else
{
temp->next=newnode;
temp=newnode;
void print()
temp=head;
while(temp!=0)
cout<<temp->data<<" ";
temp=temp->next;
}}
int main()
int n;
while(1)
cin>>n;
if(n!=-1)
push(n);
else
break;
print();
4. Consider implementing a stack using a Linked List. Create a structure struct node { int data ;
struct node * link ; } ; Write a program to implement push and pop operation on the stack and to
display the contents of the stack. Print the message “Stack is empty” in the pop function and
return the value -1000 when an attempt is made to pop data from an empty stack. Refer to the
function specifications for further details. FUNCTION DEFINITIONS: void push (struct node
**q, int num) void display (struct node * q) int pop (struct node ** q) INPUT & OUTPUT
FORMAT: Refer the sample input and output for formatting specifications. Note that the
statement "The contents of the stack are" is in the main function. In the display function, if the
stack is empty, print “ {}”. [All text in bold corresponds to the input and the rest corresponds to
output].
Input (stdin)
1
3
1
5
1
6
3
2
3
2
2
2
3
6
OUTPUT
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
Enter the element to be pushed
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
Enter the element to be pushed
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
Enter the element to be pushed
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The contents of the stack are 6 5 3
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The popped element is 6
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The contents of the stack are 5 3
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The popped element is 5
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The popped element is 3
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
Stack is empty
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
The contents of the stack are {}
Choice 1 : Push
Choice 2 : Pop
Choice 3 : Display
Any other choice : Exit
Enter your choice
PROGRAM
#include<iostream>
#include<stdlib.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
};
Node *root = NULL;
void append(int d)
{
Node* newnode = new Node();
newnode->data = d;
newnode->next = root;
root = newnode;
//return;
}
void display()
{
Node *temp = root;
if(temp==NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
}
cout<<endl;
}
int delet()
{
if(root == NULL)
{
cout<<"Stack is empty\n";
return 0;
}
cout<<"The popped element is "<<root->data<<endl;
root = root->next;
return 1;
}
int main()
{
int n,a;
while(1)
{
cout<<"Choice 1 : Push\nChoice 2 : Pop\nChoice 3 : Display\nAny other choice : Exit\n";
cout<<"Enter your choice\n";
cin>>n;
if(n==1)
{
cout<<"Enter the element to be pushed\n";
cin>>a;
append(a);
}
else if(n==2)
delet();
else if(n==3)
display();
else
break;
}
}
5.Write a C program to add the elements into a queue and print the elements in the queue
(Implement this logic using the queue using linked list).
INPUT & OUTPUT FORMAT: Input consists of integers. -1 indicates the end. Refer to sample
input & output for output formatting specifications.
INPUT
a.1
b.2
c.3
d.-1
OUTPUT
PROGRAM
#include<iostream>
#include<stdlib.h>
class Node{
public:
int data;
Node *next;
};
int main()
Node *head=NULL;
int data;
do
{
cin>>data;
if(data>0)
append(&head,data);
}while(data>0);
display(head);
Node *temp,*newnode;
temp=*headadd;
newnode=(Node*)malloc(sizeof(Node));
newnode->data=data;
newnode->next=NULL;
if(*headadd==NULL)
*headadd=newnode;
else{
while(temp->next!=NULL){
temp=temp->next;
temp->next=newnode;
}
void display(Node *head)
while(head!=NULL)
cout<<head->data<<" ";
head = head->next;
6. Implement a C program to construct a Binary Search Tree and also display the elements in the
tree using Inorder, Preorder, and Postorder traversals. Binary Search tree is a binary tree in
which each internal node x stores an element such that the elements stored in the left subtree of x
are less than or equal to x and elements stored in the right subtree of x are greater than or equal
to x. This is called binary-search-tree property. INPUT & OUTPUT FORMAT: Refer the sample
input and output for formatting specifications. [All text in bold corresponds to the output and the
rest corresponds to input].
INPUT
yes
yes
yes
no
OUTPUT
PROGRAM
#include<iostream>
#include<string.h>
#include<stdlib.h>
struct btree
int data;
};
void create(int n)
{
temp = (struct btree*)malloc(sizeof(struct btree));
temp->data = n;
insert(t->right);
t->right = temp;
insert(t->left);
t->left = temp;
}
void inorder(struct btree *t)
if(root == NULL)
cout<<"No element";
if(t->left!=NULL)
inorder(t->left);
cout<<t->data<<" ";
if(t->right != NULL)
inorder(t->right);
if(root == NULL)
cout<<"No element";
cout<<t->data<<" ";
if(t->left!=NULL)
preorder(t->left);
if(t->right != NULL)
preorder(t->right);
if(root == NULL)
cout<<"No element";
return;
if(t->left!=NULL)
postorder(t->left);
if(t->right != NULL)
postorder(t->right);
int main()
{
int n,input;
char s[10];
root = 0;
while(1)
cin>>input;
create(input);
if(root == NULL)
root = temp;
else
insert(root);
cin>>s;
if(strcmp(s,"no")==0)
break;
inorder(root);
preorder(root);
7. . Write a OOPS-C++ program to fine a given no is prime or not using class and object. If the
number is prime print"Prime" else, print "Not Prime" .
INPUT
a. 13
OUTPUT
Prime
PROGRAM
#include<iostream>
using namespace std;
class sum
{
private: int n,i,flag=0;
public:
void add()
{
cin>>n;
for (i = 2; i <= n / 2; ++i) {
Sample Input:
architect
tailor
referee
electrician
nurse
blacksmith
####
Sample Output:
architect
tailor
referee
electrician
nurse
#include<stdio.h>
#include<string.h>
int main()
char s[50][50];
int i=0, n = 0;
{
gets(s[i]);
n++;
if(!strcmp(s[i],"####"))
break;
printf("%s\n",s[0]);
for(i=0;i<n;i++)
if(s[i][strlen(s[i])-1]==s[i+1][0])
printf("%s\n",s[i+1]);
return 0;
}
9. Write a C program to sort the given string. INPUT & OUTPUT FORMAT: Input consists of 1
string. Output print the ascending order of given string.
INPUT:
God
OUTPUT:
PROGRAM
#include<stdio.h>
int main()
char s[1000];
scanf("%s",&s);
int i=0,j=0,n=strlen(s);
char t;
t= s[i];
s[i] = s[j];
s[j] = t;
}
}
10. Write a C program to perform the binary search on an array using recursion.
SAMPLE INPUT:
SAMPLE OUTPUT:
PROGRAM
#include<stdio.h>
#include<math.h>
int recSearch ( int a[], int l, int r, int index)
{
if ( r < l)
return -1;
if ( a[l] == index)
return 1;
if (a[r] == index)
return 1;
return recSearch ( a, l+1, r-1, index);
}
int main()
{
int a[100];
int n;
scanf("%d",&n);
for ( int i = 0; i < n; i++ )
{
scanf("%d",&a[i]);
}
int x;
scanf("%d",&x);
int index = ( recSearch ( a , 0 , n-1 , x));
if ( index != -1)
printf("%d",x);
else
printf("The number is not present in the list");
}