Monthly Archives: September 2013


A C program to covert decimal to binary, octal & Hexadecimal – IGNOU MCA Assignment 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/13
Assignment 2013

 

A C program that takes a decimal number and converts it into binary, octal and hexadecimal equivalents. Your program should have functions for each type of conversion. These functions should implement algorithms to perform these conversions. Predefined functions OR %x and %o codes are not allowed. The output of the programme should be stored in a text file named output.txt.

 

#include<stdio.h>
FILE *FP=NULL;
void main()
{
     int CH,NUM;
     void octal(int);
     void binary(int);
     void hexa(int);
     clrscr();
     printf("ENTER DECIMAL NUMBER TO BE CONVERTED : \n");
     scanf("%d",&NUM);
     printf("\nSELECT CONVERSION");
     printf("\n 1. DECIMAL TO BINARY\n");
     printf("\n 2. DECIMAL TO OCTAL\n");
     printf("\n 3. DECIMAL TO HEXADECIMAL\n");
     printf("\nENTER CHOICE HERE :");
     scanf("%d",&CH);
     switch(CH)
     {
       case 1 : binary(NUM);
        printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
        break;
       case 2 : octal(NUM);
        printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
        break;
       case 3 : hexa(NUM);
        printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
        break;
       default :   printf("\nYOU HAVE ENTERED WRONG CHOICE !!!");
}

  getch();
}
void hexa(int Y)
{
     char HEXC[5];
     int NUM,I,LEN,HEXD[5];
     NUM=Y;
     LEN=0;
     while(Y>0)
     {
    HEXD[LEN]=Y%16;
    Y=Y/16;
    LEN++;
     };
     for(I=LEN-1;I>-1;I--)
     {
    if(HEXD[I]<10)
       HEXC[I]=HEXD[I]+48;
    else
       HEXC[I]=HEXD[I]+55;
     }
     if((FP=fopen("output.txt","a+t"))==NULL)
     {
    printf("FILE CAN'T BE OPENED OR CREATED\n");
    exit(0);
     }
     fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);
     for(I=LEN-1;I>-1;I--)
     {
    fprintf(FP,"%c",HEXC[I]);
     }

}
void binary(int Y)
{
     int NUM,I,LEN,BIN[20];
     NUM=Y;
     LEN=0;
     while(Y>0)
     {
    BIN[LEN]=Y%2;
    Y=Y/2;
    LEN++;
     };
     if((FP=fopen("output.txt","a+t"))==NULL)
     {
    printf("FILE CAN'T BE OPENED OR CREATED\n");
    exit(0);
     }
     fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);
     for(I=LEN-1;I>-1;I--)
     {
    fprintf(FP,"%d",BIN[I]);
     }

}
void octal(int Y)
{
     int NUM,I,LEN,OCT[5];
     NUM=Y;
     LEN=0;
     while(Y>0)
     {
    OCT[LEN]=Y%8;
    Y=Y/8;
    LEN++;
     };
     if((FP=fopen("output.txt","a+t"))==NULL)
     {
    printf("FILE CAN'T BE OPENED OR CREATED\n");
    exit(0);
     }
     fprintf(FP,"\nCONVERTED OCTAL EQUIVALENT VALUE OF %d IS \t",NUM);
     for(I=LEN-1;I>-1;I--)
     {
    fprintf(FP,"%d",OCT[I]);
     }
}

