C programming
Web tutorials on C programming
Thursday, 27 October 2016
Monday, 10 October 2016
c program to insert a node in the beginning of the linked list
#include <stdio.h>
#include
<stdlib.h>
struct
Node{
int data;
struct Node *next;
};
struct
Node* head;
void
Insert(int x)
{
struct Node* temp = (struct Node*)malloc(sizeof(struct
Node));
temp->data=x;
temp->next = head;
head= temp;
}
void
Print()
{
struct Node * temp= head;
printf("List is: ");
while(temp != NULL)
{
printf("%d ", temp->data);
temp=
temp->next;
}
printf("\n");
}
int main()
{
head
=NULL;
int
n, i, x;
printf("How many Numbers?: \n");
scanf("%d", &n);
for(i= 0; i<n; i++)
{
printf("Enter the number: ");
scanf("%d",&x);
Insert(x);
Print();
}
return 0;
}
Wednesday, 5 October 2016
dynamic allocation of stack
#include
<stdio.h>
#include <stdlib.h>
struct node
{int data;
struct node *link;
}*top = NULL;
#define MAX 5
// function prototypes
void push();
void pop();
void empty();
void stack_full();
void stack_count();
void destroy();
void print_top();
void main()
{
int choice;
while (1)
{
printf("1. push an element \n");
printf("2. pop an element \n");
printf("3. check if stack is empty \n");
printf("4. check if stack is full \n");
printf("5. count/display elements present in stack \n");
printf("6. empty and destroy stack \n");
printf("7. Print top of the stack \n");
printf("8. exit \n");
printf("Enter your choice \n");
scanf("%d",&choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
empty();
break;
case 4:
stack_full();
break;
case 5:
stack_count();
break;
case 6:
destroy();
break;
case 7:
print_top();
break;
case 8:
exit(0);
default:
printf("wrong choice\n");
}
}
}
// to insert elements in stack
void push()
{
int val,count;
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
count = st_count();
if (count <= MAX - 1)
{
printf("\nEnter value which you want to
push into the stack :\n");
scanf("%d",&val);
temp->data = val;
temp->link = top;
top = temp;
}
else
printf("WARNING: STACK FULL\n");
}
// to delete elements from stack
void pop()
{
struct node *temp;
if (top = = NULL)
printf("**Stack is empty**\n");
else
{
temp = top;
printf("Value popped out is %d \n",temp->data);
top = top->link;
free(temp);
}
}
// to check if stack is empty
void empty()
{
if (top = = NULL)
printf("STACK IS EMPTY\n");
else
printf("elements are present, stack
is not empty \n");
}
// to check if stack is full
void stack_full()
{
int count;
count = st_count();
if (count = = MAX)
{
printf("stack is full\n");
}
else
printf("stack is not full \n");
}
// to count the number of elements
void stack_count()
{
int count = 0;
struct node *temp;
temp = top;
while (temp! = NULL)
{
printf(" %d\n",temp->data);
temp = temp->link;
count++;
}
printf("size of stack is %d \n",count);
}
int st_count()
{
int count = 0;
struct node *temp;
temp = top;
while (temp! = NULL)
{
temp = temp->link;
count++;
}
return count;
}
// to empty and destroy the stack
void destroy()
{
struct node *temp;
temp = top;
while (temp! = NULL)
{
pop();
temp = temp->link;
}
printf("stack destroyed\n");
}
// to print top element of stack
void print_top()
{
if (top == NULL)
printf("\n**Top is not available for an EMPTY stack**\n");
else
printf("\nTop of the stack is %d \n",top->data);
}
#include <stdlib.h>
struct node
{int data;
struct node *link;
}*top = NULL;
#define MAX 5
// function prototypes
void push();
void pop();
void empty();
void stack_full();
void stack_count();
void destroy();
void print_top();
void main()
{
int choice;
while (1)
{
printf("1. push an element \n");
printf("2. pop an element \n");
printf("3. check if stack is empty \n");
printf("4. check if stack is full \n");
printf("5. count/display elements present in stack \n");
printf("6. empty and destroy stack \n");
printf("7. Print top of the stack \n");
printf("8. exit \n");
printf("Enter your choice \n");
scanf("%d",&choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
empty();
break;
case 4:
stack_full();
break;
case 5:
stack_count();
break;
case 6:
destroy();
break;
case 7:
print_top();
break;
case 8:
exit(0);
default:
printf("wrong choice\n");
}
}
}
// to insert elements in stack
void push()
{
int val,count;
struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
count = st_count();
if (count <= MAX - 1)
{
printf("\nEnter value which you want to
push into the stack :\n");
scanf("%d",&val);
temp->data = val;
temp->link = top;
top = temp;
}
else
printf("WARNING: STACK FULL\n");
}
// to delete elements from stack
void pop()
{
struct node *temp;
if (top = = NULL)
printf("**Stack is empty**\n");
else
{
temp = top;
printf("Value popped out is %d \n",temp->data);
top = top->link;
free(temp);
}
}
// to check if stack is empty
void empty()
{
if (top = = NULL)
printf("STACK IS EMPTY\n");
else
printf("elements are present, stack
is not empty \n");
}
// to check if stack is full
void stack_full()
{
int count;
count = st_count();
if (count = = MAX)
{
printf("stack is full\n");
}
else
printf("stack is not full \n");
}
// to count the number of elements
void stack_count()
{
int count = 0;
struct node *temp;
temp = top;
while (temp! = NULL)
{
printf(" %d\n",temp->data);
temp = temp->link;
count++;
}
printf("size of stack is %d \n",count);
}
int st_count()
{
int count = 0;
struct node *temp;
temp = top;
while (temp! = NULL)
{
temp = temp->link;
count++;
}
return count;
}
// to empty and destroy the stack
void destroy()
{
struct node *temp;
temp = top;
while (temp! = NULL)
{
pop();
temp = temp->link;
}
printf("stack destroyed\n");
}
// to print top element of stack
void print_top()
{
if (top == NULL)
printf("\n**Top is not available for an EMPTY stack**\n");
else
printf("\nTop of the stack is %d \n",top->data);
}
Sunday, 28 August 2016
C programming code for binary search
#include <stdio.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements\n"); scanf("%d",&n); printf("Enter %d integers\n", n); for (c = 0; c < n; c++) scanf("%d",&array[c]); printf("Enter value to find\n"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2; while (first <= last) { if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d found at location %d.\n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; } if (first > last) printf("Not found! %d is not present in the list.\n", search); return 0; }
Friday, 19 August 2016
deleting an element from an array
#include<conio.h>
#include<stdio.h>
void main()
{
int a[20];
int n,val,i,pos;
clrscr();
printf("\nEnter the size of the array elements:\t");
scanf("%d",&n);
printf("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\nThe deleted element is =%d",val);
printf("\nThe array elements are:\n");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
getch();
}
#include<stdio.h>
void main()
{
int a[20];
int n,val,i,pos;
clrscr();
printf("\nEnter the size of the array elements:\t");
scanf("%d",&n);
printf("\nEnter the elements for the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the position of the element to be deleted:\t");
scanf("%d",&pos);
val=a[pos];
for(i=pos;i<n-1;i++)
{
a[i]=a[i+1];
}
n=n-1;
printf("\nThe deleted element is =%d",val);
printf("\nThe array elements are:\n");
for(i=0;i<n;i++){
printf("%d\t",a[i]);
}
getch();
}
Adding an elements from an array
#include<stdio.h>
#include<conio.h>
void main()
{ int pos,val,i,a[100],n;
clrscr();
printf("enter elements of array");
for(i=0;i<5;i++){
scanf("%d",&a[i]); }
printf("\nEnter the position for the new element:\t");
scanf("%d",&pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d",&val);
for(i=5-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
printf("\nThe array elements are:\n");
for(i=0;i<=5;i++){
printf("%d\t",a[i]);
}
getch();
}
#include<conio.h>
void main()
{ int pos,val,i,a[100],n;
clrscr();
printf("enter elements of array");
for(i=0;i<5;i++){
scanf("%d",&a[i]); }
printf("\nEnter the position for the new element:\t");
scanf("%d",&pos);
printf("\nEnter the element to be inserted :\t");
scanf("%d",&val);
for(i=5-1;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=val;
n=n+1;
printf("\nThe array elements are:\n");
for(i=0;i<=5;i++){
printf("%d\t",a[i]);
}
getch();
}
Wednesday, 17 August 2016
wap a code to show various operation on array like sum,max element ,min element,sum of positive and negative number and avg
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,sum=0,max=0,min=0,loc,n,neg_sum=0,pos_sum=0;
int a[100];
float avg=0;
clrscr();
printf("enter size of array\n");
scanf("%d",&n);
printf("enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
sum=sum+a[i];
}
for(i=0;i<n;i++){
if(a[i]<0)
{
neg_sum=neg_sum+a[i];
}
else if(a[i]>0){
pos_sum=pos_sum+a[i];
}
}
avg=sum/n;
max=a[0];
for(i=0;i<n;i++){
if(a[i]>max){
max=a[i];
loc=i+1;
}
}
min=a[0];
for(i=0;i<n;i++){
if(a[i]<min){
min=a[i];
}
}
printf("entered elements are\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);}
printf("sum=%d\n",sum);
printf("pos_sum=%d\n",pos_sum);
printf("neg_sum=%d\n",neg_sum);
printf("avg=%f\n",avg);
printf("max element is at location=%d and val=%d\n",loc,max);
printf("min element=%d\n",min);
getch();
}
#include<conio.h>
void main()
{
int i,j,sum=0,max=0,min=0,loc,n,neg_sum=0,pos_sum=0;
int a[100];
float avg=0;
clrscr();
printf("enter size of array\n");
scanf("%d",&n);
printf("enter %d elements\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++){
sum=sum+a[i];
}
for(i=0;i<n;i++){
if(a[i]<0)
{
neg_sum=neg_sum+a[i];
}
else if(a[i]>0){
pos_sum=pos_sum+a[i];
}
}
avg=sum/n;
max=a[0];
for(i=0;i<n;i++){
if(a[i]>max){
max=a[i];
loc=i+1;
}
}
min=a[0];
for(i=0;i<n;i++){
if(a[i]<min){
min=a[i];
}
}
printf("entered elements are\n");
for(i=0;i<n;i++){
printf("%d\n",a[i]);}
printf("sum=%d\n",sum);
printf("pos_sum=%d\n",pos_sum);
printf("neg_sum=%d\n",neg_sum);
printf("avg=%f\n",avg);
printf("max element is at location=%d and val=%d\n",loc,max);
printf("min element=%d\n",min);
getch();
}
C program to find the second largest element in an array
#include<stdio.h>
#include<conio.h>
int main(){
int a[50],size,i,j=0,big,secondbig;
printf("Enter the size of the array: ");
scanf("%d",&size);
printf("Enter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i]){
big=a[i];
j = i;
}
}
secondbig=a[size-j-1];
for(i=1;i<size;i++){
if(secondbig <a[i] && j != i)
secondbig =a[i];
}
printf("Second biggest: %d", secondbig);
return 0;
}
Sunday, 22 May 2016
Wap to draw a triangle
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "c:\\tc\\bgi");
line(300, 100, 200, 200);
line(300, 100, 400, 200);
line(200, 200, 400, 200);
getch();
closegraph();
}
Subscribe to:
Posts (Atom)