C language
//Hello World!//
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Hello World!");
getch();
}
#include<conio.h>
void main()
{
clrscr();
printf("Hello World!");
getch();
}
// Input/Output (scanf/printf) //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
#include<conio.h>
void main()
{
clrscr();
int num;
printf("Enter Number:");
printf("Enter Number:");
scanf("%d",&num);
printf("\n Entered Number: ",num);
getch();
}
}
//Condition//
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter Value:");
scanf("%d",&num);
num > 10 ? printf("Num is Big then 10") : printf("10 is big
then Num");
getch();
}
// Types of variables: //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=10;
char b='x';
float c=2.2;
printf("a=%d,b=%c,c=%f",a,b,c);
getch();
}
#include<conio.h>
void main()
{
clrscr();
int a=10;
char b='x';
float c=2.2;
printf("a=%d,b=%c,c=%f",a,b,c);
getch();
}
// Pointer //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a;
int *ptr;
a=10;
ptr=&a;
printf("%d",*ptr);
getch();
}
#include<conio.h>
void main()
{
clrscr();
int a;
int *ptr;
a=10;
ptr=&a;
printf("%d",*ptr);
getch();
}
// Calculation //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
#include<conio.h>
void main()
{
clrscr();
int a,b;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
printf("Addition=%d",a+b);
printf("Subtraction=%d",a-b);
printf("Multiplication=%d",a*b);
printf("Division=%d",a/b);
getch();
}
}
// If/Else //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
#include<conio.h>
void main()
{
clrscr();
int a,b;
a=5;
b=10;
if(a>b)
{
printf("A is bigger:%d",a);
}
printf("A is bigger:%d",a);
}
else
{
printf("B is bigger:%d",b);
}
getch();
}
}
// While Loop //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
#include<conio.h>
void main()
{
clrscr();
int i;
i=0;
while(i<=10)
{
printf("Num->%d\n",i);
printf("Num->%d\n",i);
i++;
}
getch();
}
}
// Do While loop //
#include<conio.h>
void main()
{
clrscr();
int i;
i=0;
do
{
printf("Hello World!\n");
i++;
}while(i<=10);
getch();
}
// For loop //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=0;i<=10;i++)
{
printf("i+i=%d",i+i);
printf("i+i=%d",i+i);
}
getch();
}
}
// Switch Case //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
#include<conio.h>
void main()
{
clrscr();
int num;
printf("Enter number:")l
scanf("%d",&num);
switch(num)
{
case 0:
{
printf("Number is Zero");
break;
}
case 1:
{
printf("Number is One");
break;
}
default
{
printf("Number is not zero nor one");
}
}
getch();
}
// Even/Odd //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num;
printf("Enter Number:");
scanf("%d",&num);
if(num % 2==0)
{
printf("Even");
}
else
{
printf("Odd");
}
getch();
}
#include<conio.h>
void main()
{
clrscr();
int num;
printf("Enter Number:");
scanf("%d",&num);
if(num % 2==0)
{
printf("Even");
}
else
{
printf("Odd");
}
getch();
}
// Table //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,i;
printf("Enter the value:");
scanf("%d",&a);
for(i=1;i<=10;i++);
{
printf("%d \n",a*i);
}
getch();
}
#include<conio.h>
void main()
{
clrscr();
int a,i;
printf("Enter the value:");
scanf("%d",&a);
for(i=1;i<=10;i++);
{
printf("%d \n",a*i);
}
getch();
}
// Array //
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int arr[5];
int i
for(i=0;i<=5;i++)
{
printf("Element:");
scanf("%d",&arr[i]);
}
printf("Elements:");
for(i=0;i<=5;i++)
{
printf("%d\t",&arr[i]);
}
getch();
}
#include<conio.h>
void main()
{
clrscr();
int arr[5];
int i
for(i=0;i<=5;i++)
{
printf("Element:");
scanf("%d",&arr[i]);
}
printf("Elements:");
for(i=0;i<=5;i++)
{
printf("%d\t",&arr[i]);
}
getch();
}
// Operations on Array //
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int x,arr[20],i,ch,n,ele;
printf("Enter Size:");
scanf("%d",&n);
printf("Enter elements:");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
while(1)
{
printf("Enter Choice: 1.Insert,2.Delete,3.Display,4.Exit:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Enter Position:");
scanf("%d",&x);
printf("Enter Element:");
scanf("%d",&ele);
n++;
if(x>n)
{
printf("Invalid Input");
}
else
{
for(i=n-1;i>=x-1;i--)
{
arr[i+1]=arr[i];
}
arr[x-1]=ele;
}
break;
}
case 2:
{
printf("Enter Position:");
scanf("%d",&x);
if(x>=n+1)
{
printf("Deletion is not Possible\n");
}
else
{
for(i=x;i<n;i++)
{
arr[i]=arr[i+1];
}
n--;
}
break;
}
case 3:
{
printf("Array Elements:");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
break;
}
case 4:
{
exit(0);
}
}
}
getch();
}
// Binary Search //
#include<stdio.h>
#include<conio.h>
void main()
{
int i,first,last,mid,n=7,search,arr[7];
clrscr();
printf("Enter Array:");
for(i=0;i<7;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter Element :");
scanf("%d",&search);
first=0;
last=n-1;
mid=(first/last)/2;
while(first<=last)
{
if(arr[mid]<search)
first=mid+1;
else if(arr[mid]==search){
printf("%d is found at location
%d.\n",search,mid+1);
break;
}
else
{
last=mid-1;
}
mid=(first+last)/2;
}
if(first>last)
printf("Not found! %d is not present in the
list.\n",search);
getch();
}
// Linear Search //
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[7],i,num,flag=0;
clrscr();
printf("Enter Array Elements:");
for(i=0;i<7;i++)
{
scanf("%d",&arr[i]);
}
printf("Enter Number:");
scanf("%d",&num);
for(i=0;i<7;i++)
{
if(num==arr[i])
{
printf("%d is at %d th Position in the array.",num,i+1);
flag=1;
break;
}
}
if(flag==0)
{
printf("Element is not found!!");
}
getch();
}
// Stack //
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
int q,top=-1,max=7,a[7],i,ch;
while(1)
{
printf("Enter choice: 1.Push,2.Pop,3.Show,4.Exit:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
if(top==max-1)
{
printf("Stack is full\n");
}
else
{
top++;
printf("Enter element");
scanf("%d",&a[top]);
}
break;
}
case 2:
{
if(top==-1)
{
printf("Stack is Empty\n");
}
else
{
top--;
}
break;
}
case 3:
{
if(top==-1)
{
printf("Stack is Empty\n");
}
else
{
printf("Stack elements=\n");
for(i=top;i>=0;--i)
{
printf("%d\n",a[i]);
}
}
break;
}
case 4:
{
printf("Exit");
exit(0);
}
default :
{
printf("Enter correct :");
}
}
}
getch();
}
// Queue //
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
int x,i,ch,front=-1,rare=-1,queue[7],max=7;
while(1)
{
printf("Enter Choice: 1.Push,2.Pop,3.Show,4.Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
if(rare==max-1)
{
printf("Queue is full.\n");
}
else
{
if(front==-1)
{
front=0;
}
rare=rare+1;
printf("Enter Element:");
scanf("%d",&x);
queue[rare]=x;
}
break;
}
case 2:
{
if(front==-1 || front>rare)
{
printf("Queue is Empty.\n");
}
else
{
printf("Element deleted: %d\n",queue[front]);
front=front+1;
}
break;
}
case 3:
{
if(front==-1 || front>rare)
{
printf("Queue is Empty.\n");
}
else
{
printf("Queue Elements:\n");
for(i=front;i<=rare;i++)
{
printf("%d\t",queue[i]);
}
printf("\n");
}
break;
}
case 4:
{
printf("Exit..");
exit(0);
}
default :
{
printf("Enter Correct Choice->");
break;
}
}
}
getch();
}
// Single Linked List //
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct node
{
int data;
struct node *next;
}*start=NULL,*q,*t;
int main()
{
int ch;
void insert_beg();
void insert_end();
void insert_pos();
void display();
void delete_beg();
void delete_end();
void delete_pos();
while(1)
{
printf("\n------1.Insert,2.Display,3.Delete,4.Exit------");
printf("Enter Choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("\n1.Insert at start");
printf("\n2.Insert at end ");
printf("\n3.Insert at specific pos.");
printf("\n4.Exit.");
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:insert_beg();
break;
case 2:insert_end();
break;
case 3:insert_pos();
break;
case 4:exit(0);
default: printf("Wrong
choice!!");
}
break;
}
case 2:
{
display();
break;
}
case 3:
{
printf("\n1.Delete from start");
printf("\n2.Delete from end ");
printf("\n3.Delete from specific
pos.");
printf("\n4.Exit.");
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:delete_beg();
break;
case 2:delete_end();
break;
case 3:delete_pos();
break;
case 4:exit(0);
default: printf("Wrong
choice!!");
}
break;
}
case 4:
exit (0);
default: printf("Wrong choice!!");
}
}
return 0;
}
void insert_beg()
{
int num;
t=(struct node*)malloc(sixeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
if(start==NULL)
{
t->next=NULL;
start=t;
}
else
{
t->next=start;
start=t;
}
}
void insert_end()
{
int num;
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
t->data=num;
t->next=NULL;
if(start==NULL)
{
start=t;
}
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
q->next=t;
}
}
int insert_pos()
{
int pos,i,num;
if(start==NULL)
{
printf("List is empty!!");
return 0;
}
t=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
printf("Enter position to insert:");
scanf("%d",&pos);
t->data=num;
q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t->next=q->next;
q->next=t;
return 0;
}
void display()
{
if(start==NULL)
{
printf("List is empty!!");
}
else
{
q=start;
printf("The linked list is:\n");
while(q!=NULL)
{
printf("%d->",q->data);
q=q->next;
}
}
}
void delete_beg()
{
if(start==NULL)
{
printf("The list is empty!!");
}
else
{
q=start;
start=start->next;
printf("Deleted element is %d",q->data);
free(q);
}
}
void delete_end()
{
if(start==NULL)
{
printf("The list is empt!!");
}
else
{
q=start;
while(q->next->next!=NULL)
{
q=q->next;
}
t=q->next;
q->next=NULL;
printf("Deleted element is %d",t->data);
free(t);
}
}
int delete_pos()
{
int pos,i;
if(start==NULL)
{
printf("The list is empty!!");
return 0;
}
printf("Enter position to delete:");
scanf("%d",&pos);
q=start;
for(i=1;i<pos-1;i++)
{
if(q->next==NULL)
{
printf("There are less elements!!");
return 0;
}
q=q->next;
}
t=q->next;
q->next=t->next;
printf("Deleted element is %d",t->data);
free(t);
return 0;
}
Comments
Post a Comment