CODE : -

  1. #include<stdio.h>
  2. FILE *FP=NULL;
  3. void main()
  4. {
  5. int CH,NUM;
  6. void octal(int);
  7. void binary(int);
  8. void hexa(int);
  9. clrscr();
  10. printf("ENTER DECIMAL NUMBER TO BE CONVERTED : \n");
  11. scanf("%d",&NUM);
  12. printf("\nSELECT CONVERSION");
  13. printf("\n 1. DECIMAL TO BINARY\n");
  14. printf("\n 2. DECIMAL TO OCTAL\n");
  15. printf("\n 3. DECIMAL TO HEXADECIMAL\n");
  16. printf("\nENTER CHOICE HERE :");
  17. scanf("%d",&CH);
  18. switch(CH)
  19. {
  20. case 1 : binary(NUM);
  21. printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
  22. break;
  23. case 2 : octal(NUM);
  24. printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
  25. break;
  26. case 3 : hexa(NUM);
  27. printf("\nOUPUT WRITTEN TO OUTPUT.TXT");
  28. break;
  29. default : printf("\nYOU HAVE ENTERED WRONG CHOICE !!!");
  30. }
  31.  
  32. getch();
  33. }
  34. void hexa(int Y)
  35. {
  36. char HEXC[5];
  37. int NUM,I,LEN,HEXD[5];
  38. NUM=Y;
  39. LEN=0;
  40. while(Y>0)
  41. {
  42. HEXD[LEN]=Y%16;
  43. Y=Y/16;
  44. LEN++;
  45. };
  46. for(I=LEN-1;I>-1;I--)
  47. {
  48. if(HEXD[I]<10)
  49. HEXC[I]=HEXD[I]+48;
  50. else
  51. HEXC[I]=HEXD[I]+55;
  52. }
  53. if((FP=fopen("output.txt","a+t"))==NULL)
  54. {
  55. printf("FILE CAN'T BE OPENED OR CREATED\n");
  56. exit(0);
  57. }
  58. fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);
  59. for(I=LEN-1;I>-1;I--)
  60. {
  61. fprintf(FP,"%c",HEXC[I]);
  62. }
  63.  
  64. }
  65. void binary(int Y)
  66. {
  67. int NUM,I,LEN,BIN[20];
  68. NUM=Y;
  69. LEN=0;
  70. while(Y>0)
  71. {
  72. BIN[LEN]=Y%2;
  73. Y=Y/2;
  74. LEN++;
  75. };
  76. if((FP=fopen("output.txt","a+t"))==NULL)
  77. {
  78. printf("FILE CAN'T BE OPENED OR CREATED\n");
  79. exit(0);
  80. }
  81. fprintf(FP,"\nCONVERTED BINARY EQUIVALENT VALUE OF %d IS \t",NUM);
  82. for(I=LEN-1;I>-1;I--)
  83. {
  84. fprintf(FP,"%d",BIN[I]);
  85. }
  86.  
  87. }
  88. void octal(int Y)
  89. {
  90. int NUM,I,LEN,OCT[5];
  91. NUM=Y;
  92. LEN=0;
  93. while(Y>0)
  94. {
  95. OCT[LEN]=Y%8;
  96. Y=Y/8;
  97. LEN++;
  98. };
  99. if((FP=fopen("output.txt","a+t"))==NULL)
  100. {
  101. printf("FILE CAN'T BE OPENED OR CREATED\n");
  102. exit(0);
  103. }
  104. fprintf(FP,"\nCONVERTED OCTAL EQUIVALENT VALUE OF %d IS \t",NUM);
  105. for(I=LEN-1;I>-1;I--)
  106. {
  107. fprintf(FP,"%d",OCT[I]);
  108. }
  109. }

SCREEN SHOTS : -

MCS017_Q1

MCS017_Q1_Output

MCS017_Q1_Output_txt

A C program to generate Salary Pay Slip for Staff – IGNOU MCA Assignment 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/13
Assignment 2013 

 

An interactive C program to generate pay slips for the staff of size 12 employees (2 members are clerks, one computer operator, 6 salesmen, 3 helpers) , working in a small chemist retail shop.

 

#include<stdio.h>
#include<dos.h>
struct employee
{
    int NO;
    char NAME[10];
    int DESIGN_CODE;
    int DAYS_WORKED;
}EMP[12]={
         {1,"GANESH",1,25},
         {2,"MAHESH",1,30},
         {3,"SURESH",2,28},
         {4,"KALPESH",2,26},
         {5,"RAHUL",2,24},
         {6,"SUBBU",2,25},
         {7,"RAKESH",2,23},
         {8,"ATUL",2,22},
         {9,"DHARMESH",3,26},
         {10,"AJAY",3,26},
         {11,"ABDUL",3,27},
         {12,"RASHMI",4,29}
     };
void main()
{

    int EMPNO;
    void gen_payslip(int);
    clrscr();

    printf("ENTER THE EMPLOYEE NO TO GENERATE PAYSLIP : ");
    scanf("%d",&EMPNO);
    if(EMPNO>0 && EMPNO<13)
        gen_payslip(EMPNO);
    else
        printf("\nYOU HAVE ENTERED WRONG EMP NO. !!");
    getch();
}
void gen_payslip(int EMPNO)
{
 struct date D;
 char DESG[10];
 float NETPAY,BASIC,PF,PAYRATE,PTAX=200;
 getdate(&D);
 printf("\n\n\t\t\tSHREE KRISHNA CHEMISTS AND DRUGGIST");
 printf("\n\t\t\t\tSALARY MONTH %d %d\n",D.da_mon,D.da_year);
 printf("\n\n\tEMP.NO.: %d\t\tEMP.NAME: %s",EMPNO,EMP[EMPNO-1].NAME);
 switch(EMP[EMPNO-1].DESIGN_CODE)
 {
   case 1: PAYRATE=400;
       printf("\tDESIGNATION: CLERK");
       break;
   case 2: PAYRATE=300;
       printf("\tDESIGNATION: SALESMEN");
       break;
   case 3: PAYRATE=250;
       printf("\tDESIGNATION: HELPER");
       break;
   case 4: PAYRATE=350;
       printf("\tDESIGNATION: COMP.OPTR");
       break;
 }
 BASIC=PAYRATE*EMP[EMPNO-1].DAYS_WORKED;
 PF=BASIC/10;
 printf("\n\n\tDAYS WORKED: %d",EMP[EMPNO-1].DAYS_WORKED);
 printf("\t\tPAY RATE: %.0f\t\tGEN.DATE:%d/%d/%d ",PAYRATE,D.da_day,D.da_mon,D.da_year);
 printf("\n\t______________________________________________________________________");
 printf("\n\n\tEARNINGS\tAMOUNT(RS.)\t\tDEDUCTIONS\tAMOUNT(RS.)");
 printf("\n\t______________________________________________________________________");
 printf("\n\n\tBASIC PAY\t%.0f\t\t\tP.F.\t\t%.0f",BASIC,PF);
 printf("\n\n\t\t\t\t\t\tPROF.TAX\t%.0f",PTAX);
 printf("\n\n\t______________________________________________________________________");
 printf("\n\n\tGROSS EARN.\t%.0f\t\t\tTOTAL DEDUCT.\t%.0f",BASIC,PF+PTAX);
 NETPAY=BASIC-(PF+PTAX);
 printf("\n\t\t\t\t\t\tNET PAY\t\t%.0f",NETPAY);
 printf("\n\t______________________________________________________________________");

}

