If you want to make money online : Register now

IGNOU MCS-011 Solved Paper June 2014

, , No Comments
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();
}



0 comments:

Post a Comment