program to generate random numbers
using codeblocks
---------------------------------------------------------------------------------------------
//progrm to generate random numbers
#include <stdio.h>
#include <stdlib.h> //contains rand() function
int main()
{
int k,number;
printf("how many u want=\n");
scanf("%d",&number);
printf("some randomly generated numbers are given below.\n");
for(k=0;k<=number;k++)
{
printf("%7d\t",rand());//prints randomly generated numbers
}
return 0;
}
---------------------------------------------------------------------------------------------
//progrm to generate random numbers
#include <stdio.h>
#include <stdlib.h> //contains rand() function
int main()
{
int k,number;
printf("how many u want=\n");
scanf("%d",&number);
printf("some randomly generated numbers are given below.\n");
for(k=0;k<=number;k++)
{
printf("%7d\t",rand());//prints randomly generated numbers
}
return 0;
}
----------------------------------------------------------------------------------------------
using turboc++
--------------------------------------------------------------------------------------------------------
//progrm to generate random numbers
#include <stdio.h>
#include <stdlib.h> //contains rand() function
#include<conio.h>
int main()
{
int k,number;
printf("how many u want=\n");
scanf("%d",&number);
printf("some randomly generated numbers are given below.\n");
for(k=0;k<=number;k++)
{
printf("%7d\t",rand());//prints randomly generated numbers
}
getch();
return 0;
}
enum data type with value
using codeblocks
-------------------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung=1,LG,apple,hp,acer=5}; // list of values of enum. value location starts from 1.
enum computer find; //variable for object "computer"
int main()
{
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 1 to 5.
{
printf("location of =%d\n",find);// display that. the value increases itself.
}
return 0;
}
//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>
int main()
{
enum computer{samsung=1,LG,apple,hp,acer=5}; // list of values of enum. value location starts from 1.
enum computer find; //variable for object "computer"
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 1 to 5.
{
printf("location =%d\n",find);// display that. the value increases itself.
}
return 0;
}
*/
----------------------------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung=1,LG,apple,hp,acer=5}; // list of values of enum. value location starts from 1.
enum computer find; //variable for object "computer"
int main()
{
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 1 to 5.
{
printf("location of =%d\n",find);// display that. the value increases itself.
}
return 0;
}
//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>
int main()
{
enum computer{samsung=1,LG,apple,hp,acer=5}; // list of values of enum. value location starts from 1.
enum computer find; //variable for object "computer"
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 1 to 5.
{
printf("location =%d\n",find);// display that. the value increases itself.
}
return 0;
}
*/
----------------------------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung=1,LG,apple,hp,acer=5}; // list of values of enum. value location starts from 1.
enum computer find; //variable for object "computer"
int main()
{
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 1 to 5.
{
printf("location of =%d\n",find);// display that. the value increases itself.
}
return 0;
}
//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>
int main()
{
enum computer{samsung=1,LG,apple,hp,acer=5}; // list of values of enum. value location starts from 1.
enum computer find; //variable for object "computer"
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 1 to 5.
{
printf("location =%d\n",find);// display that. the value increases itself.
}
return 0;
}
*/
program to use loop in enum data type
using codeblocks
----------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum. value location starts from 0.
enum computer find; //variable for computer
int main()
{
find=acer; // assigning position of value acer in set
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 0 to 4.
{
printf("location of =%d\n",find);// display that
}
return 0;
}
//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>
int main()
{
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum
enum computer find; //variable for computer
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 0 to 4.
{
printf("location =%d\n",find);// display that
}
return 0;
}
*/
//note:- we can also put set of value outside and enum variable inside.
// we can set value inside of set. Then,the other values takes values in increasing order.
----------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum. value location starts from 0.
enum computer find; //variable for computer
int main()
{
find=acer; // assigning position of value acer in set
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 0 to 4.
{
printf("location of =%d\n",find);// display that
}
return 0;
}
//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>
int main()
{
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum
enum computer find; //variable for computer
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 0 to 4.
{
printf("location =%d\n",find);// display that
}
return 0;
}
*/
//note:- we can also put set of value outside and enum variable inside.
// we can set value inside of set. Then,the other values takes values in increasing order.
------------------------------------------------------------------------------
using turboc++
--------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum. value location starts from 0.
enum computer find; //variable for computer
int main()
{
find=acer; // assigning position of value acer in set
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 0 to 4.
{
printf("location of =%d\n",find);// display that
}
return 0;
}
//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>
int main()
{
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum
enum computer find; //variable for computer
for(find=samsung;find<=acer;find++)// find variable runs from samsung to acer. The value runs from 0 to 4.
{
printf("location =%d\n",find);// display that
}
return 0;
}
*/
//note:- we can also put set of value outside and enum variable inside.
// we can set value inside of set. Then,the other values takes values in increasing order.
program to understand enum data type
using codeblocks
---------------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum. value location starts from 0.
enum computer find; //variable for computer
int main()
{
find=acer; // assigning position of value acer in set
printf("location of acer=%d\n",find);// display that
return 0;
}
//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>
int main()
{
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum
enum computer find; //variable for computer
find=acer; // assigning position of value acer in set
printf("location of acer=%d\n",find);// display that
return 0;
}
*/
//note:- we can also put set of value outside and enum variable inside.
---------------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum. value location starts from 0.
enum computer find; //variable for computer
int main()
{
find=acer; // assigning position of value acer in set
printf("location of acer=%d\n",find);// display that
return 0;
}
//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>
int main()
{
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum
enum computer find; //variable for computer
find=acer; // assigning position of value acer in set
printf("location of acer=%d\n",find);// display that
return 0;
}
*/
//note:- we can also put set of value outside and enum variable inside.
----------------------------------------------------------------------------------------------------------------
using turboc++
-----------------------------------------------------------------------------------------------------------------
//enum simple program
#include <stdio.h>
#include <stdlib.h>
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum. value location starts from 0.
enum computer find; //variable for computer
int main()
{
find=acer; // assigning position of value acer in set
printf("location of acer=%d\n",find);// display that
return 0;
}
//or
/*
//enum simple program
#include <stdio.h>
#include <stdlib.h>
int main()
{
enum computer{samsung,LG,apple,hp,acer}; // list of values of enum
enum computer find; //variable for computer
find=acer; // assigning position of value acer in set
printf("location of acer=%d\n",find);// display that
return 0;
}
*/
//note:- we can also put set of value outside and enum variable inside.
program to understand ftell() function
using codeblocks
------------------------------------------------------------------
/*program to 'struct ' concept to store employee id,first name and salary to a data file until you say 'n'.
Then display all those stored records.
*/
#include <stdio.h>
#include<conio.h>
struct employee //struct tag
{
int employee_id;
float employee_salary;
char employee_name[50];
}var; //variable
int main()
{
FILE *k; // pointer for file declaration
char choice; //identifier declaration
k=fopen("employees.txt","w"); //file opening in write mode
do
{
printf("\n enter employee id\n");
scanf("%d",&var.employee_id);
printf("enter employee salary\n");
scanf("%f",&var.employee_salary);
printf("enter employee name\n"); // getting inputs
scanf("%s",var.employee_name);
fwrite(&var,sizeof(var),1,k);; //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche(); //gets a character
}while(choice!='n'); //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
rewind(k);
fclose(k); //closing of file //closing data file
printf("-----------------------\n");
printf("reading data\n"); //opening file in read mode
k=fopen("employees.txt","r"); //file opening
while((fread(&var,sizeof(var),1,k))==1)// fread() reads the records till it is true.
//As it becomes less than 1, it stops
{
printf("employee id=%d,employee salary=%f,employee name=%s\n",var.employee_id,var.employee_salary,var.employee_name);//prints the struct value
}
//location=ftell(k);
printf("the location using ftell=%ld\n",ftell(k));// tells us the location of pointer.
fseek(k,2,SEEK_SET); // sets the pointer to second position
printf("the location after using seek_set=%ld\n",ftell(k));
fseek(k,0,SEEK_CUR); //takes the current location of pointer.
//IF we take any other value than 0 ,
//it adds that to previous location and displays that
printf("the location after using seek_cur=%ld\n",ftell(k));
fseek(k,0,SEEK_END); //takes position of pointer is at the end.
printf("the location after using seek_end=%ld\n",ftell(k));
rewind(k); //takes the pointer to 0 location i.e. beginning
printf("the location=%ld\n",ftell(k));
fclose(k); //closing of file.
getch();
return 0;
}
----------------------------------------------------------------------------------------------------------
using turbo c++
-----------------------------------------------------------
/*program to 'struct ' concept to store employee id,first name and salary to a data file until you say 'n'.
Then display all those stored records.
*/
#include <stdio.h>
#include<conio.h>
struct employee //struct tag
{
int employee_id;
float employee_salary;
char employee_name[50];
}var; //variable
int main()
{
FILE *k; // pointer for file declaration
char choice; //identifier declaration
k=fopen("employees.txt","w"); //file opening in write mode
do
{
printf("\n enter employee id\n");
scanf("%d",&var.employee_id);
printf("enter employee salary\n");
scanf("%f",&var.employee_salary);
printf("enter employee name\n"); // getting inputs
scanf("%s",var.employee_name);
fwrite(&var,sizeof(var),1,k);; //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche(); //gets a character
}while(choice!='n'); //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
rewind(k);
fclose(k); //closing of file //closing data file
printf("-----------------------\n");
printf("reading data\n"); //opening file in read mode
k=fopen("employees.txt","r"); //file opening
while((fread(&var,sizeof(var),1,k))==1)// fread() reads the records till it is true.
//As it becomes less than 1, it stops
{
printf("employee id=%d,employee salary=%f,employee name=%s\n",var.employee_id,var.employee_salary,var.employee_name);//prints the struct value
}
//location=ftell(k);
printf("the location using ftell=%ld\n",ftell(k));// tells us the location of pointer.
fseek(k,2,SEEK_SET); // sets the pointer to second position
printf("the location after using seek_set=%ld\n",ftell(k));
fseek(k,0,SEEK_CUR); //takes the current location of pointer.
//IF we take any other value than 0 ,
//it adds that to previous location and displays that
printf("the location after using seek_cur=%ld\n",ftell(k));
fseek(k,0,SEEK_END); //takes position of pointer is at the end.
printf("the location after using seek_end=%ld\n",ftell(k));
rewind(k); //takes the pointer to 0 location i.e. beginning
printf("the location=%ld\n",ftell(k));
fclose(k); //closing of file.
getch();
return 0;
}
------------------------------------------------------------------
/*program to 'struct ' concept to store employee id,first name and salary to a data file until you say 'n'.
Then display all those stored records.
*/
#include <stdio.h>
#include<conio.h>
struct employee //struct tag
{
int employee_id;
float employee_salary;
char employee_name[50];
}var; //variable
int main()
{
FILE *k; // pointer for file declaration
char choice; //identifier declaration
k=fopen("employees.txt","w"); //file opening in write mode
do
{
printf("\n enter employee id\n");
scanf("%d",&var.employee_id);
printf("enter employee salary\n");
scanf("%f",&var.employee_salary);
printf("enter employee name\n"); // getting inputs
scanf("%s",var.employee_name);
fwrite(&var,sizeof(var),1,k);; //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche(); //gets a character
}while(choice!='n'); //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
rewind(k);
fclose(k); //closing of file //closing data file
printf("-----------------------\n");
printf("reading data\n"); //opening file in read mode
k=fopen("employees.txt","r"); //file opening
while((fread(&var,sizeof(var),1,k))==1)// fread() reads the records till it is true.
//As it becomes less than 1, it stops
{
printf("employee id=%d,employee salary=%f,employee name=%s\n",var.employee_id,var.employee_salary,var.employee_name);//prints the struct value
}
//location=ftell(k);
printf("the location using ftell=%ld\n",ftell(k));// tells us the location of pointer.
fseek(k,2,SEEK_SET); // sets the pointer to second position
printf("the location after using seek_set=%ld\n",ftell(k));
fseek(k,0,SEEK_CUR); //takes the current location of pointer.
//IF we take any other value than 0 ,
//it adds that to previous location and displays that
printf("the location after using seek_cur=%ld\n",ftell(k));
fseek(k,0,SEEK_END); //takes position of pointer is at the end.
printf("the location after using seek_end=%ld\n",ftell(k));
rewind(k); //takes the pointer to 0 location i.e. beginning
printf("the location=%ld\n",ftell(k));
fclose(k); //closing of file.
getch();
return 0;
}
----------------------------------------------------------------------------------------------------------
using turbo c++
-----------------------------------------------------------
/*program to 'struct ' concept to store employee id,first name and salary to a data file until you say 'n'.
Then display all those stored records.
*/
#include <stdio.h>
#include<conio.h>
struct employee //struct tag
{
int employee_id;
float employee_salary;
char employee_name[50];
}var; //variable
int main()
{
FILE *k; // pointer for file declaration
char choice; //identifier declaration
k=fopen("employees.txt","w"); //file opening in write mode
do
{
printf("\n enter employee id\n");
scanf("%d",&var.employee_id);
printf("enter employee salary\n");
scanf("%f",&var.employee_salary);
printf("enter employee name\n"); // getting inputs
scanf("%s",var.employee_name);
fwrite(&var,sizeof(var),1,k);; //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche(); //gets a character
}while(choice!='n'); //writing repeats as you enter 'y'
printf("\n writing process completed successfully\n");
rewind(k);
fclose(k); //closing of file //closing data file
printf("-----------------------\n");
printf("reading data\n"); //opening file in read mode
k=fopen("employees.txt","r"); //file opening
while((fread(&var,sizeof(var),1,k))==1)// fread() reads the records till it is true.
//As it becomes less than 1, it stops
{
printf("employee id=%d,employee salary=%f,employee name=%s\n",var.employee_id,var.employee_salary,var.employee_name);//prints the struct value
}
//location=ftell(k);
printf("the location using ftell=%ld\n",ftell(k));// tells us the location of pointer.
fseek(k,2,SEEK_SET); // sets the pointer to second position
printf("the location after using seek_set=%ld\n",ftell(k));
fseek(k,0,SEEK_CUR); //takes the current location of pointer.
//IF we take any other value than 0 ,
//it adds that to previous location and displays that
printf("the location after using seek_cur=%ld\n",ftell(k));
fseek(k,0,SEEK_END); //takes position of pointer is at the end.
printf("the location after using seek_end=%ld\n",ftell(k));
rewind(k); //takes the pointer to 0 location i.e. beginning
printf("the location=%ld\n",ftell(k));
fclose(k); //closing of file.
getch();
return 0;
}
program to understand rename() function
using codeblocks
-------------------------------------------------------------------------------------------------
//understanding rename() function
#include <stdio.h>
#include<conio.h>
int main()
{
char first_name[40],second_name[40];
printf("enter file name with extension .txt\n");
gets(first_name);
printf("enter name to be changed\n");
gets(second_name);
rename("first_name","second_name");
printf("the entered file renamed successfully");
getch();
return 0;
}
//note: We do not need to open file here. We can do without opening too(from line 19).
/*i.e
//understanding rename() function
#include <stdio.h>
#include<conio.h>
int main()
{
char first_name[40],second_name[50];
printf("enter file name with extension .txt\n");
gets(first_name);
printf("enter name to be changed\n");
gets(second_name);
rename("first_name","second_name");
printf("the entered file renamed successfully");
getch();
return 0;
}
*/
-------------------------------------------------------------------------------------------------
//understanding rename() function
#include <stdio.h>
#include<conio.h>
int main()
{
char first_name[40],second_name[40];
printf("enter file name with extension .txt\n");
gets(first_name);
printf("enter name to be changed\n");
gets(second_name);
rename("first_name","second_name");
printf("the entered file renamed successfully");
getch();
return 0;
}
//note: We do not need to open file here. We can do without opening too(from line 19).
/*i.e
//understanding rename() function
#include <stdio.h>
#include<conio.h>
int main()
{
char first_name[40],second_name[50];
printf("enter file name with extension .txt\n");
gets(first_name);
printf("enter name to be changed\n");
gets(second_name);
rename("first_name","second_name");
printf("the entered file renamed successfully");
getch();
return 0;
}
*/
-------------------------------------
same program, we can use for turboc++ as well