Code: -

  1. #include<stdio.h>
  2. #include<dos.h>
  3. struct employee
  4. {
  5.     int NO;
  6.     char NAME[10];
  7.     int DESIGN_CODE;
  8.     int DAYS_WORKED;
  9. }EMP[12]={
  10.          {1,"GANESH",1,25},
  11.          {2,"MAHESH",1,30},
  12.          {3,"SURESH",2,28},
  13.          {4,"KALPESH",2,26},
  14.          {5,"RAHUL",2,24},
  15.          {6,"SUBBU",2,25},
  16.          {7,"RAKESH",2,23},
  17.          {8,"ATUL",2,22},
  18.          {9,"DHARMESH",3,26},
  19.          {10,"AJAY",3,26},
  20.          {11,"ABDUL",3,27},
  21.          {12,"RASHMI",4,29}
  22.      };
  23. void main()
  24. {
  25.  
  26.     int EMPNO;
  27.     void gen_payslip(int);
  28.     clrscr();
  29.  
  30.     printf("ENTER THE EMPLOYEE NO TO GENERATE PAYSLIP : ");
  31.     scanf("%d",&EMPNO);
  32.     if(EMPNO>0 && EMPNO<13)
  33.         gen_payslip(EMPNO);
  34.     else
  35.         printf("\nYOU HAVE ENTERED WRONG EMP NO. !!");
  36.     getch();
  37. }
  38. void gen_payslip(int EMPNO)
  39. {
  40.  struct date D;
  41.  char DESG[10];
  42.  float NETPAY,BASIC,PF,PAYRATE,PTAX=200;
  43.  getdate(&D);
  44.  printf("\n\n\t\t\tSHREE KRISHNA CHEMISTS AND DRUGGIST");
  45.  printf("\n\t\t\t\tSALARY MONTH %d %d\n",D.da_mon,D.da_year);
  46.  printf("\n\n\tEMP.NO.: %d\t\tEMP.NAME: %s",EMPNO,EMP[EMPNO-1].NAME);
  47.  switch(EMP[EMPNO-1].DESIGN_CODE)
  48.  {
  49.    case 1: PAYRATE=400;
  50.        printf("\tDESIGNATION: CLERK");
  51.        break;
  52.    case 2: PAYRATE=300;
  53.        printf("\tDESIGNATION: SALESMEN");
  54.        break;
  55.    case 3: PAYRATE=250;
  56.        printf("\tDESIGNATION: HELPER");
  57.        break;
  58.    case 4: PAYRATE=350;
  59.        printf("\tDESIGNATION: COMP.OPTR");
  60.        break;
  61.  }
  62.  BASIC=PAYRATE*EMP[EMPNO-1].DAYS_WORKED;
  63.  PF=BASIC/10;
  64.  printf("\n\n\tDAYS WORKED: %d",EMP[EMPNO-1].DAYS_WORKED);
  65.  printf("\t\tPAY RATE: %.0f\t\tGEN.DATE:%d/%d/%d ",PAYRATE,D.da_day,D.da_mon,D.da_year);
  66.  printf("\n\t______________________________________________________________________");
  67.  printf("\n\n\tEARNINGS\tAMOUNT(RS.)\t\tDEDUCTIONS\tAMOUNT(RS.)");
  68.  printf("\n\t______________________________________________________________________");
  69.  printf("\n\n\tBASIC PAY\t%.0f\t\t\tP.F.\t\t%.0f",BASIC,PF);
  70.  printf("\n\n\t\t\t\t\t\tPROF.TAX\t%.0f",PTAX);
  71.  printf("\n\n\t______________________________________________________________________");
  72.  printf("\n\n\tGROSS EARN.\t%.0f\t\t\tTOTAL DEDUCT.\t%.0f",BASIC,PF+PTAX);
  73.  NETPAY=BASIC-(PF+PTAX);
  74.  printf("\n\t\t\t\t\t\tNET PAY\t\t%.0f",NETPAY);
  75.  printf("\n\t______________________________________________________________________");
  76.  
  77. }

SCREEN SHOTS :-

MCS011_Q6

MCS011_Q6_Output

