Wednesday, October 3, 2018

18CPL27 SYLLABUS

C PROGRAMMING LABORATORY SYLLABUS
SUBJECT CODE - 18CPL27

CPL 1 - FAMILIARIZATION WITH COMPUTER HARDWARE & PROGRAMMING ENVIRONMENT

1. Familiarization with computer hardware and programming environment, concept of naming the program files, storing, compilation, execution and debugging, taking any simple C- code. [Not included in the Lab Exam ]

Generally, the programs created using programming languages like C, C++, Java etc., are written
using high level language like English. But, computer cannot understand the high level language. It can understand only low level language. So, the program written in high level language needs to be converted into low level language to make it understandable for the computer. This conversion is performed using either Interpreter or Compiler. Popular programming languages like C, C++, Java etc., use compiler to convert high level language instructions into low level language instructions. Compiler is a program that converts high level language instructions into low level language instructions. Generally, compiler performs two things, first it verifies the program errors, if errors are found, it returns list of errors otherwise it converts the complete code into low level language. To create and execute C programs in Windows Operating System, we need to install Turbo C software. We use the following steps to create and execute C programs in Windows OS…


Step 1: Creating Source Code
Source code is a file with C programming instructions in high level language. To create source
code, we use any text editor to write the program instructions. The instructions written in the
source code must follow the C programming language rules. The following steps are used to create
source code file in Windows OS…
1. Click on Start button
2. Select Run
3. Type cmd and press Enter
4. Type cd c:\TC\bin in the command prompt and press Enter
5. Type TC press Enter
6. Click on File -> New in C Editor window
7. Type the program
8. Save it as FileName.c (Use shortcut key F2 to save)


Step 2: Compile Source Code (Alt + F9)
Compilation is the process of converting high level language instructions into low level language
instructions. We use the shortcut key Alt + F9 to compile a C program in Turbo C.
Compilation is the process of converting high level language instructions into low level language
Instructions Whenever we press Alt + F9, the source file is going to be submitted to the Compiler. On receiving a source file, the compiler first checks for the Errors. If there are any Errors then compiler returns List of Errors, if there are no errors then the source code is  onverted into object code and stores it as file with .obj extension. Then the object code is given to the Linker. The Linker combines both the object code and specified header file code and generates an Executable file with .exe extension.


Step 3: Executing / Running Executable File (Ctrl + F9)
After completing compilation successfully, an executable file is created with .exe extension. The
processor can understand this .exe file content so that it can perform the task specified in the
source file. We use a shortcut key Ctrl + F9 to run a C program. Whenever we press Ctrl + F9, the .exe file is submitted to the CPU. On receiving .exe file, CPU performs the task according to the instruction written in the file. The result generated from the execution is placed in a window called User Screen.


Step 4: Check Result (Alt + F5)
After running the program, the result is placed into User Screen. Just we need to open the User
Screen to check the result of the program execution. We use the shortcut key Alt + F5 to open the User Screen and check the result. The file which contains c program instructions in high level language is said to be source code. Every c program source file is saved with .c extension, for example Sample.c. Whenever we press Alt + F9 the source file is submitted to the compiler. Compiler checks for the errors, if there are any errors, it returns list of errors, otherwise generates object code in a file  file into object file and generates executable file as Sample.exe. With this compilation process completes. 


Now, we need to Run the executable file (Sample.exe). To run a program we press Ctrl + F9. When we press Ctrl + F9 the executable file is submitted to the CPU. Then CPU performs the task according to the instructions written in that program and place the result into UserScreen.
Then we press Alt + F5 to open UserScreen and check the result of the program.


Important Points
1. C program file (Source file) must save with .c extension.
2. Compiler converts complete program at a time from high level language to low level
3 Input to the compiler is .c file and output from the compiler is .exe file, but it also
            generates .obj file in this process.
4 Compiler converts the file only if there are no errors in the source code.
5 CPU places the result in User Screen window.


Overall Process
Type the program in C editor and save with .c extension (Press F2 to save).
Press Alt + F9 to compile the program.
If there are errors, correct the errors and recompile the program.
If there are no errors, then press Ctrl + F9 to execute / run the program.
Press Alt + F5 to open User Screen and check the result.

