Programs Questions and Answers
Input : NA
Output : NA
#include <iostream>
using namespace std;
/* function declaration */
void swap(int &x, int &y);
int main () {
/* local variable declaration: */
int a = 100;
int b = 200;
cout << "Before swap, value of a :" << a << endl;
cout << "Before swap, value of b :" << b << endl;
/* calling a function to swap the values using variable reference.*/
swap(a, b);
cout << "After swap, value of a :" << a << endl;
cout << "After swap, value of b :" << b << endl;
return 0;
}
Tags:Wipro Syntel Inc. Sapient
Input : NA
Output : NA
#include <stdio.h>
#include <stdlib.h>
# include <iostream>
using namespace std;
struct node
{
struct node *prev;
int n;
struct node *next;
}*h,*temp,*temp1,*temp2,*temp4;
void insert1();
void insert2();
void insert3();
void del();
int count = 0;
void main()
{
int ch;
h = NULL;
temp = temp1 = NULL;
cout<<"\n 1 - Insert at beginning";
cout<<"\n 2 - Insert at end";
cout<<"\n 3 - Insert at position i";
cout<<"\n 4 - Delete at i";
cout<<"\n 5 - Exit";
while (1)
{
cout<<"\n Enter choice : ";
cin>>ch;
switch (ch)
{
case 1:
insert1();
break;
case 2:
insert2();
break;
case 3:
insert3();
break;
case 4:
del();
break;
case 5:
exit(0);
default:
printf("\n Wrong choice menu");
}
}
}
/* TO create an empty node */
void create()
{
int data;
temp =(struct node *)malloc(1*sizeof(struct node));
temp->prev = NULL;
temp->next = NULL;
cout<<"\n Enter value to node : ";
cin>>data;
temp->n = data;
count++;
}
/* TO insert at beginning */
void insert1()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp->next = h;
h->prev = temp;
h = temp;
}
}
/* To insert at end */
void insert2()
{
if (h == NULL)
{
create();
h = temp;
temp1 = h;
}
else
{
create();
temp1->next = temp;
temp->prev = temp1;
temp1 = temp;
}
}
/* To insert at any position */
void insert3()
{
int pos, i = 2;
cout<<"\n Enter position to be inserted : ";
cin>>pos;
temp2 = h;
if ((pos < 1) || (pos >= count + 1))
{
cout<<"\n Position out of range to insert";
return;
}
if ((h == NULL) && (pos != 1))
{
cout<<"\n Empty list cannot insert other than 1st position";
return;
}
if ((h == NULL) && (pos == 1))
{
create();
h = temp;
temp1 = h;
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
create();
temp->prev = temp2;
temp->next = temp2->next;
temp2->next->prev = temp;
temp2->next = temp;
}
}
/* To delete an element */
void del()
{
int i = 1, pos;
cout<<"\n Enter position to be deleted : ";
cin>>pos;
temp2 = h;
if ((pos < 1) || (pos >= count + 1))
{
cout<<"\n Error : Position out of range to delete";
return;
}
if (h == NULL)
{
cout<<"\n Error : Empty list no elements to delete";
return;
}
else
{
while (i < pos)
{
temp2 = temp2->next;
i++;
}
if (i == 1)
{
if (temp2->next == NULL)
{
cout<<"Node deleted from list";
free(temp2);
temp2 = h = NULL;
return;
}
}
if (temp2->next == NULL)
{
temp2->prev->next = NULL;
free(temp2);
cout<<"Node deleted from list";
return;
}
temp2->next->prev = temp2->prev;
if (i != 1)
temp2->prev->next = temp2->next; /* Might not need this statement if i == 1 check */
if (i == 1)
h = temp2->next;
cout<<"\n Node deleted";
free(temp2);
}
count--;
}
Tags:Wipro
Input : NA
Output : NA
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int row, c, n, temp;
cout<<"Enter the number of rows in pyramid of stars you wish to see ";
cin>>n;
temp = n;
for ( row = 1 ; row <= n ; row++ )
{
for ( c = 1 ; c <= row; c++ )
cout<<c;
cout<<"\n";
}
return 0;
}
Tags:Wipro
Input : NA
Output : NA
#include <iostream>
using namespace std;
int main()
{
string str = "C++ Programming";
/* you can also use str.length() */
cout << "String Length = " << str.size();
return 0;
}
Tags:No Tags on this question yet!
Input : NA
Output : NA
#include<stdio.h>
#include<iostream>
using namespace std;
#define bool int
/* Function to check if x is power of 2*/
bool isPowerOfTwo (int x)
{
/* First x in the below expression is for the case when x is 0 */
return x && (!(x&(x-1)));
}
/*Driver program to test above function*/
int main()
{
isPowerOfTwo(31)?cout<<"Yes\n": cout<<"No\n";
isPowerOfTwo(16)? cout<<"Yes\n": cout<<"No\n";
return 0;
}
Tags:PEOL Global Edge
Input : NA
Output : NA
#include <stdio.h>
#include <iostream>
using namespace std;
void main()
{
int num1, num2, gcd, lcm, remainder, numerator, denominator;
cout<<"Enter two numbers\n";
cin>> num1>>num2;
if (num1 > num2)
{
numerator = num1;
denominator = num2;
}
else
{
numerator = num2;
denominator = num1;
}
remainder = numerator % denominator;
while (remainder != 0)
{
numerator = denominator;
denominator = remainder;
remainder = numerator % denominator;
}
gcd = denominator;
lcm = num1 * num2 / gcd;
cout<<"GCD of "<< num1<<" and "<< num2<<" is "<< gcd;
cout<<"LCM of "<< num1<<" and "<< num2<<" is "<< lcm;
}
Tags:PEOL
Input : NA
Output : NA
#include <stdio.h>
#include <iostream>
using namespace std;
void main()
{
int array1[50], array2[50], array3[100], m, n, i, j, k = 0;
cout<<"\n Enter size of array Array 1: ";
cin>>m;
cout<<"\n Enter sorted elements of array 1: \n";
for (i = 0; i < m; i++)
{
cin>>array1[i];
}
cout<<"\n Enter size of array 2: ";
cin>>n;
cout<<"\n Enter sorted elements of array 2: \n";
for (i = 0; i < n; i++)
{
cin>>array2[i];
}
i = 0;
j = 0;
while (i < m && j < n)
{
if (array1[i] < array2[j])
{
array3[k] = array1[i];
i++;
}
else
{
array3[k] = array2[j];
j++;
}
k++;
}
if (i >= m)
{
while (j < n)
{
array3[k] = array2[j];
j++;
k++;
}
}
if (j >= n)
{
while (i < m)
{
array3[k] = array1[i];
i++;
k++;
}
}
cout<<"\n After merging: \n";
for (i = 0; i < m + n; i++)
{
cout<<"\n"<<array3[i];
}
}
Tags:Juniper Network
Input : {{}}{)([][]
Output :
<!DOCTYPE html>
<html>
<body>
<?php
function Balance()
{
$arr = array(
"[",
"}",
"]",
"(",
"{",
"(",
")"
);
$arrlength = count($arr);
$count1 = 0;
$count2 = 0;
$count3 = 0;
for ($x = 0; $x < $arrlength; $x++) {
if ($arr[$x] == "(")
$count1++;
if ($arr[$x] == ")")
$count1--;
if ($arr[$x] == "{")
$count2++;
if ($arr[$x] == "}")
$count2--;
if ($arr[$x] == "[")
$count3++;
if ($arr[$x] == "]")
$count3--;
}
if ($count1 == 0)
echo "brackets () are balanced<br/>";
else
echo "brackets () are not balanced<br/>";
if ($count2 == 0)
echo "brackets {} are balanced<br/>";
else
echo "brackets{} are not balanced<br/>";
if ($count3 == 0)
echo "brackets []are balanced<br/>";
else
echo "brackets [] are not balanced<br/>";
}
Balance(); //function calling
?>
</body>
</html>
Tags:Juniper Network
Input : NA
Output : NA
No Discussion on this question yet!
Tags:Juniper Network
Input : This chain applies to packets received via a network interface. Matching String: packets.
Output : This chain applies to Packets received via a network interface.
No Discussion on this question yet!
Tags:Juniper Network