A C Program to used as Weight Converter – IGNOU MCA Assignment 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/13
Assignment 2013
 An interactive C program called “WEIGHT CONVERTER” that accepts the weight in milligrams / decigrams / centigrams / kilograms /ounces / pounds / tons and displays its equivalent in grams.

 

#include<stdio.h>
void main()
{
  int NUM;
  float WEIGHT,GRAMS;
  clrscr();
  printf("SELECT MEASURING MASS");
  printf("\n 1. MILLIGRAM\n");
  printf("\n 2. CENTIGRAM\n");
  printf("\n 3. DECIGRAM\n");
  printf("\n 4. KILOGRAM\n");
  printf("\n 5. MEGAGRAM(TON)\n");
  printf("\n 6. OUNCE\n");
  printf("\n 7. POUND\n");
  printf("ENTER CHOICE HERE :");
  scanf("%d",&NUM);
  switch(NUM)
  {
    case 1 :    printf("\n SELETED MILLIGRAMS TO GRAMS\n");
        break;
    case 2 :    printf("\n SELETED CENTIGRAMS TO GRAMS\n");
        break;
    case 3 :    printf("\n SELETED DECIGRAMS TO GRAMS\n");
        break;
    case 4 :    printf("\n SELETED KILOGRAMS TO GRAMS\n");
        break;
    case 5 :    printf("\n SELETED MEGAGRAMS(TONS) TO GRAMS\n");
        break;
    case 6 :    printf("\n SELETED OUNCES TO GRAMS\n");
        break;
    case 7 :    printf("\n SELETED POUNDS TO GRAMS\n");
        break;
    default :   printf("YOU HAVE ENTERED WRONG CHOICE !!!");
        goto QUIT;
  }
  printf("ENTER WEIGHT HERE :");
  scanf("%f",&WEIGHT);
  switch(NUM)
  {
    case 1 :    GRAMS=WEIGHT/1000;
        printf("\n %.2f MILLIGRAMS IS %.2f GRAMS\n",WEIGHT,GRAMS);
        break;
    case 2 :    GRAMS=WEIGHT/100;
        printf("\n %.4f CENTIGRAMS IS %.4f GRAMS\n",WEIGHT,GRAMS);
        break;
    case 3 :    GRAMS=WEIGHT/10;
        printf("\n %.4f DECIGRAMS IS %.4f GRAMS\n",WEIGHT,GRAMS);
        break;
    case 4 :    GRAMS=WEIGHT*1000;
        printf("\n %.4f KILOGRAMS IS %.4f GRAMS\n",WEIGHT,GRAMS);
        break;
    case 5 :    GRAMS=WEIGHT*1000000;
        printf("\n %.4f TONS IS %.4f GRAMS\n",WEIGHT,GRAMS);
        break;
    case 6 :    GRAMS=WEIGHT*2835/100;
        printf("\n %.4f OUNCES IS %.4f GRAMS\n",WEIGHT,GRAMS);
        break;
    case 7 :    GRAMS=WEIGHT*45359/100;
        printf("\n %.4f POUNDS IS %.4f GRAMS\n",WEIGHT,GRAMS);
        break;
  }
  QUIT:
    getch();
}

CODE : -

  1. #include<stdio.h>
  2. void main()
  3. {
  4.   int NUM;
  5.   float WEIGHT,GRAMS;
  6.   clrscr();
  7.   printf("SELECT MEASURING MASS");
  8.   printf("\n 1. MILLIGRAM\n");
  9.   printf("\n 2. CENTIGRAM\n");
  10.   printf("\n 3. DECIGRAM\n");
  11.   printf("\n 4. KILOGRAM\n");
  12.   printf("\n 5. MEGAGRAM(TON)\n");
  13.   printf("\n 6. OUNCE\n");
  14.   printf("\n 7. POUND\n");
  15.   printf("ENTER CHOICE HERE :");
  16.   scanf("%d",&NUM);
  17.   switch(NUM)
  18.   {
  19.     case 1 :    printf("\n SELETED MILLIGRAMS TO GRAMS\n");
  20.         break;
  21.     case 2 :    printf("\n SELETED CENTIGRAMS TO GRAMS\n");
  22.         break;
  23.     case 3 :    printf("\n SELETED DECIGRAMS TO GRAMS\n");
  24.         break;
  25.     case 4 :    printf("\n SELETED KILOGRAMS TO GRAMS\n");
  26.         break;
  27.     case 5 :    printf("\n SELETED MEGAGRAMS(TONS) TO GRAMS\n");
  28.         break;
  29.     case 6 :    printf("\n SELETED OUNCES TO GRAMS\n");
  30.         break;
  31.     case 7 :    printf("\n SELETED POUNDS TO GRAMS\n");
  32.         break;
  33.     default :   printf("YOU HAVE ENTERED WRONG CHOICE !!!");
  34.         goto QUIT;
  35.   }
  36.   printf("ENTER WEIGHT HERE :");
  37.   scanf("%f",&WEIGHT);
  38.   switch(NUM)
  39.   {
  40.     case 1 :    GRAMS=WEIGHT/1000;
  41.         printf("\n %.2f MILLIGRAMS IS %.2f GRAMS\n",WEIGHT,GRAMS);
  42.         break;
  43.     case 2 :    GRAMS=WEIGHT/100;
  44.         printf("\n %.4f CENTIGRAMS IS %.4f GRAMS\n",WEIGHT,GRAMS);
  45.         break;
  46.     case 3 :    GRAMS=WEIGHT/10;
  47.         printf("\n %.4f DECIGRAMS IS %.4f GRAMS\n",WEIGHT,GRAMS);
  48.         break;
  49.     case 4 :    GRAMS=WEIGHT*1000;
  50.         printf("\n %.4f KILOGRAMS IS %.4f GRAMS\n",WEIGHT,GRAMS);
  51.         break;
  52.     case 5 :    GRAMS=WEIGHT*1000000;
  53.         printf("\n %.4f TONS IS %.4f GRAMS\n",WEIGHT,GRAMS);
  54.         break;
  55.     case 6 :    GRAMS=WEIGHT*2835/100;
  56.         printf("\n %.4f OUNCES IS %.4f GRAMS\n",WEIGHT,GRAMS);
  57.         break;
  58.     case 7 :    GRAMS=WEIGHT*45359/100;
  59.         printf("\n %.4f POUNDS IS %.4f GRAMS\n",WEIGHT,GRAMS);
  60.         break;
  61.   }
  62.   QUIT:
  63.     getch();
  64. }

