WELCOME TO DATTU.KVN'S BLOG

Friday 6 December 2013

Important C Programs

 
 {1} Fibonacci Series :

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,c,i,n; 
clrscr();
a=0; 
b=1; 
printf("\n enter n for how many times generate series"); 
scanf("%d",&n); 
printf("\nFIBONACCI SERIES\n");
printf(" %d %d",a,b);
for(i=0;i<n;i++)
{
   c=a+b;
 a=b;
b=c;
  printf(" %d",c);
 }
 getch();
}

Output:
enter n for how many times generate series:5
FIBONACCI SERIES:1 1 2 3 5


{2} Prime Number :

#include<stdio.h>
#include<conio.h>
int main()
{
int i,j=2,ch=0; 
clrscr();
printf("\nENTER ANY NUMBER");
scanf("%d",&i); 
while(j<=i/2) 
{
if(i%j==0) 
 {
 printf("%d IS NOT PRIME",i); 
ch=1; 
break; 
}
else
{
j++;
}
}
if(ch==0) 
{
printf("%d IS PRIME",i); 
}
}

Output:

ENTER ANY NUMBER 10
10 IS NOT PRIME

{3} Bubble Sort :

#include <stdio.h>
void bubble_sort(long [], long); 
int main()
{
 long array[100], n, c, d, swap; 
 printf("Enter number of elements:");
 scanf("%ld", &n); 
printf("Enter %ld longegers\n", n); 
for (c = 0; c < n; c++)
scanf("%ld", &array[c]); 
bubble_sort(array, n); 
printf("Sorted list in ascending order:n");
for ( c = 0 ; c < n ; c++ ) 
printf("%ld\n", array[c]); 
return 0; 
}
void bubble_sort(long list[], long n) 
{
  long c, d, t; 
 for (c = 0 ; c < ( n - 1 ); c++)
 {for (d = 0 ; d < n - c - 1; d++)
{
if (list[d] > list[d+1]) 
 {
        /* Swapping */ 
        t         = list[d]; 
        list[d]   = list[d+1]; 
        list[d+1] = t; 
      }
    }
  }
}

Output:
Enter number of elements:5
Enter 5 longegers 13 2 45 5 7
Sorted list in ascending order:2 5 7 13 45

{4} Matrix Multiplication :

#include<stdio.h>
#include<conio.h>
main()
{
    int m1[10][10],i,j,k,m2[10][10],mult[10][10],r1,c1,r2,c2; 
    printf("Enter number of rows and columns of first matrix (less than 10)\n");
 scanf("%d%d",&r1,&c1); 
   printf("Enternumber of rows and columns of second matrix (less than 10)\n");
    scanf("%d%d",&r2,&c2); 
if(r2==c1) 
   {
       printf("Enter rows and columns of First matrix \n");
       printf("Row wise\n");
 for(i=0;i<r1;i++)
         for(j=0;j<c1;j++)
             scanf("%d",&m1[i][j]); 
     printf("First Matrix is :\n");
      for(i=0;i<r1;i++)
       {
            for(j=0;j<c1;j++)
                printf("%d ",m1[i][j]); 
            printf("
");
        }
        printf("Enter rows and columns of Second matrix 
");
        printf("Row wise\n");
       for(i=0;i<r2;i++)
           for(j=0;j<c2;j++)
               scanf("%d",&m2[i][j]); 
       printf("Second Matrix is:\n");
        for(i=0;i<r2;i++)
        {
          for(j=0;j<c2;j++)
                printf("%d ",m2[i][j]); 
           printf("\n");
        }
        printf("Multiplication of the Matrices:\n");
        for(i=0;i<r1;i++)
       {
           for(j=0;j<c2;j++)
           {
               mult[i][j]=0; 
               for(k=0;k<r1;k++)
                    mult[i][j]+=m1[i][k]*m2[k][j]; 
               printf("%d ",mult[i][j]); 
           }
          printf("
");
        }
    }
    else
    {
        printf("Matrix multiplication cannot be done");
    }
    return 0; 
}

OutPut:
Enter number of rows and columns of first matrix (less than 10):2 2
Enter number of rows and columns of Second matrix (less than 10):2 2
Enter rows and columns of First matrix
Row wise:
2 3 4 5
First Matrix is :
2 3
4 5
Enter rows and columns of Second matrix
Row Wise
3 4 5 6
Second matrix:
3 4
5 6
Multiplications of Matrixs
21 26
37 46

{5} Palindrome :

#include <stdio.h>
#include <string.h>
 main()
{
   char a[100], b[100]; 
    printf("Enter the string to check if it is a palindrome\n");
gets(a); 
    strcpy(b,a); 
   strrev(b); 
    if( strcmp(a,b) == 0 ) 
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");
    return 0; 
}

Output:
Enter the string to check if it is a palindrome:dada
Entered string is a palindrome.
Back to TOP