Tuesday, 26 February 2019

Simple C Programs

/* Employee pay slip generation */
#include <stdio.h>
#include <conio.h>
void main()
{
    int empno;
    char name[30];
    float basic,da,ta,hra,it,gross,net;
    printf("\n Enter employee number:");
    scanf("%d",&empno);
    printf("\n Enter employee name:");
    scanf("%s",name);
    printf("\n Enter basic salary:");
    scanf("%f",&basic);
    if(basic > 8000)
    {
       hra = basic * 40 / 100;
       da =  basic * 30 / 100;
       it =  basic * 10 / 100;
     }
     else
     {
       hra = basic * 30 / 100;
       da =  basic * 20 / 100;
       it =  0;
     }
      ta = basic * 10 /100;
      gross = basic + da + ta + hra;
      net = gross - it;
     clrscr();
     printf(" Employee Number is: %d",empno);
     printf("\n Employee Name is: %s",name);
     printf("\n Basic Salary is: %.2f",basic);
     printf("\n DA is: %.2f",da);
     printf("\n TA is: %.2f",ta);
     printf("\n HRA is: %.2f",hra);
     printf("\n IT is: %.2f",it);
     printf("\n Gross Salary is: %.2f",gross);
     printf("\n Net Salary is: %.2f",net);
}
Output:
Enter employee number:101
Enter employee name: Ganesh
Enter basic salary: 10000


Employee Number is: 101
Employee Name is: Ganesh
Basic Salary is: 10000.00
DA is: 3000.00
TA is: 1000.00
 HRA is: 4000.00
 IT is: 1000.00
 Gross Salary is: 18000.00
 Net Salary is: 17000.00


/* Finding Roots of a Quadratic Equation */

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    int a,b,c;
    float r1,r2,d;
    clrscr();
    printf("\n Enter values ( > 0 )for calculating discremenent:");
    scanf("%d%d%d",&a,&b,&c);
    d = (float) b*b - 4.0 * a * c;
    if(d==0)
       {
printf("\n Roots are Real and Equal");
r1 = -b / (2.0 * a);
r2 = -b / (2.0 * a);
printf(" \nThe Roots are %f and %f",r1,r2);
       }
     else if(d>0)
       {
printf("\n Roots are Real and Unequal");
r1 = (-b + sqrt(d)) / (2.0 * a);
r2 = (-b - sqrt(d)) / (2.0 * a);
printf("\nThe Roots are %.2f and %.2f",r1,r2);
       }
     else
printf("\n Roots are imaginary");
}

Output:

Enter values ( > 0 )for calculating discremenent: 1 3 2
Roots are Real and Equal
The Roots are –1.00 and –2.00


/* Checking whether Prime number or not */

#include<stdio.h>
#include<conio.h>
void main()
{
   int n,flag, i;
   clrscr();
   printf("Enter a number:");
   scanf("%d",&n);
   flag=0;
   for(i=2;i<=n/2;i++)
    if(n % i == 0)
      {
flag++;
break;
      }
    if(flag == 0)
      printf("\nEntered number is a prime number");
    else
      printf("\nEntered number is not a prime number");
}

Output:

Enter a number: 97
Entered number is a prime number


/* Reverse of a given number */

#include<stdio.h>
#include<conio.h>
void main()
{
   int n,r,rev=0;
   clrscr();
   printf("\nEnter a number:");
   scanf("%d",&n);
   while(n != 0)
   {
     r = n % 10;
     rev = rev * 10 + r;
     n  = n / 10 ;
   }
   printf("\nReverse = %d",rev);
 }

Output:

Enter a number: 123
Reverse = 321


/* Factorial of a Number Using Recursion */

#include<stdio.h>
#include<conio.h>
int fact(int n)
{
    int f;
    if(n==0)
       return 1;
    else
       f= n * fact(n-1);
       return f;
}
void main()
{
   int n,f;
   clrscr();
   printf("\n Enter the value of n:");
   scanf("%d",&n);
   f = fact(n);
   printf("\n The factorial of %d is %d",n,f);
}

Output:

Enter the value of n: 6
The factorial of 6 is 720


/* Call by Reference */

#include<stdio.h>
#include<conio.h>
void swap(int *p, int *q)
{
   int temp;
   temp = *p;
   *p = *q;
   *q = temp;
}
void main()
{
   int a,b;
   clrscr();
   printf("Enter two numbers a and b:");
   scanf("%d%d",&a,&b);
   printf("\nBefore passing a = %d, b = %d",a,b);
   swap(&a,&b);
   printf("\nAfter passing a = %d, b = %d",a,b);
}

Output:

Enter two numbers a and b: 10 23
Before passing a = 10, b = 23
After passing a = 23, b = 10