SCREEN SHOTS :-

MCS011_Q5

MCS011_Q5_Output

A C program to check whether the string is a palindrome or not, using pointers – IGNOU MCA Assignment 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/13
Assignment 2013 

 

 An interactive C program to check whether the given string is a palindrome or not, using pointers.

 

#include<stdio.h>
void main()
{
    int MID,FLAG,I,LEN=0;
    char *PTR1,*PTR2,S[50];
    clrscr();
    printf("TO CHECK PALINDROME ENTER STRING : ");
    scanf("%s",&S);
     while(S[LEN]!=NULL)
    {
       LEN++;
    };
    printf("\nTHE STRING LENGTH IS : %d",LEN);
    ;
    PTR1=&S;
    PTR2=PTR1+(LEN-1);
    MID=(LEN/2)+1;
    for(I=0;I<MID;I++)
    {
    if(*PTR1==*PTR2)
        FLAG=0;
    else
        FLAG=1;
    LEN--;
    I++;
    }
    if(FLAG==0)
    printf("\nTHE STRING IS A PALINDROME");
    else
    printf("\nTHE STRING IS NOT A PALINDROME");
    getch();
}

CODE : -

  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int MID,FLAG,I,LEN=0;
  5.     char *PTR1,*PTR2,S[50];
  6.     clrscr();
  7.     printf("TO CHECK PALINDROME ENTER STRING : ");
  8.     scanf("%s",&S);
  9.      while(S[LEN]!=NULL)
  10.     {
  11.        LEN++;
  12.     };
  13.     printf("\nTHE STRING LENGTH IS : %d",LEN);
  14.     ;
  15.     PTR1=&S;
  16.     PTR2=PTR1+(LEN-1);
  17.     MID=(LEN/2)+1;
  18.     for(I=0;I<MID;I++)
  19.     {
  20.     if(*PTR1==*PTR2)
  21.         FLAG=0;
  22.     else
  23.         FLAG=1;
  24.     LEN--;
  25.     I++;
  26.     }
  27.     if(FLAG==0)
  28.     printf("\nTHE STRING IS A PALINDROME");
  29.     else
  30.     printf("\nTHE STRING IS NOT A PALINDROME");
  31.     getch();
  32. }

SCREEN SHOTS :-

MCS011_Q4

MCS011_Q4_Output

A C Program to replace a character in a String Using Function – IGNOU MCA Assignment 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/13
Assignment 2013 

 

A C program function strreplace(s, chr, repl_chr) which will replace each occurrences of character chr with the character repl_chr in the string s. The function returns the number of replacements. Place the source code of this function in a file named strreplace.c

 

#include<stdio.h>
void main()
{
    int COUNT;
    char CHR,REPL_CHR,S[50];
    int strreplace(char S[],char CHR,char REPL_CHR);
    clrscr();
    printf("ENTER THE STRING : ");
    scanf("%s",&S);
    printf("\n\nENTER THE CHARACTER TO BE REPLACED :");
    scanf(" %c",&CHR);
    printf("\n\nENTER THE REPLACEMENT CHARACTER :");
    scanf(" %c",&REPL_CHR);
    COUNT=strreplace(S,CHR,REPL_CHR);
    printf("\n\nTHE NUMBER OF REPLACED CHARACTER ARE : %d",COUNT);
    printf("\n\nTHE REPLACED STRING IS : %s",S);
    getch();
}

int strreplace(char S[],char CHR,char REPL_CHR)
{
    int I,NUM=0;
    I=0;
    while(S[I]!=NULL)
    {
    if(S[I]==CHR)
    {
        S[I]=REPL_CHR;
        NUM++;
    }
    I++;
    }
    return NUM;
}

