Posts

Showing posts from May, 2014

C program for insert an element at desired position in an array.

Source code: #include<stdio.h> int main() { int a[50],size,num,i,pos,temp; printf("\nEnter size of the array: "); scanf("%d",&size); printf("\nEnter %d elements in to the array: ",size);                  for(i=0;iscanf("%d",&a[i]);               printf("\nEnter position and number to insert: "); scanf("%d %d",&pos,&num); i=0; while(i!=pos-1) i++; temp=size++; while(i) { a[temp]=a[temp-1]; temp--; } a[i]=num; for(i=0;iprintf(" %d",a[i]); getch(); return 0; }

C program to find largest and smallest number in an array

source code: #include<stdio.h> int main() {   int a[50],size,i,big,small;   printf("\nEnter the size of the array: ");   scanf("%d",&size);   printf("\nEnter %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];   }   printf("Largest element: %d",big);   small=a[0];   for(i=1;i<size;i++) {       if(small>a[i])            small=a[i];   }   printf("Smallest element: %d",small);   getch();   return 0; }

C program to pass the single dimensional array to the function.

source code: #include <stdio.h> #define N 5 void fstore1D(int a[], int a_size); void fretrieve1D(int a[], int a_size); void fedit1D(int a[], int a_size); int main() { int a[N]; printf("Input data into the matrix:\n");              fstore1D(a, N); fretrieve1D(a, N); fedit1D(a, N); fretrieve1D(a, N); getch(); return 0; } void fstore1D(int a[], int n) { int i; for ( i = 0; i < n; ++i ) scanf("%d", &a[i]); } void fretrieve1D(int a[], int n){ int i; for ( i = 0; i < n; ++i ) printf("%6d ", a[i]); printf("\n"); } void fedit1D(int a[], int n) { int i, q; for ( i = 0; i < n; ++i ) { printf("Prev. data: %d\nEnter 1 to edit 0 to skip.", a[i]); scanf("%d", &q);

C Program to pass the multidimensional array to the function.

source code: #include <stdio.h> #define M 3 #define N 5 void fstore2D(int a[][N]); void fretrieve2D(int a[][N]); int main() {   int a[M][N];   printf("Input data in matrix (%d X %d)\n", M, N);   fstore2D(a);   fretrieve2D(a);   getch();   return 0; } void fstore2D(int a[][N]) {     int i, j;     for (i = 0; i < M; ++i) {     for (j = 0; j < N; ++j)          scanf("%d", &a[i][j]);     } } void fretrieve2D(int a[][N]) {    int i, j;    for ( i = 0; i < M; ++i ) {        for ( j = 0; j < N; ++j)             printf("%6d ", a[i][j]);        printf("\n");    } }

C program for bubble sort.

source code: #include<stdio.h> int main() {  int s,temp,i,j,a[20]; printf("Enter total numbers of elements: ");  scanf("%d",&s); printf("Enter %d elements: ",s);   for(i=0;i<s;i++) scanf("%d",&a[i]); //Bubble sorting algorithm   for(i=s-2;i>=0;i--) {       for(j=0;j<=i;j++) {            if(a[j]>a[j+1]) {                temp=a[j];               a[j]=a[j+1];               a[j+1]=temp;            }       }   }   printf("After sorting: ");   for(i=0;i<s;i++)       printf(" %d",a[i]);   getch();   return 0; }

C program for insertion sort

source code: #include<stdio.h> int main() {  int i,j,s,temp,a[20]; printf("Enter total elements: "); scanf("%d",&s);   printf("Enter %d elements: ",s);   for(i=0;i<s;i++)       scanf("%d",&a[i]);   for(i=1;i<s;i++) {       temp=a[i];       j=i-1;       while((temp<a[j])&&(j>=0)) {       a[j+1]=a[j];           j=j-1;       }       a[j+1]=temp;   }   printf("After sorting: ");   for(i=0;i<s;i++)       printf(" %d",a[i]);   getch();   return 0; }

C program for selection sort

