Tuesday 20 October 2020

HISSAN Pre-Board Examination -2069,2070,2071,2072

HISSAN Pre-Board Examination -2069,2070,2071,2072
C Programming Solve Question


HISSAN Pre Board Examination 2072
Grade – XII
Computer Science
Solved Question
GROUP “A”
1. Differentiate between ‘While’ and ‘do while’ loop. Write a program to display the sum of first 10 even numbers using loop.
Answer:
while loop:
This is an entry controlled looping statement. It is used to repeat a block of statements until condition
becomes true.
Syntax:
while(condition)
{
statements;
increment/decrement;
}
In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the loop and executes the block of statements associated with it. At the end of loop increment or decrement is done to change in variable value. This process continues until test condition satisfies.
Do-While loop :
This is an exit controlled looping statement. Sometimes, there is need to execute a block of statements first then to check condition. At that time such type of a loop is used. In this, block of statements are executed first and then condition is checked.
Syntax:
do
{
statements;
(increment/decrement);
}while(condition);
In above syntax, the first the block of statements are executed. At the end of loop, while statement is
executed. If the resultant condition is true then program control goes to evaluate the body of a loop once again. This process continues till condition becomes true. When it becomes false, then the loop terminates.
Sum of first 10 even numbers:
#include<stdio.h>
void main()
{
int a=1,b=0;
do
{
if(a%2==0)
{
printf("%d\n",a);
b=b+a;
}
a++;
}while(a<=10);
printf("Sum of first 10 even numbers:%d\n",b);
}
2. Write a program which asks nth terms of numbers and sort them in ascending order.
Answer:
#include<stdio.h>
void sort(int);
void main()
{
int a;
printf("Input Total Number:");
scanf("%d",&a);
sort(a);
}
void sort (int a)
{
int b,c[a],d,tmp;
for(b=0;b<a;b++)
{
printf("%d,Input Number:",b+1);
scanf("%d",&c[b]);
}
for(b=0;b<a;b++)
{
for(d=0;d<a;d++)
{
if(c[b]<=c[d])
{
tmp=c[b];
c[b]=c[d];
c[d]=tmp;
}
}
}
for(b=0;b<a;b++)
{
printf("%d\n",c[b]);
}
}
3. Describe the “strcat”,”strcpy”,”strlen” and “strrev” string functions with example.
Answer:
“strcat”
Strcat is a concatenate strings. Appends a copy of the source string to the destination string.
#include<stdio.h>
void main()
{
char *title,name[100];
printf( "Enter your name: " );
scanf( "%s", name );
title = strcat( name, " the Great" );
printf( "Hello, %s\n", title );
}
”strcpy”:
Strcpy is a copy string function. Copies the C string pointed by source into the array pointed by destination
#include<stdio.h>
void main()
{
char a[]="Hello world";
char b[100];
strcpy(b,a);
printf( "%s\n",a);
printf( "copy from a: %s\n",b);
}
”strlen”:
Strlen string function return the string length.
#include<stdio.h>
void main()
{
char a[]="Hello world";
int t;
t=strlen(a);
printf( "Total length of string %d\n",t);
}
“strrev”:
Strrev string function reverse the given string:
#include<stdio.h>
void main()
{
char a[100];
printf("Input String:");
gets(a);
printf("Reverse: %s\n",strrev(a));
}
4. a) Differentiate between structure and union.
Answer:
Structure is user defined data type which is used to store heterogeneous data under unique name.
Keyword 'struct' is used to declare structure. The variables which are declared inside the structure are
called as 'members of structure'. Union is user defined data type used to stored data under unique variable
name at single memory location. Union is similar to that of structure. Syntax of union is similar to structure.
The major difference between structure and union is 'storage.' In structures, each member has its own
storage location, whereas all the members of union use the same location. Union contains many members
of different types, it can handle only one member at a time.
Structure Syntax:
struct emp_info
{
char emp_id[10];
char nm[100];
float sal;
}emp;
Union Syntax:
union union_name
{
int comp_id;
char nm;
float sal;
}tch;
b) Write a program to input a number and find out whether it is odd number or even number.
Answer:
#include<stdio.h>
void main()
{
int a;
printf("Input Number:");
scanf("%d",&a);
if(a%2==0)
printf("%d is Even number\n",a);
else
printf("%d is Odd number\n",a);
}
5. a) Describe ‘fscanf’ and ‘fprintf’ function.
Answer:
The fscanf() function shall read from the file. The sscanf() function shall read from the strings. Each function
reads bytes, interprets them according to a format, and stores the results in its arguments. A set of pointer
arguments indicating where the converted input should be stored. The result is undefined if there are
insufficient arguments for the format. If the format is exhausted while arguments remain, the excess
arguments shall be evaluated but otherwise ignored.
fscanf Syntax:
fscanf(file pointer, "format”,&variable);
The fprintf function formats and writes output to stream. It converts each entry in the argument list, if any,
and writes to the stream according to the corresponding format specification in format.
fprintf Syntax:
fprintf (file pointer, "format", arguments)
Example:
#include <stdio.h>
void main()
{
float f1, f2;
int i1, i2;
FILE *fp;
fp = fopen ("test.txt", "w");
fprintf (fp, "%f %f %d %d",23.5,100.5,100,5);
fclose (fp);
//file created and entered integer and float value
fp = fopen ("test.txt", "r");
fscanf (fp, "%f %f %d %d",&f1,&f2,&i1,&i2);
fclose (fp);
//reading file using fscanf
printf ("Float 1 = %f\n", f1);
printf ("Float 2 = %f\n", f2);
printf ("Integer 1 = %d\n", i1);
printf ("Integer 2 = %d\n", i2);
}
b) Write a program to display name, age and address reading from a file named “record.dat”.
Answer:
#include <stdio.h>
void main()
{
int age;
char nm[20], add[50];
FILE *fp;
//file write
fp = fopen ("record.dat","a");
fprintf (fp, "%s %d %s\n","Numa",15,"Dharan-9");
fclose (fp);
//file reading
fp = fopen ("record.dat", "r");
while(feof(fp)==0)
{
fscanf (fp, "%s %d %s",&nm,&age,&add);
printf ("Name:%s\n",nm);
printf ("Age:%d\n",age);
printf ("Address:%s\n",add);
}
fclose (fp);
}