CPL 2 - A COMMERCIAL CALCULATOR

2. Develop a program to solve simple computational problems using arithmetic expressions and use of each operator leading to simulation of a commercial calculator. (No built-in math
function)

Algorithm:

Step 1: start
Step 2: read a and b variables
Step 3: display the options
Step 4: read input choice
Step 5: choice is 1 then goto step 6, or choice is 2 then goto step 7, or choice is 3 then goto step 8, or choice is 4 then goto step 9, or choice is other then these num goto step 10
Step 6: addition of two numbers
Step 7: subtraction of two numbers
Step 8: multiplication of two numbers
Step 9: division of two numbers.
Step 10: invalid choice
Step 11: print result
Step 12: stop



FLOWCHART:( click on image to zoom )


PROGRAM:

#include<stdio.h>
void main()
{
    int a,b;
    int op;

    printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n 5.Remainder\n");
    printf("Enter the values of a & b: ");
    scanf("%d %d",&a,&b);  
    printf("Enter your Choice : ");
    scanf("%d",&op);
    
    switch(op)
    {
        case 1  :
        printf("Sum of %d and %d is : %d",a,b,a+b);
        break;
        case 2  :
        printf("Difference of %d and %d is : %d",a,b,a-b);
        break;
        case 3  :
        printf("Multiplication of %d and %d is : %d",a,b,a*b);
        break;
        case 4  :
        printf("Division of Two Numbers is %d : ",a/b);
        break;
        case 5:
        printf("Remainder of %d and %d is : %d",a,b,a%b);
        break;
        default :
        printf(" Enter Your Correct Choice.");
        break;
    }
}



OUTPUT:( click on image to zoom )

CPL 3 - ROOTS OF A QUADRATIC EQUATION

3. Develop a program to compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.

Algorithm:

Step1:[read the values of A,B and C]
       Read A,B,C

Step2:[check for the co-efficients]
      If  A=0 and B=0 and C=0
       Then roots are non-determinant

Step 3:[compute the value of Disc]
       Disc=B*B-4*A*C;

Step 4:[Depending on the value of disc different roots are obtained]
  If Disc is lesser than 0 go to Step 5
         If Disc is equals to 0 go to step 6
If Disc is greater than 0 go to step 7

Step 5: Print the imaginary roots
Realp=-B/2*A;
Imagp =sqrt(Disc)/2*A;
Root1=realp+imagep;
     Root2=realp-imagep;

Step 6: roots are real and equal
    Root1=-B/2*A;
Root2=root1 ;

Step 7: roots are real and distinct
Root1=-B+sqrt(Disc)/2*A;
Root2=-B-sqrt(Disc)/2*A;

Step 8: stop


FLOWCHART:( click on image to zoom )


PROGRAM:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

void main()
{
float a,b,c,disc;
float root1,root2,realp,imagp;
printf("Enter values of a,b,c\n");
scanf("%f%f%f",&a,&b,&c);

if(a==0 && b==0 && c==0)
{
printf("roots cannot be determined\n");
exit(0);
}
else
{
disc=b*b-4*a*c;
if(disc>0)
{
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("roots are real and distinct\n");
printf("root1=%f\n",root1);
printf("root2=%f\n",root2);
}

else if(disc==0)
{
root1=-b/(2*a);
root2=root1;
printf("roots are real and equal\n");
printf("root1=%f\n",root1);
printf("root2=%f\n",root2);
}

else if(disc<0)
{
realp=-b/(2*a);
imagp=sqrt(abs(disc)/(2*a));
printf("roots are complex\n");
printf("root1=%f+i%f\n",realp,imagp);
printf("root2=%f-i%f\n",realp,imagp);
}
}

}



OUTPUT:( click on image to zoom )


CPL 4 - PALINDROME OR NOT

4. Develop a program to find the reverse of a positive integer and check for palindrome or not. Display appropriate messages.

Algorithm:

ALGORITHM:
Step 1: [read the number N]
     Read N

Step 2: initialize M=N,REV=0