source code: #include<stdio.h> int main() {  int s,i,j,temp,a[20];   printf("Enter total elements: ");   scanf("%d",&s);  printf("Enter %d elements: ",s);   for(i=0;i<s;i++)  scanf("%d",&a[i]);  for(i=0;i<s;i++) {       for(j=i+1;j<s;j++) {            if(a[i]>a[j]) {                temp=a[i];               a[i]=a[j];               a[j]=temp;            }       }   }   printf("After sorting is: ");   for(i=0;i<s;i++)       printf(" %d",a[i]);   getch();   return 0; }

C program for quick sort.

source code: #include<stdio.h> void quicksort(int [10],int,int); int main() {   int x[20],size,i;   printf("Enter size of the array: ");   scanf("%d",&size);   printf("Enter %d elements: ",size);   for(i=0;i<size;i++)     scanf("%d",&x[i]);   quicksort(x,0,size-1);   printf("Sorted elements: ");   for(i=0;i<size;i++)     printf(" %d",x[i]);   getch();   return 0; }

C program for merge sort.

source code: #include<stdio.h> #define MAX 50 void mergeSort(int arr[],int low,int mid,int high); void partition(int arr[],int low,int high); int main() {         int merge[MAX],i,n;     printf("Enter the total number of elements: ");     scanf("%d",&n);     printf("Enter the elements which to be sort: ");     for(i=0;i<n;i++)   {         scanf("%d",&merge[i]);     }     partition(merge,0,n-1);     printf("After merge sorting elements are: ");     for(i=0;i<n;i++) {          printf("%d ",merge[i]);     }    getch();    return 0; } void partition(int arr[],int low,int high) {     int mid;     if(low<high) {          mid=(low+high)/2;          partition(arr,low,mid);          partition(arr,mid+1,high);          mergeSort(arr,low,mid,high);     } } void mergeSort(int arr[],int low,int mid,int high) {     int i,m,k,l,temp[MAX]

C program to find the size of int without using sizeof operator.

source code: #include<stdio.h> int main() {   int *ptr = 0;   ptr++;   printf("Size of int data type:  %d",ptr);  getch();   return 0; }

C Program to find the size of structure without using sizeof operator.

source code: #include<stdio.h> struct student {     int roll;     char name[100];     float marks; }; int main() {   struct student *ptr = 0;   ptr++;   printf("Size of the structure student:  %d",ptr);   getch();   return 0; }

C program to find the size of union without using sizeof operator.

source code: #include<stdio.h> union student {     int roll;     char name[100];     float marks; }; int main() {   union student *ptr = 0;   ptr++;   printf("Size of the union student:  %d",ptr); .  getch();   return 0; }

C program for linear search

source code: #include<stdio.h> int main() {     int a[10],i,n,m,c=0;     printf("Enter the size of an array: ");     scanf("%d",&n);     printf("Enter the elements of the array: ");     for(i=0;i<=n-1;i++){      scanf("%d",&a[i]);     }     printf("Enter the number to be search: ");     scanf("%d",&m);     for(i=0;i<=n-1;i++){          if(a[i]==m){              c=1;              break;          }     }     if(c==0)          printf("The number is not in the list");     else          printf("The number is found");     getch();     return 0; }

C program for binary search

source code: #include<stdio.h> int main() {     int a[10],i,n,m,c=0,l,u,mid;     printf("Enter the size of an array: ");     scanf("%d",&n);     printf("Enter the elements in ascending order: ");     for(i=0;i<n;i++){     scanf("%d",&a[i]);     }     printf("Enter the number to be search: ");     scanf("%d",&m);     l=0,u=n-1;     while(l<=u) {          mid=(l+u)/2;          if(m==a[mid]) {              c=1;              break;          }          else if(m<a[mid]) {              u=mid-1;          }          else              l=mid+1;     }    if(c==0)          printf("The number is not found.");     else          printf("The number is found.");     getch();     return 0; }

C program to find the size of double without using sizeof operator

source code: #include<stdio.h> int main() {   double *ptr = 0;    ptr++;   printf("Size of double data type:  %d",ptr);   getch();  return 0; }

Prime number program in c using recursion