Solved Question 2071

HISSAN PRE BOARD EXAMINATION 2071
Grade-XII
Subject: Computer Science


Q1) Write a program that reads several different name and address into the computer, rearrange the names into alphabetical order make use of structure variable.

Answer:
#include<stdio.h>
struct st
{
    char nm[5][20],add[5][30],tmp[20],adtmp[20];
};
void main()
{
    struct st t;
    int i,x,y,z;
    puts("<<<Input 5 records>>>");
    for(i=0;i<5;i++)
    {
        printf("%d,Name:",i+1);
        scanf("%s",&t.nm[i]);
        printf("%d,Address:",i+1);
        scanf("%s",&t.add[i]);
    }
    for(x=1;x<5;x++)
    {
        for(y=1;y<5;y++)
        {
            if(strcmp(t.nm[y-1],t.nm[y])>0)
            {
                strcpy(t.tmp,t.nm[y-1]);//name
                strcpy(t.adtmp,t.add[y-1]);//address
                strcpy(t.nm[y-1],t.nm[y]);//name shift
                strcpy(t.add[y-1],t.add[y]);//address shift
                strcpy(t.nm[y],t.tmp);//name shift to first
                strcpy(t.add[y],t.adtmp);//address shift to first
            }
        }
    }
    puts("\n\n<<<Sorting records>>>\n");
    for(z=0;z<5;z++)
    {
        printf("Name:%s,Address:%s\n",t.nm[z],t.add[z]);
    }

}


Q4) Write a program to delete and rename the data file usingg remove and rename command.
Answer:

