Monthly Archives: October 2013


Beginner Write your first Assembly Language program – Hello World!! [explained]

If you have just started learning Assembly language programming, here is a example Assembly program explained so that you can understand the very basic terminology before you write more complex Assembly Applications. First Assembly program simply prints a text message “Hello World” on Screen.

DATA SEGMENT
     MESSAGE DB "HELLO WORLD!!!$"
ENDS
CODE SEGMENT  
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX
      LEA DX,MESSAGE       
      MOV AH,9
      INT 21H
      MOV AH,4CH
      INT 21H      
ENDS
END START

In this Assembly Language Programming, A single program is divided into four Segments which are 1. Data Segment, 2. Code Segment, 3. Stack Segment, and 4. Extra  Segment. Now, from these one is compulsory i.e. Code Segment if at all you don’t need variable(s) for your program.if you need variable(s) for your program you will need two Segments i.e. Code Segment and Data Segment.

First Line – DATA SEGMENT

DATA SEGMENT is the starting point of the Data Segment in a Program and DATA is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can declare our variables.

Next Line – MESSAGE DB “HELLO WORLD!!!$”

MESSAGE is the variable name given to  a Data Type (Size) that is DB. DB stands for Define Byte and is of One byte (8 bits). In Assembly language programs, variables are defined by Data Size not its Type. Character need One Byte so to store Character or String we need DB only that don’t mean DB can’t hold number or numerical value. The string is given in double quotes. $ is used as NULL character in C programming, So that compiler can understand where to STOP.

Next Line – DATA ENDS

DATA ENDS is the End point of the Data Segment in a Program. We can write just ENDS But to differentiate the end of which segment it is of which we have to write the same name given to the Data Segment.

Next Line – CODE SEGMENT

CODE SEGMENT is the starting point of the Code Segment in a Program and CODE is the name given to this segment and SEGMENT is the keyword for defining Segments, Where we can write the coding of the program.

Next Line –     ASSUME DS:DATA CS:CODE

In this Assembly Language Programming, their are Different Registers present for Different Purpose So we have to assume DATA is the name given to Data Segment register and CODE is the name given to Code Segment register (SS,ES are used in the same way as CS,DS )

Next Line – START:

START is the label used to show the starting point of the code which is written in the Code Segment. : is used to define a label as in C programming.

Next Line – MOV AX,DATA
MOV DS,AX

After Assuming DATA and CODE Segment, Still it is compulsory to initialize Data Segment to DS register.  MOV is a keyword to move the second element into the first element. But we cannot move DATA Directly to DS due to MOV commands restriction, Hence we move DATA to AX and then from AX to DS. AX is the first and most important register in the ALU unit. This part is also called INITIALIZATION OF DATA SEGMENT and It is important so that the Data elements or variables in the DATA Segment are made accessable. Other Segments are not needed to be initialized, Only assuming is enhalf.

Next Line – LEA DX,MESSAGE
MOV AH,9
INT 21H

The above three line code is used to print the string inside the MESSAGE variable. LEA stands for Load Effective Address which is used to assign Address of variable to DX register (The same can be written like this also MOV DX,OFFSET MESSAGE both mean the same). To do input and output in Assembly Language we use Interrupts. Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 9 or 9h or 9H (all means the same), That means PRINT the string whos Address is Loaded in DX register.

Next Line – MOV AH,4CH
INT 21H

The above two line code is used to exit to dos or exit to operating system. Standard Input and Standard Output related Interupts are found in INT 21H which is also called as DOS interrupt. It works with the value of AH register, If the Value is 4ch, That means Return to Operating System or DOS which is the End of the program.

Next Line – CODE ENDS

CODE ENDS is the End point of the Code Segment in a Program. We can write just ENDS But to differentiate the end of which segment it is of which we have to write the same name given to the Code Segment.

Last Line – END START

END START is the end of the label used to show the ending point of the code which is written in the Code Segment.

Execution of program explanation – Hello World

First save the program with HelloWorld.asm filename. No Space is allowed in the name of the Program File and extension as .asm (dot asm because it’s a Assembly language program). The written Program has to be complied and Run by clicking on the RUN button on the top. The Program with No Errors will only run and could show you the desired output. Just see the screenshots below.

Note :- In this Assembly Language Programming, We have Com format and EXE format. We are Learning in EXE format only which simple then COM format to understand and Write. We can write the program in lower or upper case, But i prepare Upper Case. (this program are executed on emu8086 emulator software)

Screen Shots :-

Asm_program_Hello_World

Asm_program_Hello_World_Output

A Assembly program to calculate average of numbers in an Array – IGNOU MCA Assignment 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCSL-017
Course Title : C and Assembly Language Programming
Assignment Number : MCA(1)/L017/Assign/2013

 

A program in assembly language to calculate the average of numbers in an array.

 

DATA SEGMENT
     ARRAY DB 1,4,2,3,8,6,7,5,9
     AVG DB ?
     MSG DB "AVERAGE = $"
ENDS

CODE SEGMENT 
    ASSUME DS:DATA CS:CODE
