Showing posts with label MCS011. Show all posts
Showing posts with label MCS011. Show all posts
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct emp{

char name[10];
int age;

};

File handling concept


void NameAndAgeStoring(){

struct emp e;
FILE *p, *q;
p= fopen("list.txt","a");
q= fopen("list.txt","r");

printf("enter the name and age");
scanf("%s %d",e.name,e.age);
fprintf(p,"%s %d",e.name,e.age);
fclose(p);

do{

fscanf(q,"%s %d",e.name,e.age);
printf("%s %d",e.name,e.age);
}
while(!feof(q));
getch();
}

File handling concept - Create and store value

void file()
{
 FILE *fp;
 char ch;
 fp = fopen("names.txt", "w");
  while( ( ch= getchar())!=EOF){

 putc(ch,fp);
  }
  fclose(fp);
}

string length without strlen()

void stringCal(){

int i;
char str[1000];
printf("enter string value \n");
scanf("%s",str);
for(i=0; str[i] != '\0';++i){

}
printf(" number of char entered %d",i);
getch();
}

string reverse using pointer concept

int stringReverse(){
char str[1000], *ptr;
int i, len;
printf("enter string \n");

gets(str);

ptr = str;
for(i=0; i <1000;i++)
{
if(*ptr == '\0'){
break;


}
ptr++;
}

len=i;
ptr--;
printf("revered string: ");

for(i=len; i>0; i--){

printf("%c",*ptr--);

}
getch();
return 0;

}

search name in file - file concept

void SearchName(){

FILE *fp;
char ch[10],t[10], rep[10];
int flag=0;
printf("enter the value to search \n");
gets(t);
fp = fopen("names.dat","a+");
//tp =fopen("names.dat","w+");
do{

fscanf(fp,"%s\n",ch);
if(strcmp(ch,t) == 0){
//printf("%s\n",ch);
printf("name found, enter the replacement value \n");
gets(rep);

flag=1;
fseek(fp,0,SEEK_SET);
fprintf(fp,"%s\n",rep);
fclose(fp);

break;
}else{
//printf("not found \n");
}
}
while(!feof(fp));
fclose(fp);
if(flag==0)
{
printf("name not matched \n");
}else
{
printf("name replaced successfully \n");
}
getch();
}
1 (a). Write a C program to find the factorial value of a number. Also write the algorithm and draw flowchart.

Ans.

/*c program to find out factorial value of a number*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int n,i,fact=1;
 printf("Enter any number : ");
 scanf("%d"&n);
 for(i=1; i<=n; i++)
    fact = fact * i;
 printf("Factorial value of %d = %d",n,fact);
 return 0;
}

The output of above program would be:
Output of calculate factorial value  of a number C program
Screen shot for calculate factorial value
of a number C program



Algorithm for calculate factorial value of a number:

[algorithm to calculate the factorial of a number]
step 1. Start
step 2. Read the number n
step 3. [Initialize]
        i=1, fact=1
step 4. Repeat step 4 through 6 until i=n
step 5. fact=fact*i
step 6. i=i+1
step 7. Print fact
step 8. Stop
[process finish of calculate the factorial value of a number]

Flowchart for calculate factorial value of a number:

flowchart for calculate factorial value of a number
Figure: Flowchart for calculate factorial value of
a number C program




1 (b) Using recursion, generate 'n' terms of 10 Fibonacci series (n > 0).


C code to print Fibonacci series without recursion:

#include<stdio.h>

void printFibonacci(int);

int main(){

    int k,n;
    long int i=0,j=1,f;

    printf("Enter the range of the Fibonacci series: ");
    scanf("%d %d ",&n);

    printf("Fibonacci Series: ");
    printf("%d ",0);
    printFibonacci(n);

    return 0;
}

void printFibonacci(int n){

    long int first=0,second=1,sum;

    while(n>0){
         sum = first + second;
         first = second;
         second = sum;
         printf("%ld ",sum);
         n--;
    }
}



1 (c) Using file handling, create a file, insert some 10
characters and count them.

Ans.
#include<stdio.h>
#include<conio.h>
 
void main()
{
 char ch;
 int count=0;
 FILE *fptr;
 clrscr();
 fptr=fopen("text.txt","w");
 if(fptr==NULL)
 {
  printf("File can't be created\a");
  getch();
  exit(0);
 }
 printf("Enter some text and press enter key:\n");
 while((ch=getche())!='\r')
 {
  fputc(ch,fptr);
 }
 fclose(fptr);
 fptr=fopen("text.txt","r");
 printf("\nContents of the File is:");
 while((ch=fgetc(fptr))!=EOF)
 {
  count++;
  printf("%c",ch);
 }
 fclose(fptr);
 printf("\nThe number of characters present in file is: %d",count);
 getch();
}




1 (d). Using pointers concept, reverse a given 10
string.




Ans.
Logic: The approach here is to reverse the string using the pointers. Reversing the string includes the reversing the each and every words in it. After accepting a string from user, it calls a function “strev” with two string pointer arguments, the source and destination. It has an iterative loop, which traces from the EOL through the beginning. Each time it copies the current letter to the destination. Finally it displays the resultant string.
The earlier program implements the same by direct method, i.e. without pointers.

#include<stdio.h>
#include<conio.h>
void strev(char *str1, char *str2);
void main()
{
char *str1, *str2;
clrscr();
printf("\n\n\t ENTER A STRING...: ");
gets(str1);
strev(str1,str2);
printf("\n\t THE REVERSED STRING IS...: ");
puts(str2);
getch();
}

void strev(char *str1, char *str2)
{
int i = 0, len = 0, r = 0;
while(*(str1+len)!='\0')
len++;
for(i=len-1; i>=0; i--)
{
*(str2+r) = *(str1+i);
r++;
}
*(str2+r) = '\0';
}



 


2 (a). Write a program to find the string length 10
without using strlen() function 

#include<conio.h>
#include<stdio.h>
void main(){
char *str;
int count = 0,i;
clrscr();
printf("\nEnter the String: ");
gets(str);
for(i=0;str[i]!='\0';i++){
count++;
}
printf("\nThe length of the string is %d.",count);
getch();
}