CODE : -

  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int COUNT;
  5.     char CHR,REPL_CHR,S[50];
  6.     int strreplace(char S[],char CHR,char REPL_CHR);
  7.     clrscr();
  8.     printf("ENTER THE STRING : ");
  9.     scanf("%s",&S);
  10.     printf("\n\nENTER THE CHARACTER TO BE REPLACED :");
  11.     scanf(" %c",&CHR);
  12.     printf("\n\nENTER THE REPLACEMENT CHARACTER :");
  13.     scanf(" %c",&REPL_CHR);
  14.     COUNT=strreplace(S,CHR,REPL_CHR);
  15.     printf("\n\nTHE NUMBER OF REPLACED CHARACTER ARE : %d",COUNT);
  16.     printf("\n\nTHE REPLACED STRING IS : %s",S);
  17.     getch();
  18. }
  19.  
  20. int strreplace(char S[],char CHR,char REPL_CHR)
  21. {
  22.     int I,NUM=0;
  23.     I=0;
  24.     while(S[I]!=NULL)
  25.     {
  26.     if(S[I]==CHR)
  27.     {
  28.         S[I]=REPL_CHR;
  29.         NUM++;
  30.     }
  31.     I++;
  32.     }
  33.     return NUM;
  34. }

SCREEN SHOTS :-

MCS011_Q3

MCS011_Q3_Output

A C Program to convert Binary to Octal number – IGNOU MCA Assignment 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/13
Assignment 2013 

 

An interactive C program to convert a binary number to its octal equivalent.

 

#include<stdio.h>
void main()
{
     char S[15];
     int I,Y,LIMIT,LEN,OCT[15],BIN[15],RESULT,FLAG=0;
     clrscr();
     printf("ENTER BINARY NUMBER TO BE CONVERTED : \n");
     scanf("%s",&S);
     LEN=strlen(S);
     for(I=0;I<LEN;I++)
     {
       if( S[I]=='0' || S[I]=='1' )
       {
     if( S[I]=='0')
        BIN[I]=0;
     else
        BIN[I]=1;
       }
       else
        FLAG=1;
     }
  if(FLAG==0)
  {
     printf("\nBINARY NUMBER YOU ENTERED TO BE CONVERTED IS \n");
     for(I=0;I<LEN;I++)
     {
    printf("%d ",BIN[I]);
     }

     Y=0;
     for(I=0;I<LEN;I++)
     {
    Y=Y*2+BIN[I];
     }
     printf("\n\nCONVERTED BINARY TO DECIMAL FORM IS \t%d",Y);

     LEN=0;
     while(Y>0)
     {
    OCT[LEN]=Y%8;
    Y=Y/8;
    LEN++;
     };
     printf("\n\nCONVERTED BINARY TO OCTAL FORM IS \t");
     for(I=LEN-1;I>-1;I--)
     {
    printf("%d",OCT[I]);
     }

  }
  else
    printf("YOU HAVE ENTERED WRONG BINARY VALUE !!");
  getch();
}

CODE : -

  1. #include<stdio.h>
  2. void main()
  3. {
  4. char S[15];
  5. int I,Y,LIMIT,LEN,OCT[15],BIN[15],RESULT,FLAG=0;
  6. clrscr();
  7. printf("ENTER BINARY NUMBER TO BE CONVERTED : \n");
  8. scanf("%s",&S);
  9. LEN=strlen(S);
  10. for(I=0;I<LEN;I++)
  11. {
  12. if( S[I]=='0' || S[I]=='1' )
  13. {
  14. if( S[I]=='0')
  15. BIN[I]=0;
  16. else
  17. BIN[I]=1;
  18. }
  19. else
  20. FLAG=1;
  21. }
  22. if(FLAG==0)
  23. {
  24. printf("\nBINARY NUMBER YOU ENTERED TO BE CONVERTED IS \n");
  25. for(I=0;I<LEN;I++)
  26. {
  27. printf("%d ",BIN[I]);
  28. }
  29.  
  30. Y=0;
  31. for(I=0;I<LEN;I++)
  32. {
  33. Y=Y*2+BIN[I];
  34. }
  35. printf("\n\nCONVERTED BINARY TO DECIMAL FORM IS \t%d",Y);
  36.  
  37. LEN=0;
  38. while(Y>0)
  39. {
  40. OCT[LEN]=Y%8;
  41. Y=Y/8;
  42. LEN++;
  43. };
  44. printf("\n\nCONVERTED BINARY TO OCTAL FORM IS \t");
  45. for(I=LEN-1;I>-1;I--)
  46. {
  47. printf("%d",OCT[I]);
  48. }
  49.  
  50. }
  51. else
  52. printf("YOU HAVE ENTERED WRONG BINARY VALUE !!");
  53. getch();
  54. }

SCREEN SHOTS :-

MCS011_Q2

MCS011_Q2_Output

A C program to find out perfect numbers from 1 and 50 – IGNOU MCA Assignment 2013

MASTER OF COMPUTER APPLICATIONS
Course Code : MCS-011
Course Title : Problem Solving and Programming
Assignment Number : MCA(1)/011/Assign/13
Assignment 2013 

 