START:
      MOV AX,DATA
      MOV DS,AX

      LEA SI,ARRAY 
      LEA DX,MSG
      MOV AH,9
      INT 21H

      MOV AX,00
      MOV BL,9

      MOV CX,9 
      LOOP1:
           ADD AL,ARRAY[SI]
           INC SI
      LOOP LOOP1

      DIV BL

      ADD AL,30H

      MOV DL,AL
      MOV AH,2
      INT 21H

      MOV AH,4CH
      INT 21H     
ENDS
END START

program code :

  1. DATA SEGMENT
  2. ARRAY DB 1,4,2,3,8,6,7,5,9
  3. AVG DB ?
  4. MSG DB "AVERAGE = $"
  5. ENDS
  6.  
  7. CODE SEGMENT
  8. ASSUME DS:DATA CS:CODE
  9. START:
  10. MOV AX,DATA
  11. MOV DS,AX
  12.  
  13. LEA SI,ARRAY
  14. LEA DX,MSG
  15. MOV AH,9
  16. INT 21H
  17.  
  18. MOV AX,00
  19. MOV BL,9
  20.  
  21. MOV CX,9
  22. LOOP1:
  23. ADD AL,ARRAY[SI]
  24. INC SI
  25. LOOP LOOP1
  26.  
  27. DIV BL
  28.  
  29. ADD AL,30H
  30.  
  31. MOV DL,AL
  32. MOV AH,2
  33. INT 21H
  34.  
  35. MOV AH,4CH
  36. INT 21H
  37. ENDS
  38. END START

Screen shots :-

MCS017_S2_Q1_A

MCS017_S2_Q1_A_Ouput

A C program with a function that returns the minimum and the maximum value in an array of integers

void minmax(int array[],int length,int *min,int *max); //prototype

Let’s identify variables needed for this program.

In this program, we need several variables to store array of decimal numbers and count the length of array and store minimum and maximum of the array.

First variable will be the one which will save the list of integers in an array and it will be array[]. Next is will be min and max which will hold the minimum and maximum of the list. Next we will need a length variable to store Length of a array initialized so in all Four variables.
The identified variables are array[],min,max,length.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

 void minmax(int array[],int length,int *min,int *max);
  int array[]={12,4,8,14,3,21,45,18,1,6,9,2};
  int length,min,max;

Now, Selection of data type will be int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient for array[],min,max,length.

In this program, Variable length is used for storing Length is used to count the array size which will be integer value. Call the function by passing array[], length, address of min and max variable. Finally print the minimum and maximum i.e. min and max variable returned by the function at the end of the program.

#include<stdio.h>
void main()
{
  void minmax(int array[],int length,int *min,int *max);
  int array[]={12,4,8,14,3,21,45,18,1,6,9,2};
  int length,min,max;
  clrscr();
  length=0;
  while(array[length]!=NULL)
  {
     length++;
  };
  minmax(array,length,&min,&max);
  printf("\nminimum=%d",min);
  printf("\nmaximum=%d",max);
  getch();
}

Let’s identify variables needed for function which is part of program outside main function.
Now the only variable i which will be used for FOR LOOP. The identified variable is i.

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

void minmax(int array[],int length,int *min,int *max)
{
    int i;
    *min=*max=array[0];
 
    for(i=1;i<length;i++)
    {
       if(*min>array[i])
        *min=array[i];
       else if(*max<array[i])
        *max=array[i];
    }
}

In this program, pointer *min and *max which will be used for moving the minimum and maximum value to the variable within the main function loop. Both the value of pointers are assigned to the first in the list. and Inside the LOOP the value of the array is compared and replaced till the LOOP ends and finds minimum and maximum values in the array and return to main function.

Note:- Remember whenever you are calling a function which is returning a value it should always be assigned to a variable of same data type or called inside the printf function as variable.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.   void minmax(int array[],int length,int *min,int *max);
  5.   int array[]={12,4,8,14,3,21,45,18,1,6,9,2};
  6.   int length,min,max;
  7.   clrscr();
  8.   length=0;
  9.   while(array[length]!=NULL)
  10.   {
  11.      length++;
  12.   };
  13.   minmax(array,length,&min,&max);
  14.   printf("\nminimum=%d",min);
  15.   printf("\nmaximum=%d",max);
  16.   getch();
  17. }
  18.  
  19. void minmax(int array[],int length,int *min,int *max)
  20. {
  21.     int i;
  22.     *min=*max=array[0];
  23.  
  24.     for(i=1;i<length;i++)
  25.     {
  26.        if(*min>array[i])
  27.         *min=array[i];
  28.        else if(*max<array[i])
  29.         *max=array[i];
  30.     }
  31. }

SCREEN SHOTS:-

C_program_Min_Max_Function

C_program_Min_Max_Function_Output

A C program that multiply two complex numbers

Let’s identify variables needed for the structure inside the program.

In this complex structure, we need two variables to store real part and imaginary part of complex number.

First variable will be the one which will save the real part of complex number and it will be REAL. Second variable will be the one which will save the imaginary part of complex number and it will be IMG, So in all Two variables.
The identified variables are REAL, IMG.

struct COMPLEX
{
    int REAL;
    int IMG;
};