Step 3: [check the value of  N]
        If N is not equal to 0  go to step 4 else go to step 5

Step 4: digit=N%10;
        N=N/10;
    REV=REV*10+digit ;[executes these three steps until N value become 0] 

Step 5: check weather the M=REV if so,go to step 6 else goto step 7

Step 6: Print number is palindrome

Step 7: Print number is not palindrome  

Step 8: Stop.

FLOWCHART:( click on image to zoom )

PROGRAM:

#include<stdio.h>

int main()
{
   int n,m,rev,digit;
   printf("Enter the value of n\n");
   scanf("%d",&n);

   rev=0;
   m=n;
   while(n!=0)
   {
      digit=n%10;
      n=n/10;
      rev=digit+10*rev;
   }
   printf("Reverse of %d is %d\n",m,rev);

   if(m==rev)
     printf("It is a palindrome\n");
   else
     printf("It is not a palindrome\n");
}


OUTPUT:( click on image to zoom )


CPL 5 - ELECTRICITY BOARD CHARGES

5. An electricity board charges the following rates for the use of electricity: for the first 200 units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1 per unit. All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more than Rs 400, then an additional surcharge of 15% of the total amount is charged. Write a program to read the name of the user, a number of units consumed and print out the charges.

ALGORITHM:

Step 1: start
Step 2: read customer number
Step 3: read customer name
Step 4: read unit consumed
Step 5: unit is less than 200, charge 0.80 per unit
Step 6: if the unit is greater then 200 and less then 300, charge 0.90 per unit
Step 7: unit is greater than 400 charges 1.00 per unit,
Step 8: calculate the amount by unit* charge
Step 9: if the amount is greater then 400, then an additional surcharge of 15% of the total amount is charge total-amount=amt+surcharge.
Step 10: Addition of rs 100 as a minimum meter charge for all customer to the total amount.
Step 11: print customer number,name,unit_consumed_amount,surcharge,totalAMOUNT+100

Step 12:stop


FLOWCHART:( click on image to zoom )





PROGRAM:


#include<stdio.h>
#include<string.h>

void main()
{  
   int cust_no, unit_con;
   float charge,surcharge=0, amt, total_amt;
   char nm[25];
   
   printf("Enter the customer IDNO :\t");
   scanf("%d",&cust_no);
   printf("Enter the customer Name :\t");
   scanf("%s",nm);
   printf("Enter the unit consumed by customer  :\t");
   scanf("%d",&unit_con);

   if (unit_con <200 )
      charge = 0.80;
   else if (unit_con>=200 && unit_con<300)
      charge = 0.90;
   else
      charge = 1.00;

   amt = unit_con*charge;
   if (amt>400)
      surcharge = amt*15/100.0;
   total_amt = amt+surcharge;
   
   printf("\t\t\t\nElectricity Bill\n\n");
   printf("Customer IDNO                       :\t%d",cust_no);
   printf("\nCustomer Name                       :\t%s",nm);
   printf("\nunit Consumed                       :\t%d",unit_con);
   printf("\nAmount Charges @Rs. %4.2f  per unit  :\t%0.2f",charge,amt);
   printf("\nSurchage Amount                     :\t%.2f",surcharge);
   printf("\nMinimum meter charge Rs             :\t%d",100);
   printf("\nNet Amount Paid By the Customer     :\t%.2f",total_amt+100);

}


OUTPUT:( click on image to zoom )



CPL 6 - BINARY SEARCH

6. Introduce 1D Array manipulation and implement Binary search.

Algorithm:

Step 1:[read the value of N]
     Read N

Step 2:[Enter the elements in ascending order]
For i=0 to N-1 is to be read from the keyboard

Step 3:[Enter the one element for searching that could be a key element]

Step 4: initially set low=0 and high=N

Step 5:[Find out the middle value]
           Mid=(low+hogh)/2;

Step 6: Check key value is lesser than the middle value go to step 7
       Check key value is greater than the middle value go to step 8
     
Step 7: high=mid-1

Step 8: low =mid+1;

Step 9: if key value and mid-value are same then go to step 10 if not go to step 11

Step 10: successful search

Step 11: not searched the key element

