Search

Showing posts with label C language codes. Show all posts
Showing posts with label C language codes. Show all posts

My Project using file handling in C

6 comments

#include
#include
#include

char log,sign,a;
int x,c,m,z[20];
int choice;

int main()
{

char pass[30];
char filename[30];
int balik=1;
char line1[20];
char line2[20];
char line3[20];
char line4[20];
char line5[20];
char line6[20];
int code;
char fid[20];
char fpass[20];
FILE *f;


printf("sign-in to YAHOOMAIL!\n");
printf("you are protected\n\n");
system("pause");


while(balik==1)
{
printf("enter ID:");
gets(filename);
///1
if((f=fopen(filename,"r"))!=NULL)
{
printf("error! ID exist!\n");

balik=1;
}

///1
else
{
///2
if((f=fopen(filename,"w"))==NULL)
{
printf("error ID\n");
balik=1;
}
//2
else
{


//information center

f=(fopen(filename,"w"));
printf("last name:");
gets(line1);
fprintf(f,"last name:%s\n\n",line1);


printf("first name:");
gets(line2);
fprintf(f,"first name:%s\n\n",line2);


printf("middle name:");
gets(line3);
fprintf(f,"middle name:%s\n\n",line3);


printf("age:");
gets(line4);
fprintf(f,"age:%s\n\n",line4);


printf("gender:");
gets(line5);
fprintf(f,"gender:%s\n\n",line5);


printf("Email Address:");
gets(line6);
fprintf(f,"Email Address:%s\n\n",line6);
fclose(f);

printf("PASSWORD:");
scanf("%s",pass);
f=(fopen(pass,"a"));
fclose(f);

ret:
printf("\n\nJust a couple more details...\n Type the code shown\n\n");
printf("do not input character!\n\n");
printf(" __________________\n");
printf("| |\n");
printf("| 2253028 |\n");
printf("|__________________|\n\n");


printf("type the codes:");
scanf("%d",&code);
///3
switch (code)

{
///3
case 2253028:
printf("\n\nsuccessfully signed\n\n");
system("pause");
goto alog;
///3
case 2:
printf("error");
system("cls");
goto ret;
///3
default:
system("cls");
goto ret;
}
alog:
system("cls");
for (m=0;m<=44;m++)
printf("%d",m);

printf("\n\n\n\n\n\n\n\n\n\n\n\t\t\t\tlog-in?[y/n]:");
scanf("%s",&log);
///4
switch (log)
{
///4
case 'y':
{
final:
system("cls");

printf("enter ID:");
scanf("%s",fid);
printf("enter PASSWORD:");
scanf("%s",fpass);
///5
if ((strcmp(fid,filename)==0)&&(strcmp(fpass,pass)==0))
{

main:
system("cls");

printf("\n\n\n\n\n view account[1]\n\n");
printf("military time [2]\n\n");
printf("log-out [3]\n\n\n\n\n");
printf("enter your choice:");
scanf("%d",&choice);

switch (choice)
{
case 1:
{

system("cls");
f=(fopen(filename,"r"));
fgets(line1,20,f);
fgets(line2,20,f);
fgets(line3,20,f);
fgets(line4,20,f);
fgets(line5,20,f);
fgets(line6,20,f);


printf("your account:\n\n");
puts(line1);
puts(line2);
puts(line3);
puts(line4);
puts(line5);
puts(line6);

system("pause");

goto main;
break;

}
case 2:
{
system("cls");




printf("enter your name:");
scanf("%s",z);





printf("hello %s have fun! enjoy...\n\n",z);

printf("enter your military time:");
scanf("%d",&x);
/*ACAS ARNEL M.*/

if(x==1300)
{
printf("1:00 pm");
system("cls");
system("pause");
goto main;
}
if((x>=1301)&&(x<=1399))
{
c=x-1300;
printf("1:%dpm",c);
system("pause");
goto main;
}
if(x==1400)
{
printf("2:00 pm");
system("pause");
goto main;
}
if((x>=1401)&&(x<=1499))
{
c=x-1400;
printf("2:%d pm",c);
system("pause");
goto main;
}
if(x==1500)
{
printf("3:00 pm");
system("pause");
goto main;
}
if((x>=1501)&&(x<=1599))
{
c=x-1500;
printf("3:%d pm",c);
system("pause");
goto main;
}
if(x==1600)
{
printf("4:00 pm");
system("pause");
goto main;
}
if((x>=1601)&&(x<=1699))
{
c=x-1600;
printf("4:%d pm",c);
system("pause");
goto main;
}
if(x==1700)
{
printf("5:00 pm");
system("pause");
goto main;
}
if((x>=1701)&&(x<=1799))
{
c=x-1700;
printf("5:%d pm",c);
system("pause");
goto main;
}
if(x==1800)
{
printf("6:00 pm");
system("pause");
goto main;
}
if((x>=1801)&&(x<=1899))
{
c=x-1800;
printf("6:%d pm",c);
system("pause");
goto main;
}
if(x==1900)
{
printf("7:00 pm");
system("pause");
goto main;
}
if((x>=1901)&&(x<=1999))
{
c=x-1900;
printf("7:%d pm",c);
system("pause");
goto main;
}
if(x==2000)
{
printf("8:00");
system("pause");
goto main;
}
if((x>=2001)&&(x<=2099))
{
c=x-2000;
printf("8:%d pm",c);
system("pause");
goto main;
}
if(x==2100)
{
printf("9:00 pm");
system("pause");
goto main;
}
if((x>=2101)&&(x<=2199))
{
c=x-2100;
printf("9:%d pm",c);
system("pause");
goto main;
}
if(x==2200)
{
printf("10:00 pm");
system("pause");
goto main;
}
if((x>=2201)&&(x<=2299))
{
c=x-2200;
printf("10:%d pm",c);
system("pause");
goto main;
}
if(x==2300)
{
printf("11:00 pm");
system("pause");
goto main;
}
if((x>=2301)&&(x<=2399))
{
c=x-2300;
printf("11:%d pm");
system("pause");
goto main;
}
if(x==2400)
{
printf("12:00 pm");
system("pause");
goto main;
}
if((x>=2401)&&(x<=2499))
{
c=x-2400;
printf("12:%d pm",c);
system("pause");
goto main;
}


if(x==100)
{
printf("1:00 am");
system("pause");
goto main;
}
if((x>=101)&&(x<=199))
{
c=x-100;
printf("1:%d am",c);
system("pause");
goto main;
}
if(x==200)
{
printf("2:00 am");
system("pause");
goto main;
}
if((x>=201)&&(x<=299))
{
c=x-200;
printf("2:%d am",c);
system("pause");
goto main;
}
if(x==300)
{
printf("3:00 am");
system("pause");
goto main;
}
if((x>=301)&&(x<=399))
{
c=x-300;
printf("3:%d am",c);
system("pause");
goto main;
}
if(x==400)
{
printf("4:00 am");
system("pause");
goto main;
}
if((x>=401)&&(x<=499))
{
c=x-400;
printf("4:%d am",c);
system("pause");
goto main;
}
if(x==500)
{
printf("5:00 am");
system("pause");
goto main;
}
if((x>=501)&&(x<=599))
{
c=x-500;
printf("5:%d am",c);
system("pause");
goto main;
}
if(x==600)
{
printf("6:00 am");
system("pause");
goto main;
}
if((x>=601)&&(x<=699))
{
c=x-600;
printf("6:%d am",c);
system("pause");
goto main;
}
if(x==700)
{
printf("7:00 am");
system("pause");
goto main;
}
if((x>=701)&&(x<=799))
{
c=x-700;
printf("7:%d am",c);
system("pause");
goto main;
}
if(x==800)
{
printf("8:00 am");
system("pause");
goto main;
}
if((x>=801)&&(x<=899))
{
c=x-800;
printf("8:%d am",c);
system("pause");
goto main;
}
if(x==900)
{
printf("9:00 am");
system("pause");
goto main;
}
if((x>=901)&&(x<=999))
{
c=x-900;
printf("9:%d am",c);
system("pause");
goto main;
}
if(x==1000)
{
printf("10:00 pm");
system("pause");
goto main;
}
if((x>=1001)&&(x<=1099))
{
c=x-1000;
printf("10:%d am");
system("pause");
goto main;
}
if(x==1100)
{
printf("11:00 am");
system("pause");
goto main;
}
if((x>=1101)&&(x<=1199))
{
c=x-1100;
printf("11:%d am",c);
system("pause");
goto main;
}
if(x==1200)
{
printf("12:00 pm");
system("pause");
goto main;
}
if((x>=1201)&&(x<=1299))
{
c=x-1200;
printf("12:%d pm",c);
system("pause");
goto main;
}
else
{
printf("error input! guys....\n\n\n\n");
system("pause");
goto main;
return 0;
}


goto main;
}
case 3:
{
system("cls");
printf("\n\n\nsuccessfully log-out!");

return 0;
}
default:
{
goto main;
}
}














}
///5
else
{
system("cls");
printf("user no register!\n\n");
goto final;
}

break;
}
///4
case 'n':
{
printf("bye bye.... see you soon!");
break;
}
///4
default:
{
goto alog;
}

}






















//f=(fopen("password.txt","w"));
// printf("PASSWORD:");
// scanf("%s",filename);
// fclose(f);



// f=(fopen(filename,"r"));
// fgets(line,500,f);
// printf("the line you enter:");
// puts(line);

break;
}
}
}
return 0;
}