A C program to find out perfect numbers from 1 and 50

 

#include<stdio.h>
void main()
{
int NUM,I,SUM=0;
clrscr();
for(NUM=1;NUM<50;NUM++)
{
for(I=1;I<NUM;I++)
{
if(NUM%I==0)
{
SUM=SUM+I;
}
}
if(SUM==NUM)
{
printf("\n%d IS A PERFECT NUMBER",NUM);
}
SUM=0;
}
getch();
}

CODE : -

#include<stdio.h>
void main()
{
int NUM,i,SUM=0;
printf("\nENTER ANY INTERGER AND FIND IS IT PERFECT NUMBER : ");
scanf("%d",&NUM);
for(i=1;i<NUM;i++)
{
if(NUM%i==0)
{
SUM=SUM+i;
}
}
if(SUM==NUM)
{
printf("\n%d IS A PERFECT NUMBER",NUM);
printf(" SINCE SUM OF DIVISORS IS %d \n",SUM);
}
else
{
printf("\n%d IS NOT A PERFECT NUMBER",NUM);
printf(" SINCE SUM OF DIVISORS IS %d \n",SUM);
}
getch();
}

SCREEN SHOTS :- 

MCS011_Q1

MCS011_Q1_Output

 

A C program to find the factorial of a number using Nonrecursion

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.

int factorial(int);

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 factorial 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 NUM. Other variable will be FACT which will be for holding the resultant factorial value returned by the FUNCTION. The identified variables are FACT,NUM.

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.

Main program needs only declaration of variables and function prototype. Also messages for user and scanning values  NUM and FACT is used to display the resultant values returned by the FUNCTION.

#include<stdio.h>
void main()
{
 int factorial(int);
 int FACT,NUM;
 printf("ENTER NUMBER : ");
 scanf("%d",&NUM);
 FACT=factorial(NUM);
 printf("\nFACTORIAL OF GIVEN NUMBER IS %d ",FACT);
 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 NNUM is passed to the function and saved in N variable of the function. Now the variable RESULT which will hold the resultant value to be returned by the FUNCTION. Variable.  The identified variable is RESULT.

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 RESULT;

In this program Here FOR loop starts with N is one. RESULT is the integer which is initialized to one. RESULT is multiplied with the integer I which is initialized to N and continues multiplying by reducing the value by one, till it reaches one.

int factorial(int N)
{
 int RESULT=1,I;
    for(I=N;I>0;I--)
    RESULT=RESULT*I;
 return(RESULT);
}

Steps of function call :-

RESULT=0   & I=5, RESULT = 1 * 5, hence RESULT=5
RESULT=5   & I=4, RESULT = 5 * 4, hence RESULT=20
RESULT=20  & I=3, RESULT = 20 * 3, hence RESULT=60
RESULT=60  & I=2, RESULT = 60 * 2, hence RESULT=120
RESULT=120  & I=1, RESULT = 120 * 1, hence RESULT=120

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 factorial(int);
  5.  int FACT,NUM;
  6.  printf("ENTER NUMBER : ");
  7.  scanf("%d",&NUM);
  8.  FACT=factorial(NUM);
  9.  printf("\nFACTORIAL OF GIVEN NUMBER IS %d ",FACT);
  10.  getch();
  11. }
  12.  
  13. int factorial(int N)
  14. {
  15.  int RESULT=1,I;
  16.     for(I=N;I>0;I--)
  17.     RESULT=RESULT*I;
  18.  return(RESULT);
  19. }

Screen Shots :-

C_program_Factorial_Non_Recursion

C_program_Factorial_Recursion_Output

A C program to find the factorial of a number 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.

int factorial(int);

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 factorial 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 NUM. Other variable will be FACT which will be for holding the resultant factorial value returned by the FUNCTION. The identified variables are FACT,NUM.

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.

Main program needs only declaration of variables and function prototype. Also messages for user and scanning values  NUM and FACT is used to display the resultant values returned by the FUNCTION.