#include<stdio.h>
void main()
{
    char of[128],nf[128],fn[128];
    int ch,chk;
    puts("1,File Rename");
    puts("2,Remove File");
    puts("3,Exit");
    printf("Your Choice:");
    scanf("%d",&ch);
    getchar();
    switch(ch)
    {
    case 1:
        printf("Input Old File Name:");
        gets(of);
        printf("Input New File Name:");
        gets(nf);
        chk=rename(of,nf);
        if(chk==0)
            printf("Rename success!\n");
        else
            printf("Rename Unsuccess!\n");
        break;
    case 2:
        printf("To delete,Input file name:");
        gets(fn);
        chk=remove(fn);
        if(chk==0)
            printf("Remove success!\n");
        else
            printf("Remove Unsuccess!\n");
        break;
    case 3:
        puts("Good Bye!!");
        exit(1);
    default:
        puts("Unknown Choice!!");
        puts("Good Bye!!");
    }
}




Solved Question 2070

HISSAN PRE BOARD EXAMINATION 2070
Grade-XII
Subject: Computer Science
Long Answer:
Q. 1) What is looping? Describe “for”, “while” and “do-while” loops with appropriate examples.
Answer:
Looping statements or Iterative Statements
'A loop' is a part of code of a program which is executed repeatedly. A loop is used using condition. The repetition is done until condition becomes condition true. A loop declaration and execution can be done in following ways.
·         Check condition to start a loop
·         Initialize loop with declaring a variable.
·         Executing statements inside loop.
·         Increment or decrement of value of a variable.
For loop:
This is an entry controlled looping statement. In this loop structure, more than one variable can be initialized. One of the most important feature of this loop is that the three actions can be taken at a time like variable initialization, condition checking and increment/decrement. The “for” loop can be more concise and flexible than that of while and do-while loops.
Syntax:
for(initialisation; test-condition; incre/decre)
{
statements;
}

Example:
#include <stdio.h>
void main()
{
int a;
clrscr();
for(i=1; i<=5; i++)
{
printf("i=%d\n");
}
}


while loop:
This is an entry controlled looping statement. It is used to repeat a block of statements until condition becomes true.
Syntax:
while(condition)
{
statements;
increment/decrement;
}
In above syntax, the condition is checked first. If it is true, then the program control flow goes inside the loop and executes the block of statements associated with it. At the end of loop increment or decrement is done to change in variable value. This process continues until test condition satisfies.

Example:
#include <stdio.h>
#include <conio.h>
void main()
{
int a;
clrscr();
a=1;
while(a<=5)
{
printf("\n %d \n",a);
a++;
}
getch();
}

Do-While loop :
This is an exit controlled looping statement. Sometimes, there is need to execute a block of statements first then to check condition. At that time such type of a loop is used. In this, block of statements are executed first and then condition is checked.
Syntax:
do
{
statements;
(increment/decrement);
}while(condition);
In above syntax, the first the block of statements are executed. At the end of loop, while statement is executed. If the resultant condition is true then program control goes to evaluate the body of a loop once again. This process continues till condition becomes true. When it becomes false, then the loop terminates.

Example:
#include <stdio.h>
void main()
{
int a;
a=5;
do
{
printf("%d\n ",a);
a++;
}while(a<=1);
}


Q.2) What is recursive function? Write a program to calculate the factorial of an integer using recursion.

Answer:
When a function of body calls the same function then it is called as 'recursive function’. Recursion is a tool to allow the programmer to concentrate on the key step of an algorithm, without having initially to worry about coupling that step with all the others.

Program:
#include<stdio.h>
int recursion(int a)
{
    if(a==0)
        return 1;
    return (a*recursion(a-1));
}
void main()
{
    int i,j;
    printf("Input number for factorial:");
    scanf("%d",&i);
    j=recursion(i);
    printf("Factorial Number:%d\n",j);

}


Q.3) Write a program to input names of ‘n’ numbers of students and sort them in alphabetical order.

Answer:
#include<stdio.h>
void main()
{
    char nm[100][20];
    char tmp[20];
    int r,i,j,m,n,l;
    printf("How many records?");
    scanf("%d",&r);

    for(i=0;i<r;i++)
    {
        printf("Input Name:");
        scanf("%s",&nm[i]);
    }
    printf("\n\n\nWithout sorting:\n");
    for(j=0;j<r;j++)
    {
        printf("%s\n",nm[j]);
    }
    printf("\n\n\nAfter sorting:\n");
    for(m=0;m<r;m++)
    {
        for(n=0;n<r;n++)
        {
            if(strcmp(nm[n-1],nm[n])>0)
            {
                strcpy(tmp,nm[n-1]);
                strcpy(nm[n-1],nm[n]);
                strcpy(nm[n],tmp);
            }
        }
    }
    for(l=0;l<r;l++)
    {
        printf("%s\n",nm[l]);
    }
}

Q.4) Differentiate between structure and union with suitable examples.

Answer:
Structure is user defined data type which is used to store heterogeneous data under unique name. Keyword 'struct' is used to declare structure. The variables which are declared inside the structure are called as 'members of structure'. Union is user defined data type used to stored data under unique variable name at single memory location. Union is similar to that of structure. Syntax of union is similar to structure.
The major difference between structure and union is 'storage.' In structures, each member has its own storage location, whereas all the members of union use the same location. Union contains many members of different types, it can handle only one member at a time.

Structure Syntax:
struct emp_info
{
char emp_id[10];
char nm[100];
float sal;
}emp;

Example:
#include <stdio.h>
#include <conio.h>
struct comp_info
{
char nm[100];
char addr[100];
}info;
void main()
{
clrscr();
printf("\n Enter Company Name : ");
gets(info.nm);
printf("\n Enter Address : ");
gets(info.addr);
printf("\n\n Company Name : %s",info.nm);
printf("\n\n Address : %s",info.addr);
getch();
}

Union Syntax:
union union_name
{
int comp_id;
char nm;
float sal;
}tch;
In above syntax, it declares tch variable of type union. The union contains three members as data type of int, char, float. We can use only one of them at a time.
Example:
#include <stdio.h>
#include <conio.h>
union myunion
{
int id;
char nm[50];
}tch;
void main()
{
clrscr();
printf("\n\t Enter developer id : ");
scanf("%d", &tch.id);
printf("\n\n\t Enter developer name : ");
scanf("%s", &tch.nm);
printf("\n\n Developer ID : %d", tch.id);        //Garbage
printf("\n\n Developed By : %s", tch.nm);
getch();
}


Q.5) Write a program to delete and rename the data file using remove and rename commands.

Answer:
Delete Data file:
#include<stdio.h>
void main()
{
   int status;
   char file_name[25];

   printf("Enter the name of file you wish to delete\n");
   gets(file_name);

   status = remove(file_name);

   if( status == 0 )
      printf("%s file deleted successfully.\n",file_name);
   else
   {
      printf("Unable to delete the file\n");
      perror("Error");
   }
}

Rename Data File

#include<stdio.h>
void main()
{
   int status;
   char file_name[25],newname[25];

   printf("Enter the name of file you wish to rename:");
   gets(file_name);
   printf("File Name:%s\n",file_name);
   printf("Rename your file:");
   gets(newname);
   status = rename(file_name,newname);

   if( status == 0 )
      printf("%s file renamed successfully into %s.\n",file_name,newname);
   else
   {
      printf("Unable to rename the file\n");
      perror("Error");
   }


}





 Solved Question 2069


Q1) what is infinite loop? Differentiate between “break” and “continue” statement. Write a program to read any number and find it is even or odd.
Ans:
An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over. Infinite loops are used to assure a program segment loops forever or until an exceptional condition arises, such as an error.
Break Statement
In C programming, break is used in terminating the loop immediately after it is encountered. The break statement is used with conditional if statement. For Example,
#include<stdio.h>

    int main()
    {
             int i;
        i = 0;
        while ( i < 20 )
             {
                  i++;
                  if ( i == 10)
                       break;
             }
             return 0;
    }
Continue Statement
It is sometimes desirable to skip some statements inside the loop. In such cases, continue statements are used. For Example,
#include<stdio.h>
    int main()
    {
        int i;
        i = 0;
             while ( i < 20 )
             {
                  i++;
                  continue;
                  printf("Nothing to see\n");
             }
             return 0;
    }

Odd, Even program:
#include<stdio.h>
void main()
{
    int num;
    printf("Input Number:");
    scanf("%d",&num);
    if(num%2==0)
    printf("%d is Even\n",num);
    else
    printf("%d is Odd\n",num);
}


Q2) Differentiate between Structure and Array? Write a program in C that reads the age of 100 persons and count the number of person in the age group between 50 and 60.
Ans.
Differentiate between Structure and Array.
Both the arrays and structures are classified as structured data types as they provide a mechanism that enable us to access and manipulate data in a relatively easy manner. But they differ in a number of ways listed in table below:

Arrays
Structures
1. An array is a collection of related data elements of same type.
1. Structure can have elements of different  types
2. An array is a derived data type
2. A structure is a programmer-defined data type
3. Any array behaves like a built-in data types. All we have to do is to declare an array variable and use it.
3. But in the case of structure, first we have to design and declare a data structure before the variable of that type are declared and used.

100 persons, Age between 50 and 60:
#include<stdio.h>
void main()
{
    int tot=0,num,age,i;
    printf("Number of persons:");
    scanf("%d",&num);
    for(i=1;i<=num;i++)
    {
        printf("person %d age:",i);
        scanf("%d",&age);
        if(age>=50 && age<=60)
        tot=tot+1;
    }
    printf("Age between 50 and 60: %d\n",tot);
}

Q3) Write a program that reads 10 persons names and address into the computer; and sorts the name into alphabetical order using structure variable.
Ans:
#include<stdio.h>
#include<string.h>
struct str
{
    char nm[10][20];
    char add[10][20];
};
void main()
{
struct str st;
char t[20];
int a,b,c,i,j;
for(a=0;a<10;a++)
{
printf("input name:");
scanf("%s",&st.nm[a]);
printf("input address:");
scanf("%s",&st.add[a]);
}
for(b=0;b<10;b++)
{
    printf("Name:%s\nAddress:%s\n-----------\n",st.nm[b],st.add[b]);
}
printf("Sorting names:\n");
for(i=1;i<10;i++)
    {
    for(j=1;j<10;j++)
        {
        if(strcmp(st.nm[j-1],st.nm[j])>0)
            {
            strcpy(t,st.nm[j-1]);
            strcpy(st.nm[j-1],st.nm[j]);
            strcpy(st.nm[j],t);
            }
        }
    }
    for(b=0;b<10;b++)
    {
    printf("Name:%s\n",st.nm[b]);
    }
}

Q4) Write data file operation modes? Write a program to accept any 20 employee’s records (Empno, Name, Address and Phone). Store these records on ABC.txt file.
Ans:
#include<stdio.h>
void main()
{
    FILE *f;
    char nm[10][30],add[10][35],ph[10][15];
    int empno[10],i,j;
    for(i=0;i<10;i++)
    {
        printf("Input EmpNo.:");
        scanf("%d",&empno[i]);
        printf("empno:%d\n",empno[i]);
        getchar();
        printf("Input Name:");
        scanf("%s",&nm[i]);
        printf("\nname:%s\n",nm[i]);
        printf("Input Address:");
        scanf("%s",&add[i]);
        printf("\naddress:%s\n",add[i]);
        printf("Input Phone:");
        scanf("%s",&ph[i]);
        printf("\nphone:%s\n",ph[i]);
    }
    for(j=0;j<10;j++)
    {
        f=fopen("ABC.TXT","a");
        fprintf(f,"Empno:%d\nName:%s\nAddress:%s\nPhone:%s\n",empno[j],nm[j],add[j],ph[j]);
        fclose(f);
    }
}

Q5.a) Write a program to find factorial of any number using recursive function.
Ans:
#include<stdio.h>
int fact(int m)
{
    if(m==0)
        return 1;
    else
    return(m*fact(m-1));
}
void main()
{
    int num,i,j,a;
    printf("Input number:");
    scanf("%d",&a);
    for(i=1;i<=a;i++)
    {
        j=fact(a);
    }
    printf("%d\n",j);
}

Q5.b) Write a program to enter 4-digit number and find sum of them.
Ans:
#include<stdio.h>
void main()
{
    int num,rem,sum=0;
    printf("Input Integer:");
    scanf("%d",&num);
    while(num>0)
    {
        rem=num%10;
        sum=sum+rem;
        num=num/10;
    }
   printf("Sum:%d\n",sum);

} 

No comments:

Post a Comment

MCQ Question Bank Microsoft PowerPoint