Now, Selection of data type will be int data type due to the values expected for complex number are decimals and they will be hold numbers so int data type is not sufficient for REAL, IMG.

Let’s identify variables needed for this program.

In this program, we need three variables to store two complex numbers entered by the user and one to store resultant complex number.

The identified variables are A, B, C data type will be same as the user defined data type struct COMPLEX.

Formula :- (a + bi) * (c + di) = (ac – bd) + (ad + bc)i

C.REAL=(A.REAL*B.REAL)-(A.IMG*B.IMG);
    C.IMG=(A.REAL*B.IMG)+(A.IMG*B.REAL);

In this program, Give message to user to enter the required complex numbers and scan the entered numbers i.e. REAL, IMG. Calculate the multiplication of the two complex numbers. Finally printing of the result at the end of the program.

C program code :

  1. #include<stdio.h>
  2. struct COMPLEX
  3. {
  4.     int REAL;
  5.     int IMG;
  6. };
  7. void main()
  8. {
  9.     struct COMPLEX A,B,C;
  10.     clrscr();
  11.     printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR A :");
  12.     scanf("%d",&A.REAL);
  13.     printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR A :");
  14.     scanf("%d",&A.IMG);
  15.     printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR B :");
  16.     scanf("%d",&B.REAL);
  17.     printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR B :");
  18.     scanf("%d",&B.IMG);
  19.     C.REAL=(A.REAL*B.REAL)-(A.IMG*B.IMG);
  20.     C.IMG=(A.REAL*B.IMG)+(A.IMG*B.REAL);
  21.     printf("\nRESULT FOR COMPLEX NO. C VALUES ARE :\n");
  22.     printf("(%d + %di) * (%d + %di) = %d + %di",A.REAL,A.IMG\
  23.     ,B.REAL,B.IMG,C.REAL,C.IMG);
  24.     getch();
  25. }

SCREEN SHOTS:-

C_program_Complex_Struct_Multiply

C_program_Complex_Struct_Multiply_Output

Now split the program to create a function by passing two structure complex variables to it. and return structure complex variable back.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

    struct COMPLEX A,B,C;
    struct COMPLEX multi_complex(struct COMPLEX,struct COMPLEX);

Let’s identify variables needed for the structure inside the program.

In this complex structure, we need two variables to store real part and imaginary part of complex number.

First variable will be the one which will save the real part of complex number and it will be REAL. Second variable will be the one which will save the imaginary part of complex number and it will be IMG, So in all Two variables.
The identified variables are REAL, IMG.

struct COMPLEX
{
    int REAL;
    int IMG;
};

Now, Selection of data type will be int data type due to the values expected for complex number are decimals and they will be hold numbers so int data type is not sufficient for REAL, IMG.

Let’s identify variables needed for this program.

In this program, we need three variables to store two complex numbers entered by the user and one to store resultant complex number.

The identified variables are A, B, C data type will be same as the user defined data type struct COMPLEX.

Formula :- (a + bi) * (c + di) = (ac – bd) + (ad + bc)i

C.REAL=(A.REAL*B.REAL)-(A.IMG*B.IMG);
    C.IMG=(A.REAL*B.IMG)+(A.IMG*B.REAL);

In this program, Give message to user to enter the required complex numbers and scan the entered numbers i.e. REAL, IMG. Calculate the multiplication of the two complex numbers. Finally printing of the result at the end of the program.

C program code :

  1. #include<stdio.h>
  2. struct COMPLEX
  3. {
  4.     int REAL;
  5.     int IMG;
  6. };
  7. void main()
  8. {
  9.     struct COMPLEX A,B,C;
  10.     struct COMPLEX multi_complex(struct COMPLEX,struct COMPLEX);
  11.     clrscr();
  12.     printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR A :");
  13.     scanf("%d",&A.REAL);
  14.     printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR A :");
  15.     scanf("%d",&A.IMG);
  16.     printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR B :");
  17.     scanf("%d",&B.REAL);
  18.     printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR B :");
  19.     scanf("%d",&B.IMG);
  20.     C=multi_complex(A,B);
  21.     printf("\nRESULT FOR COMPLEX NO. C VALUES ARE :\n");
  22.     printf("(%d + %di) * (%d + %di) = %d + %di",A.REAL,A.IMG,\
  23.     B.REAL,B.IMG,C.REAL,C.IMG);
  24.     getch();
  25. }
  26. struct COMPLEX multi_complex(struct COMPLEX A,struct COMPLEX B)
  27. {
  28.     struct COMPLEX R;
  29.     R.REAL=(A.REAL*B.REAL)-(A.IMG*B.IMG);
  30.     R.IMG=(A.REAL*B.IMG)+(A.IMG*B.REAL);
  31.     return R;
  32. }

SCREEN SHOTS:-

C_program_Complex_Function_Multiply

C_program_Complex_Function_Multiply_Output

A C program to process the students evolution records using structures

Let’s identify variables needed for the structure inside the program.

In this student structure, we need several variables to store string as name and number for enrollment number and marks of student appeared in the term end examination.

