// C++programto 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)
{
//basecaseif(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 0to the first half
for(j =0; j < i ; j++)
arr[j] ="0"+ arr[j];// append 1to 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 programto test above functionint main()
{
generateGrayarr(3);return0;
}
#include <stdio.h>
#include <stdlib.h>//Represent a node of singly linked list
struct node{
intdata;
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(intdata) {
//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 pointto new node
head = newNode;
tail = newNode;
}
else {
//newNode will be added after tail such that tail's next will pointto 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 pointto 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();return0;
}
#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;elseif(min> num)min= num;
}
printf ("The smallest and largest of given four numbers are %d and %d respectively.
",min,max);return0;
}