/* Febonacci Series Using Static Variables */

#include<stdio.h>
#include<conio.h>
int fib(int i)
{
   static int f=0,s=1,temp=0;
    if(i==1)
      return 0;
    f=s;
    s=temp;
    temp = f + s;
    return temp;
}
void main()
{
   int i,n,s;
   clrscr();
   printf("Enter how many numbers of the series:");
   scanf("%d",&n);
   printf(“\nThe Febonacci Series is ….”);
   for(i=1;i<=n;i++)
     {
       s = fib(i);
       printf("\n%d",s);
     }
}

Output:

Enter how many numbers of the series: 8
The Febonacci Series is ….
0
1
1
2
3
5
8
13


/* Linear Search  */
#include<stdio.h>
#include<conio.h>
void main()
{
    int a[10],n,ele,flag =0,i;
    clrscr();
    printf("How many elements:");
    scanf("%d",&n);
    printf("\n Enter elements into the array:");
    for(i=0;i<n;i++)
    {
scanf("%d",&a[i]);
    }
    printf("\nEnter element to be searched:");
    scanf("%d",&ele);
    for(i=0;i<n;i++)
    {
if(ele == a[i])
{
    flag++;
    break;
}
     }
    if(flag == 1)
printf("\n Element %d found at %d",ele,i+1);
     else
printf("\n Element %d is not found",ele);
 }

Output

How many elements: 5
Enter elements into the array: 10
40
30
50
20
Enter element to be searched: 30
Element 30 found at 3 


/* Binary Search */
#include<stdio.h>
#include<conio.h>
void main()
{
   int a[10],flag=0,i,n,top,mid,bottom,key;
   clrscr();
   printf("\nEnter size of the array:");
   scanf("%d",&n);
   printf("\nEnter elements into the array in sorted order:");
   for(i=0;i<n;i++)
       scanf("%d",&a[i]);
   printf("\nEnter element to be searched:");
   scanf("%d",&key);
   top = 0;
   bottom = n-1;
   while(top <= bottom && flag == 0)
   {
       mid = (top + bottom) /2 ;
       if(a[mid] == key)
  flag = 1;
else if(a[mid] < key)
   top = mid + 1;
else
   bottom = mid - 1;
    }
    if(flag == 1)
       printf("\nSearch element %d found at position %d",key,mid+1);
    else
       printf("\nSearch element %d is not found.",key);
}

Output

Enter size of the array: 5
Enter elements into the array in sorted order: 10  20  30  40  50
Enter element to be searched: 40
Search element 40 found at position 4


/* Bubble Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
    int a[10],n,i,j,t;
    clrscr();
    printf("\nEnter size of the array:");
    scanf("%d",&n);
    printf("\nEnter elements into the array:");
    for(i=0;i<n;i++)
       scanf("%d",&a[i]);
    for(i=0;i<n-1;i++)
      for(j=0;j<n-1-i;j++)
   if(a[j] > a[j+1])
   {
      t = a[j];
      a[j] = a[j+1];
      a[j+1] = t;
    }
     printf("\n The sorted array is :");
     for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

Output

Enter size of the array: 5
Enter elements into the array: 10
40
30
50
20

The sorted array is :  10     20     30      40    50


/* Addition of two matrices */
#include <stdio.h>
#include <conio.h>
void main()
{
    int m1[3][3],m2[3][3],m3[3][3],i,j;
    clrscr();
    printf("Enter elements into matrix-1: \n");
    for(i=0;i<3;i++)
      for(j=0;j<3;j++)
scanf("%d",&m1[i][j]);
    printf("Enter elements into matrix-2: \n");
    for(i=0;i<3;i++)
      for(j=0;j<3;j++)
scanf("%d",&m2[i][j]);
    for(i=0;i<3;i++)
      for(j=0;j<3;j++)
  m3[i][j] = m1[i][j] + m2[i][j];
    printf("\n Sum : \n");
    for(i=0;i<3;i++)
     {
      for(j=0;j<3;j++)
printf("%3d",m3[i][j]);
      printf("\n");
     }
}

Output:
Enter elements into matrix-1:
1 2 3 4 5 6 7 8 9
Enter elements into matrix-2:
1 2 3 4 5 6 7 8 9
Sum :
   2    4    6
   8  10  12
 14  16  18