First variable will be the one which will save the enrollment number and it will be ENROL. In C language we do not have string as variable so we have to use character array for the NAME to save the name of the student. Next are the marks of the student MCS011, MCS012, MCS013, MCS014, MCS015, MCS016, MCSL017 for the term end examination, We can use array for marks but i prefer different variables for the same, So in all Nine variables.
The identified variables are ENROL, NAME, MCS011, MCS012, MCS013, MCS014, MCS015, MCS016, MCSL017.

struct student
{
    unsigned long int ENROL;
    char NAME[15];
    int MCS011,MCS012,MCS013,MCS014,MCS015,MCS016,MCSL017;
}STUD[12]={
         {102038400,"GANESH",55,78,45,56,75,64,75},
         {102038401,"MAHESH",84,56,51,61,56,49,78},
         {102038402,"SURESH",51,95,16,18,91,19,51},
         {102038403,"KALPESH",87,98,74,54,15,61,64},
         {102038404,"RAHUL",74,65,45,41,55,56,54},
         {102038405,"SUBBU",19,84,68,67,45,64,65},
         {102038406,"RAKESH",55,78,45,56,75,64,75},
         {102038407,"ATUL",55,78,45,56,75,64,75},
         {102038408,"DHARMESH",55,78,45,56,75,64,75},
         {102038409,"AJAY",55,78,45,56,75,64,75},
         {102038410,"ABDUL",55,78,45,56,75,64,75},
         {102038411,"RASHMI",55,78,45,56,75,64,75}
     };

Now, Selection of data type will be int data type due to the values expected for enrollment number are decimals and they will be holding nine digit values i.e. 102038401 so int data type is not sufficient for ENROL_NO So to increase the capacity Unsigned for positive number and long modifier for double the storage size. Selection of data type will be int data type due to the values expected for marks are decimals and they will be holding smaller values 0 to 100, So int data type is sufficient for MCS011, MCS012, MCS013, MCS014, MCS015, MCS016, MCSL017. Selection of data type will be char data type due to string is a character array and we don’t have any string data type so char data type is selected for NAME.

Structure of student are initialized as database of the program with 12 records.

Let’s identify variables needed for this program.

In this program, we need only one variable to store enrollment number of the student entered by the user.

Only variable will be the one which will save the enrollment number entered by the user and it will be ENROL_NO so in all One variable.
The identified variables are ENROL_NO data type will be same as above.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

    unsigned long int ENROL_NO;
    void gen_result(unsigned long int);

In this program, Give message to user to enter the required enrollment number and scan the enter number i.e. ENROL . Variable ENROL is used for CHECK the range of enrollment numbers present and pass it  to the function call if number is within the range else print the appropriate message. Finally printing of the result screen will be done in the function of the program.

#include<stdio.h>
void main()
{
    unsigned long int ENROL_NO;
    void gen_result(unsigned long int);
    clrscr();
    printf("ENTER THE EMPLOYEE NO TO GENERATE PAYSLIP : ");
    scanf("%ld",&ENROL_NO);
    if(ENROL_NO>102038399 && ENROL_NO<102038412)
        gen_result(ENROL_NO);
    else
        printf("\nYOU HAVE ENTERED WRONG ENROLMENT NO. !!");
    getch();
}

Let’s identify variables needed for function which is part of program outside main function.
Variable which are passed in the function may not be declared again which is  character pointer ENROL_NOENROL is passed to the function and saved in ENROL_NO variable of the function. Now the variable STATUS which will hold the status value to be printed as S or N. SC or NC are used as status as SUCCESSFULLY COMPLETED or NOT COMPLETED respectively in the FUNCTION. In SC and NC we have C common and S and N different so STATUS Variable will have to hold only one character so we use char as data type.  The identified variable is STATUS.