converts text into binary using the division method through ASCII code

0 comments

#include
using namespace std;
#include
#include

char *entry, letter, choice[2];
int ascii, len, binary[8], total;
void prog();

int main()
{
prog();
return 0;
}

void prog()
{
entry = new char[501];
/* entry should be dynamic, otherwise a new
string entry of 501 chars would be created
each time function is called!
Talk about memory hog! */
cout<<"Enter string to convert (up to 500 chars): "; cin.getline(entry, 500); len = strlen(entry); /* get the number of characters in entry. */ /* this loop is executed for each letter in the string. */ for(int i = 0; i0) /* This while loop converts the ASCII # into binary,
stores it backwards into the binary array. */
{
/* To get the binary code one must take the decimal number in
question, take it and divide it by two repeatedly, save
the remainder (which will become the binary number), save
the whole number, divide by two, and repeat the whole
process until 0 is reached. This if-else statement serves
this functionality, by getting the remainder of the ascii
code, storing it in the array and then dividing the int
ascii by two */
if((ascii%2)==0)
{
binary[total] = 0;
ascii = ascii/2;
total++; /* increasing by one each time will yeild the
number of numbers in the array. */
}
else
{
binary[total] = 1;
ascii = ascii/2;
total++;
}
}
total--; /* due to data type factors, the program will actually
add a 0 at the end of the array that is not supposed
to be there, decrementing total will solve this
problem, as that 0 will not be displayed. */
/* this while loop displays the binary code for that letter. */
while(total>=0)
{
cout<
total--;
}
}
delete[] entry; /* free up the memory used by entry */
cout<<<"Do again(1 = yes, 2= no)?: ";
cin.getline(choice,3);
if(choice[0] == '1')
prog(); /* program is recursive, it calls itself. It's kinda
like a function loop of sorts. */
else
exit(0); /* quits the program */
}