/* Matrix Multiplication */
#include <stdio.h>
#include <conio.h>
#include <process.h>
void main()
{
  int m1[10][10],m2[10][10],m3[10][10]={0};
  int i,j,k,r1,r2,c1,c2;
  clrscr();
  printf("Enter number of rows and number of columns of matrix-1:");
  scanf("%d%d",&r1,&c1);
  printf("Enter number of rows and number of columns of matrix-2:");
  scanf("%d%d",&r2,&c2);
  if(c1 != r2)
  {
     printf("\n Multiplication is not possible.");
     exit(0);
  }
  printf("\n Enter elements into matrix-1:");
  for(i=0;i<r1;i++)
    for(j=0;j<c1;j++)
       scanf("%d",&m1[i][j]);
  printf("\n Enter elements into matrix-2:");
  for(i=0;i<r2;i++)
    for(j=0;j<c2;j++)
       scanf("%d",&m2[i][j]);
  for(i=0;i<r1;i++)
    for(j=0;j<c2;j++)
       for(k=0;k<c1;k++)
  m3[i][j] = m3[i][j] + m1[i][k] * m2[k][j];
  printf("\n Product:\n");
  for(i=0;i<r1;i++)
   {
    for(j=0;j<c2;j++)
       printf("%5d",m3[i][j]);
    printf("\n");
   }
}

Output:
Enter number of rows and number of columns of matrix-1: 3 3
Enter number of rows and number of columns of matrix-2: 3 3
Enter elements into matrix-1: 1 2 3 4 5 6 7 8 9
Enter elements into matrix-2: 1 2 3 4 5 6 7 8 9
Product:
         30     36     42
         66     81     96
       102   126   150


/* Transpose of a Matrix */
#include <stdio.h>
#include <conio.h>
void main()
{
  int m1[10][10],m2[10][10],i,j,r,c;
  clrscr();
  printf("Enter number of rows and number of columns of the matrix:");
  scanf("%d%d",&r,&c);
  printf("\n Enter elements into matrix:");
  for(i=0;i<r;i++)
    for(j=0;j<c;j++)
       scanf("%d",&m1[i][j]);
  for(i=0;i<c;i++)
    for(j=0;j<r;j++)
  m2[i][j] = m1[j][i];
  printf("\n Original :\n");
  for(i=0;i<r;i++)
   {
    for(j=0;j<c;j++)
       printf("%5d",m1[i][j]);
    printf("\n");
   }
  printf("\n Transpose:\n");
  for(i=0;i<c;i++)
   {
    for(j=0;j<r;j++)
       printf("%5d",m2[i][j]);
    printf("\n");
   }
}

Output:
Enter number of rows and number of columns of the matrix: 4 3
Enter elements into matrix: 1 2 3 4 5 6 7 8 9 10 11 12
Original :
1    2     3
4      5     6
7      8     9
10  11   12
Transpose:
     1      4     7   10
     2      5     8   11
     3      6     9   12

/* String Concatenation */
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
    char st1[30],st2[30],st3[60];
    clrscr();
    printf("\n Enter string-1:");
    gets(st1);
    printf("\n Enter string-2:");
    gets(st2);
    strcpy(st3,st1);
    strcat(st3,st2);
    puts(st1);
    puts(st2);
    puts(st3);
 }


Output:
Enter string-1: gvp
Enter string-2: college

gvp
college
gvpcollege


/* Student Structure */
#include <stdio.h>
#include <conio.h>
struct stud
{
    int sno;
    char sname[10];
    int m1,m2,m3;
    float avg;
};
void main()
{
   struct stud s;
   clrscr();
   printf("Enter student number:");
   scanf("%d",&s.sno);
   printf("Enter student name:");
   scanf("%s",s.sname);
   printf("Enter marks in subject-1:");
   scanf("%d",&s.m1);
   printf("Enter marks in subject-2:");
   scanf("%d",&s.m2);
   printf("Enter marks in subject-3:");
   scanf("%d",&s.m3);
   s.avg = ( s.m1 + s.m2 + s.m3 ) / 3.0;
   clrscr();
   printf("Student Details: \n ");
   printf("Number    Name    Average \n");
   printf("%d   %s  %f",s.sno,s.sname,s.avg);
}

Output:
Enter student number: 1001
Enter student name: Ganesh
Enter marks in subject-1: 80
Enter marks in subject-2: 70
Enter marks in subject-3: 60

Student Details:
Number    Name    Average
1001         Ganesh   70.00


/* Greatest of three numbers */
#include <stdio.h>
#include <conio.h>
#define A printf("%d",a)
#define B printf("%d",b)
#define C printf("%d",c)
void main()
{
    int a,b,c;
    clrscr();
    printf("\n Enter Three numbers:");
    scanf("%d%d%d",&a,&b,&c);
    printf("\n Gretest of the three numbers is :");
    a > b ? (a>c ? A:C):(b>c ? B:C);
}

Output:
Enter Three numbers: 10
20
30

Gretest of the three numbers is : 30


No comments:

Post a Comment