// C++ program to generate n-bit Gray codes
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// This function generates all n bit Gray codes and prints the
// generated codes
void generateGrayarr(int n)
{
// base case
if (n <= 0)
return;
// 'arr' will store all generated codes
vector<string> arr;
// start with one-bit pattern
arr.push_back("0");
arr.push_back("1");
// Every iteration of this loop generates 2*i codes from previously
// generated i codes.
int i, j;
for (i = 2; i < (1<<n); i = i<<1)
{
// Enter the previously generated codes again in arr[] in reverse
// order. Nor arr[] has double number of codes.
for (j = i-1 ; j >= 0 ; j--)
arr.push_back(arr[j]);
// append 0 to the first half
for (j = 0 ; j < i ; j++)
arr[j] = "0" + arr[j];
// append 1 to the second half
for (j = i ; j < 2*i ; j++)
arr[j] = "1" + arr[j];
}
// print contents of arr[]
for (i = 0 ; i < arr.size() ; i++ )
cout << arr[i] << endl;
}
// Driver program to test above function
int main()
{
generateGrayarr(3);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i, j, n, sum = 0, tsum;
cout << "
Find the sum of the series (1) + (1+2) + (1+2+3) + (1+2+3+4) + ... + (1+2+3+4+...+n):
";
cout << "------------------------------------------------------------------------------------------
";
cout << " Input the value for nth term: ";
cin >> n;
for (i = 1; i <= n; i++)
{
tsum = 0;
for (j = 1; j <= i; j++)
{
sum += j;
tsum += j;
cout << j;
if (j < i)
{
cout << "+";
}
}
cout << " = " << tsum << endl;
}
cout << " The sum of the above series is: " << sum << endl;
}
#include <stdio.h>
#include <stdlib.h>
//Represent a node of singly linked list
struct node{
int data;
struct node *next;
};
//Represent the head and tail of the singly linked list
struct node *head, *tail = NULL;
//addNode() will add a new node to the list
void addNode(int data) {
//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
newNode->next = NULL;
//Checks if the list is empty
if(head == NULL) {
//If list is empty, both head and tail will point to new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode will become new tail of the list
tail = newNode;
}
}
//display() will display all the nodes present in the list
void display() {
//Node current will point to head
struct node *current = head;
if(head == NULL) {
printf("List is empty
");
return;
}
printf("Nodes of singly linked list:
");
while(current != NULL) {
//Prints each node by incrementing pointer
printf("%d ", current->data);
current = current->next;
}
printf("
");
}
int main()
{
//Add nodes to the list
addNode(1);
addNode(2);
addNode(3);
addNode(4);
//Displays the nodes present in the list
display();
return 0;
}
#include <stdio.h>
int main()
{
int num, max, min;
printf ("Enter four numbers: ");
scanf ("%d", &num);
max = min = num;
for (int i = 0; i < 3; i++)
{
scanf ("%d", &num);
if (max < num)
max = num;
else if (min > num)
min = num;
}
printf ("The smallest and largest of given four numbers are %d and %d respectively.
", min, max);
return 0;
}