Factorial Permutation and Combination in C

0 comments

#include

int factor (int num)
{
if (num>1)
{
return (num * factor(num-1));
}
else{}
return (1);
}

int main(void)
{

int 1num,2num,permdin,permfact;
int den,comdin,comfact,sub1,sub2;

printf("Enter First Number:");
scanf("%d",&1num);
printf("%d = %d \n\n\n\n\n\n\n",1num,factor(1num));
printf("Enter Second Number:");
scanf("%d",&2num);
printf("%d = %d \n\n",2num,factor(2num));

if(1num>2num)
{
sub1=1num-2num;
permdin=factor(sub1);
permfact=(factor(1num)/permdin);
printf("Permutation = %d\n\n",permfact);



printf( "Combination : %d",factor(1num) / (factor(2num)* factor(1num-2num)));



printf("\n\n\n");
}
else
{
printf("Invalid:\n\n");

}
return 0;
}

Calloc in C

0 comments

/* SAGpa Sa bala Kan-a */
#include
#include

int main ()
{
int i,n;
int * pData;
printf ("Amount of numbers to be entered: ");
scanf ("%d",&i);
pData = (int*) calloc (i,sizeof(int));
if (pData==NULL) exit (1);
for (n=0;n
{
printf ("Enter number #%d: ",n);
scanf ("%d",&pData[n]);
}
printf ("You have entered: ");
for (n=0;n
free (pData);
return 0;
}



Malloc in C

2 comments

#include
#include

int strlen(char *s)
{
int n;
for(n=0;*s!='\0';s++)
n++;
return n;
}

int main()
{
/*char s[] = "JohnBridge my Heart";
printf("length = %d\n",strlen(s));
*/
int n;
char *s;
n = strlen("JohnBridge my Heart");
s = (char *)malloc(n*sizeof(char));
*s = "JohnBridge my Heart";
printf("length = %d\n",strlen(s));
return 0;
}

Multiplication Table in C

0 comments

#include

int main()
{
//char ans;
int column,row,i,j;
printf("rows:");
scanf("%d",&row);
printf("column:");
scanf("%d",&column);
i=0;
while(i
{
j=0;
printf("%d\t",(i+1));
while(j
{
printf("%d\t",(i+1)*(j+1));
j++;
}
printf("\n");
i++;
}
return 0;
}

File manipulation in C

0 comments

#include
#include

int main()
{
FILE *fp;
int returnvar = 1;
char ans = 'y';
char filename[20];
char statement[30];
while(returnvar!=0)
{
printf("enter filename: ");
scanf("%s",filename);
printf("\nWriting to %s...\n\n",filename);
if((fp=fopen(filename,"r"))!=NULL)
{
printf("error: file already exists\n");
returnvar = 1;
exit(1);
}
else
{
if((fp=fopen(filename,"w+"))==NULL)
{
printf("error creating file\n");
returnvar = 1;
exit(1);
}
else
{
printf("writing successful\n");
break;
}
}
}
while(ans!='n')
{
printf("enter a statement to write to file of not more than 30 characters");
scanf("%s",statement);
fprintf(fp,statement);
fprintf(fp,"\n");
printf("input another statement? ");
scanf("%c",&ans);
}
fclose(fp);
return 0;
}

Output Random Numbers

0 comments

#include
#include
#include
int x,y[5],i,temp,k;

int main(void)

{

srand(time(NULL));
for (i=1;i<=5;i++)
{

printf("%d\n",x=rand()%10);
y[i]=x;
}

printf("\n\n");

for (i=1;i<=5;i++)
{

for (k=i;k<=5;k++)

{
if (y[i]>y[k])

{
temp=y[i];
y[i]=y[k];
y[k]=temp;
}
}


}
for (i=1;i<=5;i++)
{
printf("%d\n",y[i]);

}






return(0);
}