#include<stdio.h>
void main()
{
 int factorial(int);
 int FACT,NUM;
 printf("ENTER NUMBER : ");
 scanf("%d",&NUM);
 FACT=factorial(NUM);
 printf("\nFACTORIAL OF GIVEN NUMBER IS %d ",FACT);
 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 NNUM is passed to the function and saved in N variable of the function. Now the variable RESULT which will hold the resultant value to be returned by the FUNCTION. Variable.  The identified variable is RESULT.

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 RESULT;

In this program there is a risk of generating an ENDLESS LOOP. Here we have to Check the TERMINATION condition is given or not. N is the given value it is reduced by one in the recursive function call as we have to multiply all the number from given N to 1. Their must stop at N equals 1, and if user enters value 1 then the result is also 1, hence we should return 1 in if condition and recursion in else condition. Return the Result at the end.

int factorial(int N)
{
 int RESULT;
    if(N==1)
    return(1);
    else
    RESULT=N*factorial(N-1);
 return(RESULT);
}

Steps of function call :-

RESULT=0   & N=5, RESULT = 5 * factorial(4), hence RESULT=5
RESULT=5   & N=4, RESULT = 4 * factorial(3), hence RESULT=20
RESULT=20  & N=3, RESULT = 3 * factorial(2), hence RESULT=60
RESULT=60  & N=2, RESULT = 2 * factorial(1), hence RESULT=120
RESULT=120  & N=1, RESULT = 1 * 1, hence RESULT=120

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 factorial(int);
  5.  int FACT,NUM;
  6.  printf("ENTER NUMBER : ");
  7.  scanf("%d",&NUM);
  8.  FACT=factorial(NUM);
  9.  printf("\nFACTORIAL OF GIVEN NUMBER IS %d ",FACT);
  10.  getch();
  11. }
  12.  
  13. int factorial(int N)
  14. {
  15.  int RESULT;
  16.     if(N==1)
  17.     return(1);
  18.     else
  19.     RESULT=N*factorial(N-1);
  20.  return(RESULT);
  21. }

Screen Shots :-

C_program_Factorial_Recursion

C_program_Factorial_Recursion_Output

A C program to compute transpose of a matrix

Let’s identify variables needed for this program.

Transpose of A matrix which is formed by turning all the rows of a given matrix into columns and vice-verse. The transpose of matrix A is written AT.

Transpose_Matrix

In this program, we need to save matrices which consists Rows and Columns. To save this we need Two dimensional ARRAY.

Multi dimensional Array is a nothing different than any Array but the only difference is that it has more than one deminsion to it e.g. square has two dimension and cube has three dimension. The size of array is decided by us in number of square brackets [] depending upon the Dimension selected. If We select two dimension than we have to take two square brackets[][]. It has to be accessed with the help of index number ranging from 0 to n-1 and 0 to m-1. (e.g. num[0][0],num[0][1],num[2][2] so on.) 

First few variables will be the one which will save the values entered by the user and it will be array A[4][4] . Here we are taking3x4 matrices hence taking number 3 and 4 in both the square brackets respectively. Transpose of the given matrix will be saved in B[3][3] and displayed later. other variables will be I and J which will be for FOR Loop so in all four variables.
The identified variables are A[3][3],B[3][3],I,J.

int A[3][4],B[4][3],I,J;

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.

In this program there is a requirement of accessing the two dimension array A[0][0],A[..][..],..A[3][4]. Here we have the index values ranging from zero to two (i.e. n-1) for ROWS and index values ranging from zero to three (i.e. n-1) for COLUMNS . Best Loop for such kind of condition is FOR Loop the value of loop will start with 0 to 2 and 0 to 3 . Now here we need nested loop one loop inside the other. So that we can access all the twelve elements with it. We will need four nested loop in all eight FOR loops, First two nested loop for scanning and printing required array values of a matrix from the user. Next nested loops for Transposing the values into another array B[4][3]. Next nested loops for printing resultant (Transpose) array values as output.

This let’s you understand nested loop

for(I=0;I<4;I++)
    {
        for(J=0;J<4;J++)
        {
            scanf("%d",&A[I][J]);
        }
    }

Transposing the matrix is simple just we have to do is start the column index first instead of row index and row index second instead of column index. Now, save the copy the values of given matrix in reverse index to B[][] and forward index to A[][] (i.e. B[J][I]=A[I][J];).

for(J=0;J<4;J++)
    {
        for(I=0;I<3;I++)
        {
            B[J][I]=A[I][J];
        }
    }

Finally print the Numbers of Two Dimension Resultant Array in the Matrix form by using Tab and New Line character in the nested Loop at the end of the program.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int A[3][4],B[4][3],I,J;
  5.     clrscr();
  6.     printf("\nENTER MATRIX A VALUES\n");
  7.     for(I=0;I<3;I++)
  8.     {
  9.         for(J=0;J<4;J++)
  10.         {
  11.             scanf("%d",&A[I][J]);
  12.         }
  13.     }
  14.     printf("\nVALUES GIVEN OF MATRIX ARE :\n");
  15.     for(I=0;I<3;I++)
  16.     {
  17.         for(J=0;J<4;J++)
  18.         {
  19.              printf("%d\t",A[I][J]);
  20.         }
  21.         printf("\n");
  22.     }
  23.     for(J=0;J<4;J++)
  24.     {
  25.         for(I=0;I<3;I++)
  26.         {
  27.             B[J][I]=A[I][J];
  28.         }
  29.     }
  30.     printf("\nVALUES GIVEN OF MATRIX ARE :\n");
  31.     for(I=0;I<4;I++)
  32.     {
  33.         for(J=0;J<3;J++)
  34.         {
  35.              printf("%d\t",B[I][J]);
  36.         }
  37.         printf("\n");
  38.     }
  39.     getch();
  40. }

Note:- whenever you have large number of inputs to be entered, we can use space instead of enter button all the time. You can see have its done in Output screen.

SCREEN SHOTS:-

C_program_Matrix_Transpose

C_program_Matrix_Transpose_Output