# 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("\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 : -

 Source code
`#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]);     }}`

SCREEN SHOTS : -

## 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: -

 Source code
`#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______________________________________________________________________"); }`

SCREEN SHOTS :-

## 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 : -

 Source code
`#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();}`

SCREEN SHOTS :-

## 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 : -

 Source code
`#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();}`

SCREEN SHOTS :-

## 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 : -

 Source code
`#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;}`

SCREEN SHOTS :-

## 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 : -

 Source code
`#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();}`

SCREEN SHOTS :-

## 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 : -

 Source 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 :-

## 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.

 Source code
`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.

 Source code
```#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.

 Source code
`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.

 Source code
```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 :

 Source code
`#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();} int factorial(int N){ int RESULT=1,I;    for(I=N;I>0;I--)    RESULT=RESULT*I; return(RESULT);}`

Screen Shots :-

## C019 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.

 Source code
`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.

 Source code
```#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.

 Source code
`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.

 Source code
```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 :

 Source code
`#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();} int factorial(int N){ int RESULT;    if(N==1)    return(1);    else    RESULT=N*factorial(N-1); return(RESULT);}`

Screen Shots :-

Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.

## C018 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.

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.

 Source code
`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

 Source code
```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];).

 Source code
```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 :

 Source code
`#include<stdio.h>void main(){    int A[3][4],B[4][3],I,J;    clrscr();    printf("\nENTER MATRIX A VALUES\n");    for(I=0;I<3;I++)    {        for(J=0;J<4;J++)        {            scanf("%d",&A[I][J]);        }    }    printf("\nVALUES GIVEN OF MATRIX ARE :\n");    for(I=0;I<3;I++)    {        for(J=0;J<4;J++)        {             printf("%d\t",A[I][J]);        }        printf("\n");    }    for(J=0;J<4;J++)    {        for(I=0;I<3;I++)        {            B[J][I]=A[I][J];        }    }    printf("\nVALUES GIVEN OF MATRIX ARE :\n");    for(I=0;I<4;I++)    {        for(J=0;J<3;J++)        {             printf("%d\t",B[I][J]);        }        printf("\n");    }    getch();}`

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:-

Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.