Step 12: stop

FLOWCHART:( click on image to zoom )






PROGRAM:


#include<stdio.h>
#include<string.h>

void main()
{
char name[50][50],key[50];
int n,i,low,high,mid,found=0;
printf("How many names to Read:\n");
scanf("%d",&n);
printf("Enter the Names in ascending order:\n");
for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}
printf("\nType the name to be searched:");
scanf("%s",key);

low=0;
high=n-1;
while(low<=high && !found)
{
mid=(low+high)/2;
if(strcmp(name[mid],key)==0)
found=1;
    else if(strcmp(name[mid],key)<0)
low=mid+1;
    else
high=mid-1;
}

if(found==1)
printf("\nSearch successful and Name found in the list at  position:%d",mid+1);
else
printf("Name not found and search Unsuccessful....!");

}


OUTPUT:( click on image to zoom )



CPL 7 - PRIME NUMBER

7. Implement using functions to check whether the given number is prime and display appropriate messages. (No built-in math function)

ALGORITHM:

Step 1: Start 

Step 2: [Read the value of N(positive integers)]
    Read N 

Step 3: [to check whether N is prime or not]
For i=2 to m/2 
If m%i=0 then go to step 4 otherwise go to step 5 

Step 4: Print message  not a prime number

Step 5: Print a message  prime number 

Step 6: stop


FLOWCHART:( click on image to zoom )



PROGRAM:

#include<stdio.h>
int isprime(int n);

int main()
{
int i,m1,m2;
printf("enter range\n");
scanf("%d%d",&m1,&m2);
printf("prime num from %d to %d are:\n",m1,m2);
for(i=m1;i<=m2;i++)
{
if(isprime(i))
printf("%d\n",i);
}
}

int isprime(int m)
{
int i;
for(i=2;i<=m/2;i++)
{
if(m%i==0)
{
return 0;
}
}
return 1;
}


OUTPUT:( click on image to zoom )



CPL 8 - MATRIX MULTIPLICATION

8. Develop a program to introduce 2D Array manipulation and implement Matrix multiplication and ensure the rules of multiplication are checked.

ALGORITHM:

Step 1: Start

Step 2: [Read the order of both matrices M,N,P,Q]

Step 3: Check for i=0 to M  and j=0 to N for the matrix A,read A[i][j]

Step 4: Check for i=0to P and j=0 to Q for matrix B read B[i][j]

Step 5: if(N=P) then only multiplication is possible then go to step 6 otherwise go
            to step 7

Step 6: intialy set matrix C[i][j] as 0
  For k=0 to n
C[i][j]=C[i][j]+A[i][k]*B[i][k] go to step 8

Step 7: Multiplication is not possible

Step 8: Prints the multiplication of two matrix


Step 9: Stop

FLOWCHART PART - 1

FLOWCHART PART - 2

PROGRAM :

#include<stdio.h>

int main()
{
   int a[20][20],b[20][20],c[20][20];
   int m,n,p,q,i,j,k;

   printf("Enter rows and columns of matrix A\n");
   scanf("%d%d",&m,&n);
   printf("Enter rows and columns of matrix B\n");
   scanf("%d%d",&p,&q);

   if(n!=p)
   {
      printf("Matrix multiplication not possible\n");
      return 0;
   }

   printf("Enter elements of matrix A\n");
   for(i=0;i<m;i++)
   {
      for(j=0;j<n;j++)
      {
        scanf("%d",&a[i][j]);
      }
   }

   printf("Enter elements of matrix B\n");
   for(i=0;i<p;i++)
   {
      for(j=0;j<q;j++)
      {
        scanf("%d",&b[i][j]);
      }
   }

   for(i=0;i<p;i++)
   {
      for(j=0;j<q;j++)
      {
       c[i][j]=0;
       for(k=0;k<n;k++)
       {
          c[i][j]=c[i][j]+a[i][k]*b[k][j];
       }
      }
   }

   printf("Product of two matrices is\n");
   for(i=0;i<m;i++)
   {
      for(j=0;j<q;j++)
      {
      printf("%d\n",c[i][j]);
      }
   }

}


OUTPUT: