-->

neb39:Differentiate between break and continue with example

Differentiate between break and continue with example
ans:
differences are :

break
continue
1
It terminates the execution.
It takes the iteration to next level(skipping).
2
It can be used in loop and switch.
It can be used in switch
3
It uses keyword ‘break’.
It uses keyword ‘continue’.
4
Its syntax is
break;
Its syntax is
continue;
5
Example is
For(i=1;i<=4;i++)
{
  If(i<=2)
    Printf(“%d\n”,i);
     Break;
}
}
Example is
For(i=1;i<=4;i++)
{
  If(i<=2)
    Printf(“%d\n”,i);
     continue;
}
}

output is:1 
output is:1,2

neb37:Differentiate between while and do.. while loop.

Differentiate between while and do.. while loop.
ans:
Following are differences between while and do...while loop




while
Do..while
1
It is called pre-testing loop.
it is called post-testing loop
2
It uses keyword ‘while’.
it uses keyword ‘do.. while’
3
Its syntax is
Initialization;
While(condition)
{
Statement;
Increment/decrement;
}
 Its syntax is
Initialization;
do
{
Statement;
Increment/decrement;
              }while(condition);
4
nothing will happen if the condition is false.
 even after false(condition), it will run at least once.
5
Its flowchart is



Its flowchart is


6
example is
Int i=1;
While(i<=5)
{
Printf(“%d”,i);
I++;
}
 example is
Int i=1;
do
{
Printf(“%d”,i);
I++;
}while(i<=5);

q36)Wap to display those records which have price >340. Records are with fields name,price and edition.

//Wap to display those records which have price >340. Records are with fields name,price and edition.
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k;                                                                    //  pointer for file declaration
char book_name[30],author_name[40];                   //identifier declaration
int book_edition;                                                         //       "             "
k=fopen("book.dat","r");

while((fscanf(k,"%d %s %d",&book_price,book_name,&book_edition)!=EOF))//reading data from data file
{
 if(book_price>340)                                                   // condition validating
{
printf("book name=%s ,book edition=%d and price=%d",book_name,book edition,book_price); //displaying data
}
}
fclose(k);                                                                                   //closing data file
getch();
}

NEB35:WAP top read all records stored in a file book.dat. Fields are name,price and author's name.

//WAP top read all records stored in a file book.dat. Fields are name,price and author's name.
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k;                                                                    //  pointer for file declaration
char book_name[30],author_name[40];                   //identifier declaration
int book_price;                                                         //       "             "
k=fopen("book.dat","r");

while((fscanf(k,"%d %s %s",&book_price,book_name,author_name)!=EOF))//reading data from data file
{
printf("book name=%s ,author name=%s and price=%d",book_name,author_name,book_price); //displaying data
}
fclose(k);                                                                                   //closing data file
getch();
}

NEB34:WAP to store details about book(any 'n') having fields name,price,edition.

//WAP to store details about book(any 'n') having fields name,price,edition.
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k;                                                                    //  pointer for file declaration
char book_name[30],choice;                                                    //identifier declaration
int book_price,edition;                                             //       "             "
k=fopen("book.dat","w");
do
{
printf("enter book's name\n");                                       // getting inuts
gets(book_name);
printf("enter edition\n");
scanf("%d",&edition);
printf("enter book's price\n");
scanf("%d",&book_price);
fprintf(k,"%s %d %d",book_name,edition,book_price);   //writing data to data file
printf("want to continue y/n\n");
choice=getche();                                                        //getting a character from user  
}while(choice!='n');                                                      // validating the input
printf("writing process completed successfully\n");
fclose(k);                                                                                   //closing data file
getch();
}

NEB33":WAP to store book's name,price and author's name in a file namd 'book.dat'.

//WAP to store book's name,price and author's name in a file named 'book.dat'.
#include<stdio.h>
include<conio.h>
void main()
{
FILE *k;                                                                    //  pointer for file declaration
char book_name[30],author_name[40];                   //identifier declaration
int book_price;                                                         //       "             "
k=fopen("book.dat","w");
printf("enter book's name and author's name\n");   // getting inuts
gets(book_name);
printf("enter author's name\n");
gets(author_name);
printf("enter book's price\n");
scanf("%d",&book_price);
fprintf(k,"%s %s %d",book_name,author_name,book_price);   //writing data to data file
printf("writing process completed successflly\n");
fclose(k);                                                                                   //closing data file
getch();
}




NEB32:three data file handling functions with examples.

Explain any three data file handling functions with examples.
ans:-
There are many data file functions which we use while programming. Following are some functions used to handle data stored in data file.

1) fopen(): It is used to open the file in particular mode. Its syntax is
                FILE *fopen(const char *filename, const char *mode); 
or 
FILE pointer= fopen("file name","mode");

Example
FILE *k;
k=fopen("data.txt","w");
This statement opens the file named data.txt in writing mode(w). Here ,writing mode means, we are going to store data in data file .

2)fclose(): It is used to close the opened file.As we have finished working with data file,it needs to be closed; so, at last we close it using fclose().

syntax
fclose(FILE name);
or fclose(FILE stream);

example
FILE *k;

k=fopen("data.txt","w");

..................

fclose(k);
Here we have closed data file using fclose() function.

3) fprintf(): It is used to write data/records in an opened data file i.e it writes data in an opened data file.

 syntax
int fprintf(FILE *stream,constant char *format);

example;
FILE *k;

k=fopen("data.txt","w");

fprintf(k,"%s %s  %s","hello","world", "of c");

this writes data (mentioned above) in an opened file data.txt.
4) fscan(): It is used to read data/records from opened data file. 
 syntax
int fscanf(FILE *stream,constant char *format,...);

example;
FILE *k;

k=fopen("data.txt","r");
char string[100];fscanf(k,"%s %s  %s",string,string,string);

this reads data (mentioned above) from opened file data.txt.