source code: #include<stdio.h> int isPrime(int,int); int main() {     int num,prime;     printf("Enter a positive number: ");     scanf("%d",&num);     prime = isPrime(num,num/2);    if(prime==1)         printf("%d is a prime number",num);    else       printf("%d is not a prime number",num);    getch();    return 0; } int isPrime(int num,int i) {     if(i==1){         return 1;     }else{        if(num%i==0)          return 0;        else          isPrime(num,i-1);     } }

Factorial of a number using recursion

source code:  #include<stdio.h>          int fact(int);         int main()    {         int num,f;         printf("\nEnter a number: ");        scanf("%d",&num);        f=fact(num);        printf("\nFactorial of %d is: %d",num,f);       getch();       return 0;   }      int fact(int n) {        if(n==1)            return 1;      else        return(n*fact(n-1));  }

Print one to hundred without using loop

source code: #include<stdio.h>             int main()     {     int num = 1;     print(num);     getch();     return 0; } int print(num) {     if(num<=100){          printf("%d ",num);          print(num+1);     } }

Subtraction without '-' operator

source code: #include<stdio.h>    int main()   {     int a,b;     int sum;     printf("Enter any two integers:/n ");     scanf("%d%d",&a,&b);        sum = a + ~b + 1;     printf("Difference of two integers: %d",sum);        getch();        return 0;   }

Reverse of a number

source code: #include<stdio.h> int main() {     int num,r,reverse=0;     printf("Enter any number: ");     scanf("%d",&num);     while(num) {          r=num%10;          reverse=reverse*10+r;          num=num/10;     }     printf("Reversed of number: %d",reverse);     getch();     return 0; }

Snake And Ladder

source code: #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> #include<time.h> void draw_line(int n,char ch); void board(); void gamescore(char name1[],char name2[],int p1, int p2); void play_dice(int &score); void main() {   int player1=0,player2=0,lastposition;   char player1name[80],player2name[80];   clrscr();   randomize();   draw_line(50,'=');   cout<<"\n\n\n\n\t\tSNAKE LADDER GAME\n\n\n\n";   draw_line(50,'=');   cout<<"\n\n\nEnter Name of player 1 :";   gets(player1name);   cout<<"\n\n\Enter Name of player 2 :";   gets(player2name);   while(player1<=100 && player2<=100)   {     board();     gamescore(player1name,player2name,player1,player2);     cout<<"\n\n--->" <<player1name<<" Now your Turn >> Press any key to play ";     getch

Hangman

source code: #include <iostream.h> #include <stdlib.h> #include <string.h> #include <conio.h> const int MAXLENGTH=80; const int MAX_TRIES=5; const int MAXROW=7; int letterFill (char, char[], char[]); void initUnknown (char[], char[]); int main () { char unknown [MAXLENGTH]; char letter; int num_of_wrong_guesses=0; char word[MAXLENGTH]; char words[][MAXLENGTH] = { "india", "pakistan", "nepal", "malaysia", "philippines", "australia", "iran", "ethiopia", "oman", "indonesia" }; //choose and copy a word from array of words randomly randomize(); int n=random(10); strcpy(word,words[n]); // Initialize the secret word with the * character. initUnknown(word, unknown); // welcome the user cout << "\n\nWelcome to hangman...Guess a country Name

Casino

source code: #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<stdio.h> #include<time.h> void draw_line(int n,char ch); void rules(); void main() {   int balanceamt,amt,no,dice;   char playername[80],ch;   clrscr();   draw_line(60,'=');   cout<<"\n\n\n\n\t\tCASINO GAME\n\n\n\n";   draw_line(60,'=');   cout<<"\n\n\nEnter Your Name  :";   gets(playername);   cout<<"\n\n\Enter Deposit amount to play game :";   cin>>balanceamt;   do   {     clrscr();     rules();     cout<<"\n\nYour current balance is Rs."<<balanceamt;     do     { cout<<"\n\n"<<playername<<" enter money to bet"; cin>>amt; if(amt>balanceamt)   cout<<"Your betting amount is more than your current balance\n\nRe-enter data\n "; else   break;