void gen_result(unsigned long int ENROL)
{
 char STATUS;
 printf("\n\t\t\tINDIRA GANDHI NATIONAL OPEN UNIVERSITY");
 printf("\n\t\t\tTERM-END EXAMINATION (DEC - 2013)");
 printf("\n\n\tENROLMENT NO.\t: %ld",ENROL);
 printf("\n\tNAME\t\t: %s",STUD[ENROL-102038400].NAME);
 printf("\n\tPROGRAMME CODE \t: MCA");
 printf("\n\t_______________________________________________________________");
 printf("\n\tCOURSE\t\tTERM END\tTERM END \t");
 printf("\n\t CODE \t\t THEORY \tPRACTICAL\tSTATUS");
 printf("\n\t_______________________________________________________________");
 if(STUD[ENROL-102038400].MCS011<40) STATUS='N'; else STATUS='S';
 printf("\n\tMCS011\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS011,STATUS);
 if(STUD[ENROL-102038400].MCS012<40) STATUS='N'; else STATUS='S';
 printf("\n\n\tMCS012\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS012,STATUS);
 if(STUD[ENROL-102038400].MCS013<40) STATUS='N'; else STATUS='S';
 printf("\n\n\tMCS013\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS013,STATUS);
 if(STUD[ENROL-102038400].MCS014<40) STATUS='N'; else STATUS='S';
 printf("\n\n\tMCS014\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS014,STATUS);
 if(STUD[ENROL-102038400].MCS015<40) STATUS='N'; else STATUS='S';
 printf("\n\n\tMCS015\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS015,STATUS);
 if(STUD[ENROL-102038400].MCS016<40) STATUS='N'; else STATUS='S';
 printf("\n\n\tMCS016\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS016,STATUS);
 if(STUD[ENROL-102038400].MCSL017<40) STATUS='N'; else STATUS='S';
 printf("\n\n\tMCSL017\t\t-\t\t%d\t\t%cC",STUD[ENROL-102038400].MCSL017,STATUS);
 printf("\n\t_______________________________________________________________");
 printf("\n\t\tSC :- SUCCESSFULL COMPLETED\tNC :- NOT COMPLETED");
}

In this program, we are passing only the enrollment number and the array of STUD which are the STRUCT STUDENT user defined data type and to find the index number of the array which is zero subtract 102038400 from enrollment number we will find the same. Access the student records in stud array by using dot operator to all the elements of structure student. print the record of the student by customizing the display by using \n new line character and \t tab character. also the status of SC and NC as the status depending upon the marks obtained. Less than 39 will be NC and else SC.

C program code :

  1. #include<stdio.h>
  2. struct student
  3. {
  4.     unsigned long int ENROL;
  5.     char NAME[15];
  6.     int MCS011,MCS012,MCS013,MCS014,MCS015,MCS016,MCSL017;
  7. }STUD[12]={
  8.          {102038400,"GANESH",55,78,45,56,75,64,75},
  9.          {102038401,"MAHESH",84,56,51,61,56,49,78},
  10.          {102038402,"SURESH",51,95,16,18,91,19,51},
  11.          {102038403,"KALPESH",87,98,74,54,15,61,64},
  12.          {102038404,"RAHUL",74,65,45,41,55,56,54},
  13.          {102038405,"SUBBU",19,84,68,67,45,64,65},
  14.          {102038406,"RAKESH",55,78,45,56,75,64,75},
  15.          {102038407,"ATUL",55,78,45,56,75,64,75},
  16.          {102038408,"DHARMESH",55,78,45,56,75,64,75},
  17.          {102038409,"AJAY",55,78,45,56,75,64,75},
  18.          {102038410,"ABDUL",55,78,45,56,75,64,75},
  19.          {102038411,"RASHMI",55,78,45,56,75,64,75}
  20.      };
  21. void main()
  22. {
  23.  unsigned long int ENROL_NO;
  24.     void gen_result(unsigned long int);
  25.     clrscr();
  26.     printf("ENTER THE EMPLOYEE NO TO GENERATE PAYSLIP : ");
  27.     scanf("%ld",&ENROL_NO);
  28.     if(ENROL_NO>102038399 && ENROL_NO<102038412)
  29.         gen_result(ENROL_NO);
  30.     else
  31.         printf("\nYOU HAVE ENTERED WRONG ENROLMENT NO. !!");
  32.     getch();
  33. }
  34. void gen_result(unsigned long int ENROL)
  35. {
  36.  char STATUS;
  37.  printf("\n\t\t\tINDIRA GANDHI NATIONAL OPEN UNIVERSITY");
  38.  printf("\n\t\t\tTERM-END EXAMINATION (DEC - 2013)");
  39.  printf("\n\n\tENROLMENT NO.\t: %ld",ENROL);
  40.  printf("\n\tNAME\t\t: %s",STUD[ENROL-102038400].NAME);
  41.  printf("\n\tPROGRAMME CODE \t: MCA");
  42.  printf("\n\t_______________________________________________________________");
  43.  printf("\n\tCOURSE\t\tTERM END\tTERM END \t");
  44.  printf("\n\t CODE \t\t THEORY \tPRACTICAL\tSTATUS");
  45.  printf("\n\t_______________________________________________________________");
  46.  if(STUD[ENROL-102038400].MCS011<40) STATUS='N'; else STATUS='S';
  47.  printf("\n\tMCS011\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS011,STATUS);
  48.  if(STUD[ENROL-102038400].MCS012<40) STATUS='N'; else STATUS='S';
  49.  printf("\n\n\tMCS012\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS012,STATUS);
  50.  if(STUD[ENROL-102038400].MCS013<40) STATUS='N'; else STATUS='S';
  51.  printf("\n\n\tMCS013\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS013,STATUS);
  52.  if(STUD[ENROL-102038400].MCS014<40) STATUS='N'; else STATUS='S';
  53.  printf("\n\n\tMCS014\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS014,STATUS);
  54.  if(STUD[ENROL-102038400].MCS015<40) STATUS='N'; else STATUS='S';
  55.  printf("\n\n\tMCS015\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS015,STATUS);
  56.  if(STUD[ENROL-102038400].MCS016<40) STATUS='N'; else STATUS='S';
  57.  printf("\n\n\tMCS016\t\t%d\t\t-\t\t%cC",STUD[ENROL-102038400].MCS016,STATUS);
  58.  if(STUD[ENROL-102038400].MCSL017<40) STATUS='N'; else STATUS='S';
  59.  printf("\n\n\tMCSL017\t\t-\t\t%d\t\t%cC",STUD[ENROL-102038400].MCSL017,STATUS);
  60.  printf("\n\t_______________________________________________________________");
  61.  printf("\n\t\tSC :- SUCCESSFULL COMPLETED\tNC :- NOT COMPLETED");
  62. }

SCREEN SHOTS:-

C_program_Students_Evaluation

C_program_Students_Evaluation_Output

A C program to input a string and output the reversed string using pointer notation

Let’s identify variables needed for this program.

In this program, we need several variables to store string and count the number of characters in the given string.

First variables will be the one which will save the string entered by the user and reverse of it. So it will be S[50] and R[50]. In C language we do not have string as variable so we have to use character array for the same. Next is a pointer which will hold the address of S[50] and character pointers will be needed to point to the character arrays So we need *PTR1  and *PTR2. Next we will need a LEN variable to store Length of a characters in a string so in all Five variables.
The identified variables are S[50], R[50],*PTR1, *PTR2, LEN.

char *PTR1,*PTR2,S[50],R[50];
 int LEN=0;

Now, Selection of data type will be int data type due to the values expected for counting are decimals and they will be holding smaller values so int data type is sufficient for LEN. Selection of data type will be char data type due to string is a character array and we don’t have any string data type so char data type is selected for S[50], R[50] and *PTR1, *PTR2 pointers are always of same data type.

In this program, Give message to user to enter the required string and scan the enter character is the character array i.e. S[50]. The base address of the string S[50], R[50] is assigned to the character pointers *PTR1, *PTR2 respectively. In While loop we will use *PTR1 it will return true if any character is present in it or return false if any character is absent in it. Now pointer *PTR1 which will be used for moving from start to end of the string given within the loop pointer is incremented and so is LEN which will count the character in the string and used to reverse the string by adding it to the second pointer *PTR2 .

Main points to take care are all the strings are terminated by a NULL value at the end of the string and indexing of the string starts from zero so when we add the Length to the second pointer it is after the characters are finished, Hence we are assigning NULL to that position. Another point is that we have already moved the first pointer so assigned the address of S[50] to it again else no output will be sent to second string. So we are incrementing second pointer and decrementing first pointer.

#include<stdio.h>
void main()
{
 char *PTR1,*PTR2,S[50],R[50];
 int LEN=0;
 clrscr();
 printf("\nENTER THE STRING TO BE REVERSED :- ");
 scanf("%s",&S);
 PTR1=&S;
 PTR2=&R;
 while(*PTR1)
     {
      LEN++;
      PTR1++;
     }
 printf("%d",LEN);
 PTR1=&S;
 PTR2=PTR2+LEN;
 *PTR2=NULL;
 PTR2--;
 while(*PTR1)
     {
      *PTR2=*PTR1;
      PTR1++;
      PTR2--;
     }
 printf("\nTHE REVERSED STRING IS :- %s",R);
 getch();
}

Finally print the Length and the reversed String at the end of the program.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.  char *PTR1,*PTR2,S[50],R[50];
  5.  int I,LEN=0;
  6.  clrscr();
  7.  printf("\nENTER THE STRING TO BE REVERSED :- ");
  8.  scanf("%s",&S);
  9.  PTR1=&S;
  10.  PTR2=&R;
  11.  while(*PTR1)
  12.      {
  13.       LEN++;
  14.       PTR1++;
  15.      }
  16.  printf("%d",LEN);
  17.  PTR1=&S;
  18.  PTR2=PTR2+LEN;
  19.  *PTR2=NULL;
  20.  PTR2--;
  21.  while(*PTR1)
  22.      {
  23.       *PTR2=*PTR1;
  24.       PTR1++;
  25.       PTR2--;
  26.      }
  27.  printf("\nTHE REVERSED STRING IS :- %s",R);
  28.  getch();
  29. }

SCREEN SHOTS:-

C_program_String_Reverse_Pointer

C_program_String_Reverse_Pointer_Output

A C program with a function that will return the length of a character string without using inbuilt function

Let’s identify variables needed for this program.

In this program, we need several variables to store string and count the number of characters in the given string.

First variable will be the one which will save the string entered by the user and it will be S[50]. In C language we do not have string as variable so we have to use character array for the same. Next is a pointer which will hold the address of S[50] and that is used to pass the character pointer but S itself is a pointer so no need to take any pointer. Next we will need a LEN variable to store Length of a characters in a string returned by the function so in all Two variables.
The identified variables are S[50],LEN.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

int string_length(char*);
 char S[50];
 int LEN;

 

Now, Selection of data type will be int data type due to the values expected for counting are decimals and they will be holding smaller values so int data type is sufficient for LEN. Selection of data type will be char data type due to string is a character array and we don’t have any string data type so char data type is selected for S[50].

In this program, Give message to user to enter the required string and scan the enter character is the character array i.e. S[50]. Variable LEN is used for storing Length is assigned to the function call which will return integer value. Finally print the integer as Length of String entered by the user at the end of the program.

#include<stdio.h>
void main()
{
 int string_length(char*);
 char S[50];
 int LEN;
 clrscr();
 printf("\nENTER STRING TO FIND IT'S LENGTH : ");
 scanf("%s",&S);
 LEN=string_length(S);
 printf("\nLENGTH OF STRING IS :- %d",LEN);
 getch();
}

Let’s identify variables needed for function which is part of program outside main function.
Variables which are passed in the function may not be declared again which is  character pointer *STRS is passed to the function and saved in *STR variable of the function. Now the variable LEN which will hold the resultant value to be returned by the FUNCTION. Variable.  The identified variable is LEN.

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

int string_length(char *STR)
{
     int L=0;
     while(*STR)
     {
      L++;
      STR++;
     }
     return L;
}

In this program, we should not use inbuilt function so we are writing it in new function with new name. The base address of the string is passed to the function character pointer return true if any character is present in it or return false if any character is absent in it. Now pointer *STR which will be used for moving from start to end of the string given within the loop pointer is incremented and so is L which will count the character in the string and return length as result.

Note:- Remember whenever you are calling a function which is returning a value it should always be assigned to a variable of same data type or called inside the printf function as variable.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.  int string_length(char*);
  5.  char S[50];
  6.  int LEN;
  7.  clrscr();
  8.  printf("\nENTER STRING TO FIND IT'S LENGTH : ");
  9.  scanf("%s",&S);
  10.  LEN=string_length(S);
  11.  printf("\nLENGTH OF STRING IS :- %d",LEN);
  12.  getch();
  13. }
  14. int string_length(char *STR)
  15. {
  16.      int L=0;
  17.      while(*STR)
  18.      {
  19.       L++;
  20.       STR++;
  21.      }
  22.      return L;
  23. }
  24.      }

SCREEN SHOTS:-

C_program_String_Length_Function

C_program_String_Length_Function_Output

A C program to count number of vowels, consonants & spaces in a given string

Let’s identify variables needed for this program.

In this program, we need several variables to store large sentence and count the number of occurrences in the given sentence.

First variable will be the one which will save the sentence given and it will be S[]. Keeping the square bracket blank means it will take the size automatically against the initialized value, But we can not keep the brackets blank without assignment of values to any kind of array form. Next is a pointer which will hold the address of S[]  and that will be *PTR which will be used for moving from start to end of the string given and SP,VO,CO will be used to store the total count of SPACES, VOWELS, CONSONANTS respectively so in all Five variables.
The identified variables are S[],*PTR,SP,VO,CO.

Now, Selection of data type will be int data type due to the values expected for counting are decimals and they will be holding smaller values so int data type is sufficient for SP,VO,CO. Selection of data type will be char data type due to string or sentence is a character array and we don’t have any string data type so char data type is selected for S[] and *PTR pointers are always of same data type.

In this program variables used for storing counts is initialized to zero. As we all now the word is differentiated by a space and vowels are all the words starting with A,E,I,O,U  and all other are consonants.

Here we are checking for space incrementing SP i.e. counting space, then if any word after space is A or E or I or O or U, if yes incrementing VO i.e. counting vowel and else incrementing CO i.e. counting consonants.

SP=VO=CO=0;
 while(*PTR)
     {
      if(*PTR==' ')
      {
         SP++;
 
         if(*(PTR+1)=='a'||*(PTR+1)=='A'||\
            *(PTR+1)=='e'||*(PTR+1)=='E'||\
            *(PTR+1)=='i'||*(PTR+1)=='I'||\
            *(PTR+1)=='o'||*(PTR+1)=='O'||\
            *(PTR+1)=='u'||*(PTR+1)=='U')
 
        VO++;
         else
        CO++;
      }
 
       PTR++;
     }

Finally print the Numbers as counts of Vowels, Consonants and Spaces at the end of the program.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.  char *PTR,S[]="When it HURTS to look back, and \
  5.   you're SCARED to look ahead, you can look beside\
  6.   you and your BEST FRIEND will be there";
  7.  int SP,VO,CO;
  8.  clrscr();
  9.  printf("\nTHE GIVEN STRING IS :-\n\t  %s\n",S);
  10.  PTR=&S;
  11.  SP=VO=CO=0;
  12.  while(*PTR)
  13.      {
  14.       if(*PTR==' ')
  15.       {
  16.          SP++;
  17.  
  18.          if(*(PTR+1)=='a'||*(PTR+1)=='A'||\
  19.          *(PTR+1)=='e'||*(PTR+1)=='E'||\
  20.          *(PTR+1)=='i'||*(PTR+1)=='I'||\
  21.          *(PTR+1)=='o'||*(PTR+1)=='O'||\
  22.          *(PTR+1)=='u'||*(PTR+1)=='U')
  23.  
  24.         VO++;
  25.          else
  26.         CO++;
  27.       }
  28.  
  29.        PTR++;
  30.      }

SCREEN SHOTS:-

C_program_Space_Vowel_Consonant

C_program_Space_Vowel_Consonant_Output

A C program to convert a given lowercase string to uppercase string without using the inbuilt string function

Explanation of FUNCTION:

A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required.

Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by itself. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind.

The name of the function is unique in a C Program and is Global. It means that a function can be accessed from any location within a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.

We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

void lwr_to_upr(char*);

Function returns value and the ones which does not return any value is called and declared as void. This function which we are writing returns the gcd in decimal form as output. The values inside the round brackets are called Arguments. The Argument and return type are integer type. A function can return only one value so we have to take only one data type in which its value is expected.

Let’s identify variables needed for this program.
First variables will be the one which will save the value entered by the user S[50]. The identified variables are S[50].

Now, Selection of data type is char array data type due to the values expected are string and they will be holding value as char array data type.

Main program needs only declaration of variables and function prototype. Also messages for user and scanning string  S[50] print the message and call the Lwr_to_Upr FUNCTION which will convert the characters within the variable itself. Their is no single value to be returned hence the return value will be void.

#include<stdio.h>
void main()
{
 void lwr_to_upr(char*);
 char S[50];
 clrscr();
 printf("\nENTER STRING TO CONVERT TO UPPERCASE : ");
 scanf("%s",&S);
 lwr_to_upr(S);
 printf("\nCONVERTED UPPERCASE STRING IS :- %s",S);
 getch();
}

Let’s identify variables needed for function which is part of program outside main function.
their are no variable of the function, since character pointer is passed to the function.

In this program, we should not use inbuilt function so we are writing it in new function with new name. The base address of the string is passed to the function character pointer return true if any character is present in it or return false if any character is absent in it. check whether the character lies between a to z if yes than convert it to lowercase to uppercase. The can be down by deducting 32 to the character. see the ascii table to find out ascii number.

void lwr_to_upr(char *STR)
{
     while(*STR)
     {
      if(*STR>='a' && *STR<='z')
        *STR=*STR-32;
      STR++;
     }
}

Note:- Remember whenever you are calling a function which is returning a value it should always be assigned to a variable of same data type or called inside the printf function as variable or if void function can be called directly since return value is void (nothing).

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.  void lwr_to_upr(char*);
  5.  char S[50];
  6.  clrscr();
  7.  printf("\nENTER STRING TO CONVERT TO UPPERCASE : ");
  8.  scanf("%s",&S);
  9.  lwr_to_upr(S);
  10.  printf("\nCONVERTED UPPERCASE STRING IS :- %s",S);
  11.  getch();
  12. }
  13. void lwr_to_upr(char *STR)
  14. {
  15.      while(*STR)
  16.      {
  17.       if(*STR>='a' && *STR<='z')
  18.         *STR=*STR-32;
  19.       STR++;
  20.      }
  21. }

Screen Shots :-

C_program_Lower_to_Upper

C_program_Lower_to_Upper_Output

A C program to reverse the given string using recursion

Explanation of FUNCTION:

A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required.

Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by itself. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind.

The name of the function is unique in a C Program and is Global. It means that a function can be accessed from any location within a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.

We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind.

 

Recursive function is a function which calls itself. Calling a function within itself makes it a endless loop. So we need to take care that there must be a termination condition in every recursive function.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

void reverse_rec(char *STR);

Function returns value and the ones which does not return any value is called and declared as void. This function which we are writing returns the gcd in decimal form as output. The values inside the round brackets are called Arguments. The Argument and return type are integer type. A function can return only one value so we have to take only one data type in which its value is expected.

Let’s identify variables needed for this program.
First variables will be the one which will save the value entered by the user S[50]. The identified variables are S[50].

Now, Selection of data type is char array data type due to the values expected are string and they will be holding value as char array data type.

Main program needs only declaration of variables and function prototype. Also messages for user and scanning string  S[50] print the message and call the Reverse FUNCTION which will print the characters within the function call itself. Their is no single value to be returned hence the return value will be void.

#include<stdio.h>
void main()
{
 void reverse_rec(char*);
 int S[50];
 printf("\nENTER STRING TO REVERSE : ");
 scanf("%s",&S);
 printf("\nREVERSED STRING IS :- ");
 reverse_rec(S);
 getch();
}

Let’s identify variables needed for function which is part of program outside main function.
their are no variable of the function.

In this program there is a risk of generating an ENDLESS LOOP. Here we have to Check the TERMINATION condition is given or not. If condition will have character pointer will be true till the character is present and when the character is NULL it will change to false. In beginning print a character will print the straight order and to print reverse order we have to print it after function call.

void reverse_rec(char *STR)
{
     if(*STR)
     {
      reverse_rec(STR+1);
      printf("%c",*STR);
     }
}

Note:- Remember whenever you are calling a function which is returning a value it should always be assigned to a variable of same data type or called inside the printf function as variable.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.  void reverse_rec(char*);
  5.  int S[50];
  6.  printf("\nENTER STRING TO REVERSE : ");
  7.  scanf("%s",&S);
  8.  printf("\nREVERSED STRING IS :- ");
  9.  reverse_rec(S);
  10.  getch();
  11. }
  12. void reverse_rec(char *STR)
  13. {
  14.      if(*STR)
  15.      {
  16.       reverse_rec(STR+1);
  17.       printf("%c",*STR);
  18.      }
  19. }

Screen Shots :-

C_program_Reverse_Recursion

C_program_Reverse_Recursion_Output