#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <windows.h>
int board[10] = {2,2,2,2,2,2,2,2,2,2};
int turn = 1,flag = 0;
int player,comp;
void menu();
void go(int n);
void start_game();
void check_draw();
void draw_board();
void player_first();
void put_X_O(char ch,int pos);
COORD coord={0,0}; // this is global variable
//center of axis is set to the top left cornor of the screen
void gotoxy(int x,int y)
{
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void main()
{
system("cls");
menu();
getch();
}
void menu()
{
int choice;
system("cls");
printf("\n--------MENU--------");
printf("\n1 : Play with X");
printf("\n2 : Play with O");
printf("\n3 : Exit");
printf("\nEnter your choice:>");
scanf("%d",&choice);
turn = 1;
switch (choice)
{
case 1:
player = 1;
comp = 0;
player_first();
break;
case 2:
player = 0;
comp = 1;
start_game();
break;
case 3:
exit(1);
default:
menu();
}
}
int make2()
{
if(board[5] == 2)
return 5;
if(board[2] == 2)
return 2;
if(board[4] == 2)
return 4;
if(board[6] == 2)
return 6;
if(board[8] == 2)
return 8;
return 0;
}
int make4()
{
if(board[1] == 2)
return 1;
if(board[3] == 2)
return 3;
if(board[7] == 2)
return 7;
if(board[9] == 2)
return 9;
return 0;
}
int posswin(int p)
{
// p==1 then X p==0 then O
int i;
int check_val,pos;
if(p == 1)
check_val = 18;
else
check_val = 50;
i = 1;
while(i<=9)//row check
{
if(board[i] * board[i+1] * board[i+2] == check_val)
{
if(board[i] == 2)
return i;
if(board[i+1] == 2)
return i+1;
if(board[i+2] == 2)
return i+2;
}
i+=3;
}
i = 1;
while(i<=3)//column check
{
if(board[i] * board[i+3] * board[i+6] == check_val)
{
if(board[i] == 2)
return i;
if(board[i+3] == 2)
return i+3;
if(board[i+6] == 2)
return i+6;
}
i++;
}
if(board[1] * board[5] * board[9] == check_val)
{
if(board[1] == 2)
return 1;
if(board[5] == 2)
return 5;
if(board[9] == 2)
return 9;
}
if(board[3] * board[5] * board[7] == check_val)
{
if(board[3] == 2)
return 3;
if(board[5] == 2)
return 5;
if(board[7] == 2)
return 7;
}
return 0;
}
void go(int n)
{
if(turn % 2)
board[n] = 3;
else
board[n] = 5;
turn++;
}
void player_first()
{
int pos;
check_draw();
draw_board();
gotoxy(30,18);
printf("Your Turn :> ");
scanf("%d",&pos);
if(board[pos] != 2)
player_first();
if(pos == posswin(player))
{
go(pos);
draw_board();
gotoxy(30,20);
//textcolor(128+RED);
printf("Player Wins");
getch();
exit(0);
}
go(pos);
draw_board();
start_game();
}
void start_game()
{
// p==1 then X p==0 then O
if(posswin(comp))
{
go(posswin(comp));
flag = 1;
}
else
if(posswin(player))
go(posswin(player));
else
if(make2())
go(make2());
else
go(make4());
draw_board();
if(flag)
{
gotoxy(30,20);
//textcolor(128+RED);
printf("Computer wins");
getch();
}
else
player_first();
}
void check_draw()
{
if(turn > 9)
{
gotoxy(30,20);
//textcolor(128+RED);
printf("Game Draw");
getch();
exit(0);
}
}
void draw_board()
{
int j;
for(j=9;j<17;j++)
{
gotoxy(35,j);
printf("| |");
}
gotoxy(28,11);
printf("-----------------------");
gotoxy(28,14);
printf("-----------------------");
for(j=1;j<10;j++)
{
if(board[j] == 3)
put_X_O('X',j);
else
if(board[j] == 5)
put_X_O('O',j);
}
}
void put_X_O(char ch,int pos)
{
int m;
int x = 31, y = 10;
m = pos;
if(m > 3)
{
while(m > 3)
{
y += 3;
m -= 3;
}
}
if(pos % 3 == 0)
x += 16;
else
{
pos %= 3;
pos--;
while(pos)
{
x+=8;
pos--;
}
}
gotoxy(x,y);
printf("%c",ch);
}
AITB International Conference, 2019
Kathmandu, Nepal
My Youtube Channel
Please Subscribe
Flag of Nepal
Built in OpenGL
World Covid-19 Data Visualization
Choropleth map
Word Cloud in Python
With masked image
Monday, July 31, 2017
Simple quiz in C
#include<stdio.h>
#include<conio.h>
#include <windows.h>
#include<ctype.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
void displayscore()
{
char name[20];
float s;
FILE *f;
system("cls");
f=fopen("score.txt","r");
fscanf(f,"%s%f",&name,&s);
printf("\n\n\t\t ");
printf("\n\n\t\t %s has secured the Highest Score %.2f",name,s);
printf("\n\n\t\t ");
fclose(f);
getch();
}
void help()
{
system("cls");
printf("\n\n\n\tThis game is very easy to play. You'll be asked some general");
printf("\n\n\tknowledge questions and the right answer is to be chosen among");
printf("\n\n\tthe four options provided. Your score will be calculated at the");
printf("\n\n\tend. Remember that the more quicker you give answer the more");
printf("\n\n\tscore you will secure. Your score will be calculated and displayed");
printf("\n\n\tat the end and displayed. If you secure highest score, your score");
printf("\n\n\twill be recorded. So BEST OF LUCK.");
}
void writescore(float score, char plnm[20])
{
float sc;
char nm[20];
FILE *f;
system("cls");
f=fopen("score.txt","r");
fscanf(f,"%s%f",&nm,&sc);
if (score>=sc)
{ sc=score;
fclose(f);
f=fopen("score.txt","w");
fprintf(f,"%s\n%.2f",plnm,sc);
fclose(f);
}
}
int main()
{
int countq,countr;
int r,i;
int pa;int nq[6];int w;
float score;
char choice;
char playername[20];
time_t initialtime,finaltime;
system("cls");
//randomize();
mainhome:
system("cls");
puts("\n\t\t WELCOME TO I.Q. TEST PROGRAM\n\n") ;
puts("\n\t\t-------------------------------");
puts("\n\t\t Enter 'S' to start game ");
puts("\n\t\t Enter 'V' to view high score ");
puts("\n\t\t Enter 'H' for help ");
puts("\n\t\t Enter 'Q' to quit ");
printf("\n\t\t-------------------------------\n\n\t\t ");
choice=toupper(getch());
if (choice=='V')
{
displayscore();
goto mainhome;
}
else if (choice=='Q')
exit(1);
else if (choice=='H')
{
help();
getch();
goto mainhome;
}
else if(choice=='S'){
system("cls");
printf("\n\n\n\t\t\tEnter your name...");
printf("\n\t\t\t(only one word)\n\n\t\t\t");
gets(playername);
home:
system("cls");
initialtime=time(NULL);
countq=countr=0;
i=1;
start:
srand ( time(NULL) );
r=rand()%23+1;
nq[i]=r;
for (w=0;w<i;w++)
if (nq[w]==r) goto start;
switch(r)
{
case 1:
printf("\n\nWhat is the maximum no. of asymptotes of the curve x^4+2x+6=0?");
printf("\n\nA.4\tB.3\n\nC.none\tD.infinite\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.4");break;}
case 2:
printf("\n\n\nHow many points are possible in a compound pendulum about which");
printf("time period is same?");
printf("\n\nA.4\tB.2\n\nC.none\tD.infinite\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
printf("\n\nWrong!!! The correct answer is A.4");
break;
case 3:
printf("\n\n\nWho was the first US President?");
printf("\n\nA.Richard Nikson\tB.Abraham Linkon\n\nC.John F. Kennedy\tD.George Washington\n\n");
countq++;
if (toupper(getch())=='D')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is D.George Washington");break;}
case 4:
printf("\n\n\nWho was awarded the 'Man of the Tournament' of ICC WORLD CUP 2007?");
printf("\n\nA.Glen Magrath\tB.Mahela Jawardan\n\nC.Mathew Hayden\tD.Sachin Tendulkar\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.Glen Magrath");break;}
case 5:
printf("\n\n\nWhich country won the Fifa World Cup 1998?");
printf("\n\nA.France\tB.Brazil\n\nC.Italy\tD.England\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.France");break;}
case 6:
printf("\n\n\nWhich syllabe is stressed in the word 'democracy'?");
printf("\n\nA.1st\tB.2nd\n\nC.3rd\tD.4th\n\n");
countq++;
if (toupper(getch())=='B' )
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is B.2nd");break;}
case 7:
printf("\n\n\nWhich country was the winner of Cricket World Cup 1987?");
printf("\n\nA.West Indies\tB.India\n\nC.Australia\tD.England\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.Australia");break;}
case 8:
printf("\n\n\nWhat is the height of Mount everest in feet?");
printf("\n\nA.8648\tB.6648\n\nC.8884\tD.8848\n\n");
countq++;
if (toupper(getch())=='D')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is D.8848");break;}
case 9:
printf("\n\n\nWhat is the capital of Denmark?");
printf("\n\nA.Copenhagen\tB.Helsinki\n\nC.Rome\t\tD.Madrid\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.Copenhagen");break;}
case 10:
printf("\n\n\nWhich syllabe is stressed in the word 'instanteneous'?");
printf("\n\nA.1st\tB.2nd\n\nC.3rd\tD.4th\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.3rd");break;}
case 11:
printf("\n\n\nWho was the only player to score 6 successive sixes in an over?");
printf("\n\nA.Adam Gilchrist\tB.M.S.Dhoni\n\nC.Herschel Gibbs\tD.Sanath Jayasurya\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.Herschel Gibbs");break;}
case 12:
printf("\n\n\nWho was the only player to take 4 successive wickets?");
printf("\n\nA.Malinga Bandara\tB.Lasith Malinga\n\nC.Bret Lee\tD.Murali Daran\n\n");
countq++;
if (toupper(getch())=='B')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is B.Lasith Malinga");break;}
case 13:
printf("\n\n\nWhich country is hosting the Fifa World Cup 2010?");
printf("\n\nA.South Africa\tB.Italy\n\nC.Argentina\tD.Spain\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.South Africa");break;}
case 14:
printf("\n\n\nWho is the author of 'Pulpasa Cafe'?");
printf("\n\nA.Narayan Wagle\tB.Lal Gopal Subedi\n\nC.B.P. Koirala\tD.Khagendra Sangraula\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.Narayan Wagle");break;}
case 15:
printf("\n\n\nWhich country is Maria Sarapova from?");
printf("\n\nA.Russia\tB.Switzerland\n\nC.Argentina\tD.Spain\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.Russia");break;}
case 16:
printf("\n\n\nWho was awarded the youngest player award in Fifa World Cup 2006?");
printf("\n\nA.Wayne Rooney\tB.Lucas Podolski\n\nC.Lionel Messi\tD.Christiano Ronaldo\n\n");
countq++;
if (toupper(getch())=='B')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is B.Lucas Podolski");break;}
case 17:
printf("\n\n\nWhat is the smallest district of Nepal?");
printf("\n\nA.Lalitpur\tB.Karnali\n\nC.Bhaktapur\tD.Gulmi\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.Bhaktapur");break;}
case 18:
printf("\n\n\nWhat is the headquarter of Western Development Region?");
printf("\n\nA.Dhankuta\tB.Kathmandu\n\nC.Dhangadhi\tD.Pokhara\n\n");
countq++;
if (toupper(getch())=='D')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is D.Pokhara");break;}
case 19:
printf("\n\n\nWhich place is called 'The Cherrapunji of Nepal'?");
printf("\n\nA.Dharan\tB.Kathmandu\n\nC.Pokhara\tD.Butwal\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.Pokhara");break;}
case 20:
printf("\n\n\nWhich city is known at 'The City of Seven Hills'?");
printf("\n\nA.Rome\tB.Vactican City\n\nC.Madrid\tD.Berlin\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.Rome");break;}
case 21:
printf("\n\n\nWho was the F1 racing champion of 2006?");
printf("\n\nA.Louis Hamilton\tB.Felipe Massa\n\nC.Fernando Alonso\tD.Michael Schumaker\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.Fernanda Alonso");break;}
case 22:
printf("\n\n\nWho won the Women Australian Open 2007?");
printf("\n\nA.Martina Hingis\tB.Maria Sarapova\n\nC.Kim Clijster\tD.Serena Williams\n\n");
countq++;
if (toupper(getch())=='D')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is D.Serena Williams");break;}
case 23:
printf("\n\n\nName the country where there no mosquito is found?");
printf("\n\nA.Germany\tB.Spain\n\nC.Japan\tD.France\n\n");
countq++;
if (toupper(getch())=='D')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is D.France");break;}
}
i++;
if (i<=5) goto start;
finaltime=time(NULL);
score=(float)countr/countq*100-difftime(finaltime,initialtime)/3;
if (score<0) score=0;
printf("\n\n\nYour Score: %.2f",score);
if (score==100) printf("\n\nEXCELLENT!!! KEEP IT UP");
else if (score>=80 && score<100) printf("\n\nVERY GOOD!!");
else if (score>=60 &&score<80) printf("\n\nGOOD! BUT YOU NEED TO KNOW MORE.");
else if (score>=40 && score<60) printf("\n\nSATISFACTORY RESULT, BUT THIS MUCH IS MUCH SUFFICIENT.");
else printf("\n\nYOU ARE VERY POOR IN G.K.,WORK HARD");
puts("\n\nNEXT PLAY?(Y/N)");
if (toupper(getch())=='Y')
goto home;
else
{
writescore(score,playername);
goto mainhome;
}
}
else
{
printf("\n\n\t\t Enter the right key\n\n\t\t ");
Sleep(700);
goto mainhome;
}
return 0;
}
#include<conio.h>
#include <windows.h>
#include<ctype.h>
#include<stdlib.h>
#include<time.h>
#include<dos.h>
void displayscore()
{
char name[20];
float s;
FILE *f;
system("cls");
f=fopen("score.txt","r");
fscanf(f,"%s%f",&name,&s);
printf("\n\n\t\t ");
printf("\n\n\t\t %s has secured the Highest Score %.2f",name,s);
printf("\n\n\t\t ");
fclose(f);
getch();
}
void help()
{
system("cls");
printf("\n\n\n\tThis game is very easy to play. You'll be asked some general");
printf("\n\n\tknowledge questions and the right answer is to be chosen among");
printf("\n\n\tthe four options provided. Your score will be calculated at the");
printf("\n\n\tend. Remember that the more quicker you give answer the more");
printf("\n\n\tscore you will secure. Your score will be calculated and displayed");
printf("\n\n\tat the end and displayed. If you secure highest score, your score");
printf("\n\n\twill be recorded. So BEST OF LUCK.");
}
void writescore(float score, char plnm[20])
{
float sc;
char nm[20];
FILE *f;
system("cls");
f=fopen("score.txt","r");
fscanf(f,"%s%f",&nm,&sc);
if (score>=sc)
{ sc=score;
fclose(f);
f=fopen("score.txt","w");
fprintf(f,"%s\n%.2f",plnm,sc);
fclose(f);
}
}
int main()
{
int countq,countr;
int r,i;
int pa;int nq[6];int w;
float score;
char choice;
char playername[20];
time_t initialtime,finaltime;
system("cls");
//randomize();
mainhome:
system("cls");
puts("\n\t\t WELCOME TO I.Q. TEST PROGRAM\n\n") ;
puts("\n\t\t-------------------------------");
puts("\n\t\t Enter 'S' to start game ");
puts("\n\t\t Enter 'V' to view high score ");
puts("\n\t\t Enter 'H' for help ");
puts("\n\t\t Enter 'Q' to quit ");
printf("\n\t\t-------------------------------\n\n\t\t ");
choice=toupper(getch());
if (choice=='V')
{
displayscore();
goto mainhome;
}
else if (choice=='Q')
exit(1);
else if (choice=='H')
{
help();
getch();
goto mainhome;
}
else if(choice=='S'){
system("cls");
printf("\n\n\n\t\t\tEnter your name...");
printf("\n\t\t\t(only one word)\n\n\t\t\t");
gets(playername);
home:
system("cls");
initialtime=time(NULL);
countq=countr=0;
i=1;
start:
srand ( time(NULL) );
r=rand()%23+1;
nq[i]=r;
for (w=0;w<i;w++)
if (nq[w]==r) goto start;
switch(r)
{
case 1:
printf("\n\nWhat is the maximum no. of asymptotes of the curve x^4+2x+6=0?");
printf("\n\nA.4\tB.3\n\nC.none\tD.infinite\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.4");break;}
case 2:
printf("\n\n\nHow many points are possible in a compound pendulum about which");
printf("time period is same?");
printf("\n\nA.4\tB.2\n\nC.none\tD.infinite\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
printf("\n\nWrong!!! The correct answer is A.4");
break;
case 3:
printf("\n\n\nWho was the first US President?");
printf("\n\nA.Richard Nikson\tB.Abraham Linkon\n\nC.John F. Kennedy\tD.George Washington\n\n");
countq++;
if (toupper(getch())=='D')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is D.George Washington");break;}
case 4:
printf("\n\n\nWho was awarded the 'Man of the Tournament' of ICC WORLD CUP 2007?");
printf("\n\nA.Glen Magrath\tB.Mahela Jawardan\n\nC.Mathew Hayden\tD.Sachin Tendulkar\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.Glen Magrath");break;}
case 5:
printf("\n\n\nWhich country won the Fifa World Cup 1998?");
printf("\n\nA.France\tB.Brazil\n\nC.Italy\tD.England\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.France");break;}
case 6:
printf("\n\n\nWhich syllabe is stressed in the word 'democracy'?");
printf("\n\nA.1st\tB.2nd\n\nC.3rd\tD.4th\n\n");
countq++;
if (toupper(getch())=='B' )
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is B.2nd");break;}
case 7:
printf("\n\n\nWhich country was the winner of Cricket World Cup 1987?");
printf("\n\nA.West Indies\tB.India\n\nC.Australia\tD.England\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.Australia");break;}
case 8:
printf("\n\n\nWhat is the height of Mount everest in feet?");
printf("\n\nA.8648\tB.6648\n\nC.8884\tD.8848\n\n");
countq++;
if (toupper(getch())=='D')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is D.8848");break;}
case 9:
printf("\n\n\nWhat is the capital of Denmark?");
printf("\n\nA.Copenhagen\tB.Helsinki\n\nC.Rome\t\tD.Madrid\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.Copenhagen");break;}
case 10:
printf("\n\n\nWhich syllabe is stressed in the word 'instanteneous'?");
printf("\n\nA.1st\tB.2nd\n\nC.3rd\tD.4th\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.3rd");break;}
case 11:
printf("\n\n\nWho was the only player to score 6 successive sixes in an over?");
printf("\n\nA.Adam Gilchrist\tB.M.S.Dhoni\n\nC.Herschel Gibbs\tD.Sanath Jayasurya\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.Herschel Gibbs");break;}
case 12:
printf("\n\n\nWho was the only player to take 4 successive wickets?");
printf("\n\nA.Malinga Bandara\tB.Lasith Malinga\n\nC.Bret Lee\tD.Murali Daran\n\n");
countq++;
if (toupper(getch())=='B')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is B.Lasith Malinga");break;}
case 13:
printf("\n\n\nWhich country is hosting the Fifa World Cup 2010?");
printf("\n\nA.South Africa\tB.Italy\n\nC.Argentina\tD.Spain\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.South Africa");break;}
case 14:
printf("\n\n\nWho is the author of 'Pulpasa Cafe'?");
printf("\n\nA.Narayan Wagle\tB.Lal Gopal Subedi\n\nC.B.P. Koirala\tD.Khagendra Sangraula\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.Narayan Wagle");break;}
case 15:
printf("\n\n\nWhich country is Maria Sarapova from?");
printf("\n\nA.Russia\tB.Switzerland\n\nC.Argentina\tD.Spain\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.Russia");break;}
case 16:
printf("\n\n\nWho was awarded the youngest player award in Fifa World Cup 2006?");
printf("\n\nA.Wayne Rooney\tB.Lucas Podolski\n\nC.Lionel Messi\tD.Christiano Ronaldo\n\n");
countq++;
if (toupper(getch())=='B')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is B.Lucas Podolski");break;}
case 17:
printf("\n\n\nWhat is the smallest district of Nepal?");
printf("\n\nA.Lalitpur\tB.Karnali\n\nC.Bhaktapur\tD.Gulmi\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.Bhaktapur");break;}
case 18:
printf("\n\n\nWhat is the headquarter of Western Development Region?");
printf("\n\nA.Dhankuta\tB.Kathmandu\n\nC.Dhangadhi\tD.Pokhara\n\n");
countq++;
if (toupper(getch())=='D')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is D.Pokhara");break;}
case 19:
printf("\n\n\nWhich place is called 'The Cherrapunji of Nepal'?");
printf("\n\nA.Dharan\tB.Kathmandu\n\nC.Pokhara\tD.Butwal\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.Pokhara");break;}
case 20:
printf("\n\n\nWhich city is known at 'The City of Seven Hills'?");
printf("\n\nA.Rome\tB.Vactican City\n\nC.Madrid\tD.Berlin\n\n");
countq++;
if (toupper(getch())=='A')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is A.Rome");break;}
case 21:
printf("\n\n\nWho was the F1 racing champion of 2006?");
printf("\n\nA.Louis Hamilton\tB.Felipe Massa\n\nC.Fernando Alonso\tD.Michael Schumaker\n\n");
countq++;
if (toupper(getch())=='C')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is C.Fernanda Alonso");break;}
case 22:
printf("\n\n\nWho won the Women Australian Open 2007?");
printf("\n\nA.Martina Hingis\tB.Maria Sarapova\n\nC.Kim Clijster\tD.Serena Williams\n\n");
countq++;
if (toupper(getch())=='D')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is D.Serena Williams");break;}
case 23:
printf("\n\n\nName the country where there no mosquito is found?");
printf("\n\nA.Germany\tB.Spain\n\nC.Japan\tD.France\n\n");
countq++;
if (toupper(getch())=='D')
{printf("\n\nCorrect!!!");countr++; break;}
else
{printf("\n\nWrong!!! The correct answer is D.France");break;}
}
i++;
if (i<=5) goto start;
finaltime=time(NULL);
score=(float)countr/countq*100-difftime(finaltime,initialtime)/3;
if (score<0) score=0;
printf("\n\n\nYour Score: %.2f",score);
if (score==100) printf("\n\nEXCELLENT!!! KEEP IT UP");
else if (score>=80 && score<100) printf("\n\nVERY GOOD!!");
else if (score>=60 &&score<80) printf("\n\nGOOD! BUT YOU NEED TO KNOW MORE.");
else if (score>=40 && score<60) printf("\n\nSATISFACTORY RESULT, BUT THIS MUCH IS MUCH SUFFICIENT.");
else printf("\n\nYOU ARE VERY POOR IN G.K.,WORK HARD");
puts("\n\nNEXT PLAY?(Y/N)");
if (toupper(getch())=='Y')
goto home;
else
{
writescore(score,playername);
goto mainhome;
}
}
else
{
printf("\n\n\t\t Enter the right key\n\n\t\t ");
Sleep(700);
goto mainhome;
}
return 0;
}
Graphics in C
#include <windows.h>
#include <stdio.h>
void SetColorAndBackground(int ForgC, int BackC);
void main()
{
int f,b;
system("cls");
printf("Enter fore and background color code : ");
scanf("%d",&f);
SetColorAndBackground(f,16); //color value range 0 up-to 256
printf("what is text background color");
SetColorAndBackground(30,256);
printf("\nCOLOR IN CODE BLOCKS...");
getch();
}
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
#include <stdio.h>
void SetColorAndBackground(int ForgC, int BackC);
void main()
{
int f,b;
system("cls");
printf("Enter fore and background color code : ");
scanf("%d",&f);
SetColorAndBackground(f,16); //color value range 0 up-to 256
printf("what is text background color");
SetColorAndBackground(30,256);
printf("\nCOLOR IN CODE BLOCKS...");
getch();
}
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
Check if a string is palindrome or not in C ?
#include<stdio.h>
#include<string.h>
void main()
{
char word[50];
int i,j,l,c=0;
printf("Enter a word : ");
gets(word);
l=strlen(word);
for(i=0,j=l-1;i<l/2;i++,j--)
{
if(word[i]-word[j]>0)
{
c=1;
break;
}
}
if(c==0)
{
printf("Palindrome");
}
else
{
printf("Not palindrome");
}
getch();
}
#include<string.h>
void main()
{
char word[50];
int i,j,l,c=0;
printf("Enter a word : ");
gets(word);
l=strlen(word);
for(i=0,j=l-1;i<l/2;i++,j--)
{
if(word[i]-word[j]>0)
{
c=1;
break;
}
}
if(c==0)
{
printf("Palindrome");
}
else
{
printf("Not palindrome");
}
getch();
}
Check if 3rd digit of a no. is prime or not in C ?
#include<stdio.h>
int digitcount(int x)
{
int r,c=0,i=0,y=x;
while(x!=0)
{
r=x%10;
x=x/10;
c++;
}
while(y!=0)
{
r=y%10;
y=y/10;
i++;
if(i==c-2)
{
return r;
}
}
}
void primecheck(int p)
{
int count=0,j;
for(j=1;j<=p;j++)
{
if(p%j==0)
{
count++;
}
}
if(count==2)
{
printf("3rd digit i.e. %d is prime.",p);
}
else
{
printf("3rd digit i.e. %d is not prime.",p);
}
}
void main()
{
int n,digino;
printf("Enter the number : ");
scanf("%d",&n);
digino=digitcount(n);
primecheck(digino);
getch();
}
int digitcount(int x)
{
int r,c=0,i=0,y=x;
while(x!=0)
{
r=x%10;
x=x/10;
c++;
}
while(y!=0)
{
r=y%10;
y=y/10;
i++;
if(i==c-2)
{
return r;
}
}
}
void primecheck(int p)
{
int count=0,j;
for(j=1;j<=p;j++)
{
if(p%j==0)
{
count++;
}
}
if(count==2)
{
printf("3rd digit i.e. %d is prime.",p);
}
else
{
printf("3rd digit i.e. %d is not prime.",p);
}
}
void main()
{
int n,digino;
printf("Enter the number : ");
scanf("%d",&n);
digino=digitcount(n);
primecheck(digino);
getch();
}
Application of struct in C
#include<stdio.h>
#include<string.h>
struct student
{
char name[20];
int roll;
};
void main()
{
struct student s[10],t;
int i,n,j;
printf("Enter no. of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %dth student's name and roll : ",i+1);
scanf("%s%d",s[i].name,&s[i].roll);
}
for(i=0;i<=n-2;i++)
{
for(j=i+1;j<=n-1;j++)
{
if(strcmp(s[i].name,s[j].name)>0)
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
printf("\nName sorted in alphabetical order is : ");
printf("\nName\tRoll No.");
for(i=0;i<n;i++)
{
printf("\n%s\t%d",s[i].name,s[i].roll);
}
getch();
}
#include<string.h>
struct student
{
char name[20];
int roll;
};
void main()
{
struct student s[10],t;
int i,n,j;
printf("Enter no. of students : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter %dth student's name and roll : ",i+1);
scanf("%s%d",s[i].name,&s[i].roll);
}
for(i=0;i<=n-2;i++)
{
for(j=i+1;j<=n-1;j++)
{
if(strcmp(s[i].name,s[j].name)>0)
{
t=s[i];
s[i]=s[j];
s[j]=t;
}
}
}
printf("\nName sorted in alphabetical order is : ");
printf("\nName\tRoll No.");
for(i=0;i<n;i++)
{
printf("\n%s\t%d",s[i].name,s[i].roll);
}
getch();
}
Check if a no. is prime or not in C ?
#include<stdio.h>
int testprime(int ,int);
void main()
{
int n,prime;
printf("Enter a positive number : ");
scanf("%d",&n);
prime=testprime(n,n/2);
if(prime==1)
printf("%d is prime number.",n);
else
printf("%d is not a prime number.",n);
getch();
}
int testprime(int num, int i)
{
if(i==1)
{
return 1;
}
else
{
if(num%i==0)
{
return 0;
}
else
{
testprime(num,i-1);
}
}
}
int testprime(int ,int);
void main()
{
int n,prime;
printf("Enter a positive number : ");
scanf("%d",&n);
prime=testprime(n,n/2);
if(prime==1)
printf("%d is prime number.",n);
else
printf("%d is not a prime number.",n);
getch();
}
int testprime(int num, int i)
{
if(i==1)
{
return 1;
}
else
{
if(num%i==0)
{
return 0;
}
else
{
testprime(num,i-1);
}
}
}
Find HCF in C
#include<stdio.h>
int hcf(int a,int b,int c)
{
if(a%c==0 && b%c==0)
return c;
hcf(a,b,c-1);
}
void main()
{
int h,x,y;
printf("Enter two numbers x and y : ");
scanf("%d%d",&x,&y);
if(x>y)
{
h=hcf(x,y,y);
}
else
{
h=hcf(y,x,x);
}
printf("The HCF of the entered number is : %d",h);
getch();
}
int hcf(int a,int b,int c)
{
if(a%c==0 && b%c==0)
return c;
hcf(a,b,c-1);
}
void main()
{
int h,x,y;
printf("Enter two numbers x and y : ");
scanf("%d%d",&x,&y);
if(x>y)
{
h=hcf(x,y,y);
}
else
{
h=hcf(y,x,x);
}
printf("The HCF of the entered number is : %d",h);
getch();
}
Find fibonacci series in C
#include<stdio.h>
void fibonacci(int);
void main()
{
int n,first=0,second=1;
printf("Enter the number of fibonacci series to be printed : ");
scanf("%d",&n);
printf("%d\t%d\t",first,second);
fibonacci(n);
getch();
}
void fibonacci(int n)
{
static int first=0,second=1,sum=0;
if(n-2>0)
{
sum=first+second;
first=second;
second=sum;
printf("%d\t",sum);
fibonacci(n-1);
}
}
void fibonacci(int);
void main()
{
int n,first=0,second=1;
printf("Enter the number of fibonacci series to be printed : ");
scanf("%d",&n);
printf("%d\t%d\t",first,second);
fibonacci(n);
getch();
}
void fibonacci(int n)
{
static int first=0,second=1,sum=0;
if(n-2>0)
{
sum=first+second;
first=second;
second=sum;
printf("%d\t",sum);
fibonacci(n-1);
}
}
Find no. of vowels in a string in C
#include<stdio.h>
void strcount(char []);
void main()
{
char str[50];
char c;
while(1)
{
printf("enter string\n");
//gets(str);
scanf("%s",str);
strcount(str);
printf("want to run again? press y/n \n");
scanf("%s",&c);
if(c == 'n' || c == 'N')
break;
}
}
void strcount(char str[50])
{
int i,j=0;
for (i=0;str[i]!=NULL;i++)
{
if( str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U')
j++;
}
printf("count of vowels in string is %d\n",j);
}
Find triangular no. in C
#include<stdio.h>
void main()
{
int n1,n2,i,t;
printf("Enter two numbers : ");
scanf("%d%d",&n1,&n2);
for(i=1;i<=n2;i++)
{
t = i*(i+1)/2;
if(t>=n1 && t<=n2)
{
printf("%d ",t);
}
}
getch();
}
void main()
{
int n1,n2,i,t;
printf("Enter two numbers : ");
scanf("%d%d",&n1,&n2);
for(i=1;i<=n2;i++)
{
t = i*(i+1)/2;
if(t>=n1 && t<=n2)
{
printf("%d ",t);
}
}
getch();
}
Find highest paid employee in C
#include<stdio.h>
struct emp
{
char name[50];
int salary;
};
void main()
{
int n,i,sum=0,avg;
struct emp e[20],ls,hs;
printf("Enter the number of employee : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name and salary of employees : ");
scanf("%s%d",e[i].name,&e[i].salary);
sum = sum + e[i].salary;
}
avg = sum/n;
ls.salary=e[0].salary;
for(i=1;i<n;i++)
{
if(ls.salary>e[i].salary)
{
ls=e[i];
}
}
hs.salary=e[0].salary;
for(i=1;i<n;i++)
{
if(hs.salary<e[i].salary)
{
hs=e[i];
}
}
printf("\nAVERAGE SALARY = %d",avg);
printf("\n\nThe highest salary of employee is\nNAME\t\tSALARY");
printf("\n%s\t\t%d",hs.name,hs.salary);
printf("\n\nThe lowest salary of employee is\nNAME\t\tSALARY");
printf("\n%s\t\t%d",ls.name,ls.salary);
printf("\n\nEMPLOYEE HAVING SALARY ABOVE 15000");
printf("\nNAME\t\tSALARY");
for(i=0;i<n;i++)
{
if(e[i].salary>15000)
{
printf("\n%s\t\t%d",e[i].name,e[i].salary);
}
}
getch();
}
struct emp
{
char name[50];
int salary;
};
void main()
{
int n,i,sum=0,avg;
struct emp e[20],ls,hs;
printf("Enter the number of employee : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name and salary of employees : ");
scanf("%s%d",e[i].name,&e[i].salary);
sum = sum + e[i].salary;
}
avg = sum/n;
ls.salary=e[0].salary;
for(i=1;i<n;i++)
{
if(ls.salary>e[i].salary)
{
ls=e[i];
}
}
hs.salary=e[0].salary;
for(i=1;i<n;i++)
{
if(hs.salary<e[i].salary)
{
hs=e[i];
}
}
printf("\nAVERAGE SALARY = %d",avg);
printf("\n\nThe highest salary of employee is\nNAME\t\tSALARY");
printf("\n%s\t\t%d",hs.name,hs.salary);
printf("\n\nThe lowest salary of employee is\nNAME\t\tSALARY");
printf("\n%s\t\t%d",ls.name,ls.salary);
printf("\n\nEMPLOYEE HAVING SALARY ABOVE 15000");
printf("\nNAME\t\tSALARY");
for(i=0;i<n;i++)
{
if(e[i].salary>15000)
{
printf("\n%s\t\t%d",e[i].name,e[i].salary);
}
}
getch();
}
Operations on complex numbers in C
#include<stdio.h>
struct cmplx
{
float real;
float img;
};
struct cmplx add(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = x.real + y.real;
z.img = x.img + y.img;
return(z);
}
struct cmplx sub(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = x.real - y.real;
z.img = x.img - y.img;
return(z);
}
struct cmplx mul(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = x.real * y.real- x.img * y.img;
z.img = x.real * y.img + x.img * y.real;
return(z);
}
struct cmplx div(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = (x.real*y.real+x.img*y.img)/(y.real*y.real+y.img*y.img);
z.img = (y.real*x.img-y.img*x.real)/(y.real*y.real+y.img*y.img);
return(z);
}
void main()
{
struct cmplx c1,c2,c3;
char ch;
printf("Enter the operator +,-,*,/ : ");
ch = getchar();
printf("\nEnter the real part of fist complex number : ");
scanf("%f",&c1.real);
printf("\nEnter the imaginary part of fist complex number : ");
scanf("%f",&c1.img);
printf("\nEnter the real part of 2nd complex number : ");
scanf("%f",&c2.real);
printf("\nEnter the imaginary part of 2nd complex number : ");
scanf("%f",&c2.img);
switch(ch)
{
case '+':
{
c3=add(c1,c2);
break;
}
case '-':
{
c3=sub(c1,c2);
break;
}
case '*':
{
c3=mul(c1,c2);
break;
}
case '/':
{
c3=div(c1,c2);
break;
}
default:
{
printf("Invalid operator....\a");
break;
}
}
printf("\n\nThe result is %f + %fi",c3.real,c3.img);
}
struct cmplx
{
float real;
float img;
};
struct cmplx add(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = x.real + y.real;
z.img = x.img + y.img;
return(z);
}
struct cmplx sub(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = x.real - y.real;
z.img = x.img - y.img;
return(z);
}
struct cmplx mul(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = x.real * y.real- x.img * y.img;
z.img = x.real * y.img + x.img * y.real;
return(z);
}
struct cmplx div(struct cmplx x, struct cmplx y)
{
struct cmplx z;
z.real = (x.real*y.real+x.img*y.img)/(y.real*y.real+y.img*y.img);
z.img = (y.real*x.img-y.img*x.real)/(y.real*y.real+y.img*y.img);
return(z);
}
void main()
{
struct cmplx c1,c2,c3;
char ch;
printf("Enter the operator +,-,*,/ : ");
ch = getchar();
printf("\nEnter the real part of fist complex number : ");
scanf("%f",&c1.real);
printf("\nEnter the imaginary part of fist complex number : ");
scanf("%f",&c1.img);
printf("\nEnter the real part of 2nd complex number : ");
scanf("%f",&c2.real);
printf("\nEnter the imaginary part of 2nd complex number : ");
scanf("%f",&c2.img);
switch(ch)
{
case '+':
{
c3=add(c1,c2);
break;
}
case '-':
{
c3=sub(c1,c2);
break;
}
case '*':
{
c3=mul(c1,c2);
break;
}
case '/':
{
c3=div(c1,c2);
break;
}
default:
{
printf("Invalid operator....\a");
break;
}
}
printf("\n\nThe result is %f + %fi",c3.real,c3.img);
}
Arranging numbers in ascending and descending order in C
#include<stdio.h>
void main()
{
int x[5],i,j,temp;
for(i=0;i<=4;i++)
{
printf("Enter x[%d]th number : ",i);
scanf("%d",&x[i]);
}
for(i=0;i<=5-2;i++)
{
for(j=i+1;j<=5-1;j++)
{
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
printf("\nIn Ascending Order : ");
for(i=0;i<=4;i++)
{
printf("%d ",x[i]);
}
printf("\nIn Descending Order : ");
for(i=4;i>=0;i--)
{
printf("%d ",x[i]);
}
getch();
}
void main()
{
int x[5],i,j,temp;
for(i=0;i<=4;i++)
{
printf("Enter x[%d]th number : ",i);
scanf("%d",&x[i]);
}
for(i=0;i<=5-2;i++)
{
for(j=i+1;j<=5-1;j++)
{
if(x[i]>x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
printf("\nIn Ascending Order : ");
for(i=0;i<=4;i++)
{
printf("%d ",x[i]);
}
printf("\nIn Descending Order : ");
for(i=4;i>=0;i--)
{
printf("%d ",x[i]);
}
getch();
}
Find largest and smallest no. in C
#include<stdio.h>
void main()
{
int x[10],i,j,temp;
for(i=0;i<=9;i++)
{
printf("Enter %dth number : ",i+1);
scanf("%d",&x[i]);
}
for(i=0;i<=8;i++)
{
for(j=i+1;j<=9;j++)
{
if(x[i]<x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
printf("\nThe largest of 10 entered numbers is %d",x[0]);
//printf("\nThe smallest of 10 entered numbers is %d",x[9]);
getch();
}
void main()
{
int x[10],i,j,temp;
for(i=0;i<=9;i++)
{
printf("Enter %dth number : ",i+1);
scanf("%d",&x[i]);
}
for(i=0;i<=8;i++)
{
for(j=i+1;j<=9;j++)
{
if(x[i]<x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
}
printf("\nThe largest of 10 entered numbers is %d",x[0]);
//printf("\nThe smallest of 10 entered numbers is %d",x[9]);
getch();
}
Display string after deleting vowels in it in C
#include<stdio.h>
void main()
{
char str[50],i;
printf("Enter a string : ");
gets(str);
printf("\nAfter deleting vowels :\t");
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U')
{
continue;
}
else
{
printf("%c",str[i]);
}
}
getch();
}
void main()
{
char str[50],i;
printf("Enter a string : ");
gets(str);
printf("\nAfter deleting vowels :\t");
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U')
{
continue;
}
else
{
printf("%c",str[i]);
}
}
getch();
}
Count no. of words in a sentence in C
#include<stdio.h>
void main()
{
char str[50],i,wordcount=0;
printf("Enter a line of text : ");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==32)
{
wordcount++;
}
if(str[i]==' ' && str[i-1]==' ')
{
wordcount--;
}
}
if(str[0]==' ' || str[0]=='\0')
{
wordcount=-1;
}
printf("\n\nThe number of words in the line of text entered is %d",wordcount+1);
getch();
}
void main()
{
char str[50],i,wordcount=0;
printf("Enter a line of text : ");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]==32)
{
wordcount++;
}
if(str[i]==' ' && str[i-1]==' ')
{
wordcount--;
}
}
if(str[0]==' ' || str[0]=='\0')
{
wordcount=-1;
}
printf("\n\nThe number of words in the line of text entered is %d",wordcount+1);
getch();
}
Sorting names in C
#include<stdio.h>
//#include<string.h>
void copystring(char txt1[], char txt2[])
{
int k;
for(k=0;txt2[k]!='\0';k++)
{
txt1[k]=txt2[k];
}
txt1[k]='\0';
}
int comparestring(char str1[], char str2[])
{
int p,difference;
for(p=0;str1[p]!='\0' || str2[p]!='\0';p++)
{
difference=str1[p]-str2[p];
if(difference!=0)
{
break;
}
}
return(difference);
}
void swapstring(char txt1[], char txt2[])
{
int i,temp[50];
for(i=0;txt2[i]!='\0';i++)
{
temp[i]=txt2[i];
}
temp[i]='\0';
for(i=0;txt1[i]!='\0';i++)
{
txt2[i]=txt1[i];
}
txt2[i]='\0';
for(i=0;temp[i]!='\0';i++)
{
txt1[i]=temp[i];
}
txt1[i]='\0';
}
void main()
{
char name[25][40],temp[40];
int n,i,j;
printf("Enter number of names to be sorted in ascending order : ");
scanf("%d",&n);
printf("\nEnter %d names to be sorted : \n",n);
for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}
for(i=0;i<=n-2;i++)
{
for(j=i+1;j<=n-1;j++)
{
if(comparestring(name[i],name[j])>0)
{
swapstring(name[i],name[j]);
//copystring(temp,name[i]);
//copystring(name[i],name[j]);
//copystring(name[j],temp);
}
}
}
printf("\nThe sorted name in ascending order is : \n");
for(i=0;i<n;i++)
{
printf("\n%s",name[i]);
}
getch();
}
//#include<string.h>
void copystring(char txt1[], char txt2[])
{
int k;
for(k=0;txt2[k]!='\0';k++)
{
txt1[k]=txt2[k];
}
txt1[k]='\0';
}
int comparestring(char str1[], char str2[])
{
int p,difference;
for(p=0;str1[p]!='\0' || str2[p]!='\0';p++)
{
difference=str1[p]-str2[p];
if(difference!=0)
{
break;
}
}
return(difference);
}
void swapstring(char txt1[], char txt2[])
{
int i,temp[50];
for(i=0;txt2[i]!='\0';i++)
{
temp[i]=txt2[i];
}
temp[i]='\0';
for(i=0;txt1[i]!='\0';i++)
{
txt2[i]=txt1[i];
}
txt2[i]='\0';
for(i=0;temp[i]!='\0';i++)
{
txt1[i]=temp[i];
}
txt1[i]='\0';
}
void main()
{
char name[25][40],temp[40];
int n,i,j;
printf("Enter number of names to be sorted in ascending order : ");
scanf("%d",&n);
printf("\nEnter %d names to be sorted : \n",n);
for(i=0;i<n;i++)
{
scanf("%s",name[i]);
}
for(i=0;i<=n-2;i++)
{
for(j=i+1;j<=n-1;j++)
{
if(comparestring(name[i],name[j])>0)
{
swapstring(name[i],name[j]);
//copystring(temp,name[i]);
//copystring(name[i],name[j]);
//copystring(name[j],temp);
}
}
}
printf("\nThe sorted name in ascending order is : \n");
for(i=0;i<n;i++)
{
printf("\n%s",name[i]);
}
getch();
}
Copying string 2 to string 1
#include<stdio.h>
void copystr(char [],char []);
void main()
{
char str1[50],str2[50];
printf("Enter 1st string,str1 : ");
gets(str1);
printf("\nEnter 2nd string,str2 : ");
gets(str2);
copystr(str1,str2);
printf("\n\nAfter copying str2 in str1\n\nstr1 is %s ",str1);
getch();
}
void copystr(char txt1[], char txt2[])
{
int i;
for(i=0;txt2[i]!='\0';i++)
{
txt1[i]=txt2[i];
}
txt1[i]='\0';
}
void copystr(char [],char []);
void main()
{
char str1[50],str2[50];
printf("Enter 1st string,str1 : ");
gets(str1);
printf("\nEnter 2nd string,str2 : ");
gets(str2);
copystr(str1,str2);
printf("\n\nAfter copying str2 in str1\n\nstr1 is %s ",str1);
getch();
}
void copystr(char txt1[], char txt2[])
{
int i;
for(i=0;txt2[i]!='\0';i++)
{
txt1[i]=txt2[i];
}
txt1[i]='\0';
}
Concatenation of two string in C
#include<stdio.h>
void catstr(char [],char []);
void main()
{
char str1[50],str2[50];
printf("Enter 1st string (str1) :");
gets(str1);
printf("\nEnter 2nd string (str2) :");
gets(str2);
catstr(str1,str2);
printf("\n\nAfter concatenating(joining) these strings\n\nFinal string is ");
puts(str1);
getch();
}
void catstr(char txt1[], char txt2[])
{
int i,j;
for(i=0;txt1[i]!='\0';i++);
for(j=0;txt2[j]!='\0';j++,i++)
{
txt1[i]=txt2[j];
}
txt1[i]='\0';
}
void catstr(char [],char []);
void main()
{
char str1[50],str2[50];
printf("Enter 1st string (str1) :");
gets(str1);
printf("\nEnter 2nd string (str2) :");
gets(str2);
catstr(str1,str2);
printf("\n\nAfter concatenating(joining) these strings\n\nFinal string is ");
puts(str1);
getch();
}
void catstr(char txt1[], char txt2[])
{
int i,j;
for(i=0;txt1[i]!='\0';i++);
for(j=0;txt2[j]!='\0';j++,i++)
{
txt1[i]=txt2[j];
}
txt1[i]='\0';
}
Code in C to multiply two matrix
#include<stdio.h>
void matmultiply(int m[3][3], int n[3][3]);
void read(int x[3][3]);
//void read2(int y[3][3]);
void main()
{
int a[3][3],b[3][3],l,w;
printf("\nFor matrix a :-\n");
read(a);
printf("\nFor matrix b :-\n");
read(b);
printf("\nMatrix a :\n");
for(l=0;l<3;l++)
{
for(w=0;w<3;w++)
{
printf("%d\t",a[l][w]);
}
printf("\n");
}
printf("\nMatrix b :\n");
for(l=0;l<3;l++)
{
for(w=0;w<3;w++)
{
printf("%d\t",b[l][w]);
}
printf("\n");
}
matmultiply(a,b);
getch();
}
void read(int x[3][3])
{
int r,s;
for(r=0;r<3;r++)
{
for(s=0;s<3;s++)
{
printf("Enter the [%d][%d]th element of matrix : ",r,s);
scanf("%d",&x[r][s]);
}
}
}
void matmultiply(int m[3][3], int n[3][3])
{
int t[3][3],i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
t[i][j]=0;
for(k=0;k<3;k++)
{
t[i][j]=t[i][j]+m[i][k]*n[k][j];
}
}
}
printf("\nThe resultant matrix after multiplying is :-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",t[i][j]);
}
printf("\n");
}
}
void matmultiply(int m[3][3], int n[3][3]);
void read(int x[3][3]);
//void read2(int y[3][3]);
void main()
{
int a[3][3],b[3][3],l,w;
printf("\nFor matrix a :-\n");
read(a);
printf("\nFor matrix b :-\n");
read(b);
printf("\nMatrix a :\n");
for(l=0;l<3;l++)
{
for(w=0;w<3;w++)
{
printf("%d\t",a[l][w]);
}
printf("\n");
}
printf("\nMatrix b :\n");
for(l=0;l<3;l++)
{
for(w=0;w<3;w++)
{
printf("%d\t",b[l][w]);
}
printf("\n");
}
matmultiply(a,b);
getch();
}
void read(int x[3][3])
{
int r,s;
for(r=0;r<3;r++)
{
for(s=0;s<3;s++)
{
printf("Enter the [%d][%d]th element of matrix : ",r,s);
scanf("%d",&x[r][s]);
}
}
}
void matmultiply(int m[3][3], int n[3][3])
{
int t[3][3],i,j,k;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
t[i][j]=0;
for(k=0;k<3;k++)
{
t[i][j]=t[i][j]+m[i][k]*n[k][j];
}
}
}
printf("\nThe resultant matrix after multiplying is :-\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",t[i][j]);
}
printf("\n");
}
}
Telephone directory
Code for telephone directory in c
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<time.h>
struct teledir
{
char firstname[50];
char lastname[50];
char address[50];
char landline[15];
char mobile[15];
}td;
struct signup
{
char firstname[20];
char lastname[20];
char email[50];
char password[20];
}s;
int f=0,ls=0;
char em[50];
void gotoxy(int x,int y)
{
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void SetColorAndBackground(int ForgC, int BackC)
{
WORD wColor = ((BackC & 0x0F) << 4) + (ForgC & 0x0F);;
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), wColor);
}
void rectangle(int x, int y, int l, int b)
{
int i,j;
gotoxy(x,y);
printf("%c",201);
for(i=x+1;i<l-1;i++)
{
printf("%c",205);
}
gotoxy(x,y+1);
for(i=y+1;i<b-1;i++)
{
gotoxy(x,i);
printf("%c",186);
}
gotoxy(x,b-1);
printf("%c",200);
gotoxy(x+1,b-1);
for(i=x+1;i<l-1;i++)
{
printf("%c",205);
}
printf("%c",188);
gotoxy(l-1,y);
printf("%c",187);
gotoxy(l-1,y+1);
for(i=y+1;i<b-1;i++)
{
gotoxy(l-1,i);
printf("%c",186);
}
}
void delay(unsigned int mseconds)
{
clock_t goal = mseconds + clock();
while (goal > clock());
}
void animation(char *anm,int a,int b)
{
int i;
for(i=0;i<strlen(anm);i++)
{
delay(150);
gotoxy(a+i,b);
printf("%c",*(anm+i));
}
}
void wait()
{
int i;
printf("PLEASE WAIT...");
for(i=0;i<10;i++)
{
delay(100);
printf(".");
}
}
void readnum(char *word)
{
int i=0;
word[i] = getch();
while(word[i]!='\r')
{
if(word[i]=='\b')
{
printf("\b \b");
i--;
}
else if((word[i]>=48&&word[i]<=57)||(word[i]=='+')||(word[i]=='-')||(word[i]=='*')||(word[i]=='#'))
{
printf("%c",word[i]);
i++;
}
word[i]=getch();
}
word[i]='\0';
}
void upsign()
{
FILE *fp,*tdr;
char first[50],last[50];
int c=0;
system("cls");
do
{
c=0;
fp=fopen("id.txt","ab+");
if(fp==NULL)
{
printf("Can't access the desired file.....");
}
SetColorAndBackground(14,16);
rectangle(10,10,60,20);
gotoxy(12,12);
printf("ENTER YOUR FIRST NAME : ");
gets(first);
fflush(stdin);
gotoxy(12,14);
printf("ENTER YOUR LAST NAME : ");
gets(last);
fflush(stdin);
gotoxy(12,16);
printf("ENTER UNIQUE USERNAME : ");
gets(em);
while(fread(&s,sizeof(s),1,fp)==1)
{
if(strcmp(s.email,em)==0)
{
c++;
}
}
if(c!=0)
{
gotoxy(12,31);
printf("ALREADY EXISTS......");
getch();
system("cls");
continue;
}
strcpy(s.email,em);
fflush(stdin);
gotoxy(12,18);
printf("ENTER PASSSWORD : ");
readpass(s.password);
strcpy(s.firstname,first);
strcpy(s.lastname,last);
fflush(stdin);
fwrite(&s,sizeof(s),1,fp);
tdr=fopen(em,"ab+");
}while(c!=0);
fclose(fp);
fclose(tdr);
}
void inlog()
{
FILE *fp,*tdr;
int flag=1;
char ch,pass[20],x;
system("cls");
fp=fopen("id.txt","ab+");
if(fp==NULL)
{
printf("Can't access the desired file.....");
}
SetColorAndBackground(9,16);
rectangle(7,7,60,20);
gotoxy(12,10);
printf("ENTER USERNAME : ");
scanf("%s",em);
fflush(stdin);
gotoxy(12,15);
printf("ENTER PASSWORD : ");
readpass(pass);
fflush(stdin);
while(fread(&s,sizeof(s),1,fp)==1)
{
if((strcmp(s.email,em)==0) && (strcmp(s.password,pass)==0))
{
gotoxy(12,21);
wait();
gotoxy(12,22);
printf("\aLOGIN SUCCESSFUL...PRESS ANY KEY......");
getch();
tdr=fopen(em,"ab+");
flag=0;
break;
}
}
if(flag==1)
{
gotoxy(12,22);
printf("\aUSERNAME OR PASSWORD ERROR.....!!!");
getch();
system("cls");
//inlog();
ls=0;
main();
}
fclose(fp);
fclose(tdr);
}
void readpass(char pass[])
{
int i=0;
pass[i]=getch();
while(pass[i]!='\r')
{
if(pass[i]=='\b')
{
printf("\b \b");
i--;
}
else if((pass[i]>='a'&&pass[i]<='z')||(pass[i]>='A'&&pass[i]<='Z')||(pass[i]>='0'&&pass[i]<='9'))
{
printf("%c",42);//42 is the ascii value of *
i++;
}
pass[i]=getch();
}
pass[i]='\0';
}
void frontscreen()
{
int i,d=200;
system("cls");
rectangle(0,0,80,24);
gotoxy(30,3); printf("T_");delay(d);
gotoxy(30,3); printf("TE_");delay(d);
gotoxy(30,3); printf("TEL_");delay(d);
gotoxy(30,3); printf("TELE_");delay(d);
gotoxy(30,3); printf("TELEP_");delay(d);
gotoxy(30,3); printf("TELEPH_");delay(d);
gotoxy(30,3); printf("TELEPHO_");delay(d);
gotoxy(30,3); printf("TELEPHON_");delay(d);
gotoxy(30,3); printf("TELEPHONE_");delay(d);
gotoxy(30,3); printf("TELEPHONE _");delay(d);
gotoxy(30,3); printf("TELEPHONE D_");delay(d);
gotoxy(30,3); printf("TELEPHONE DI_");delay(d);
gotoxy(30,3); printf("TELEPHONE DIR_");delay(d);
gotoxy(30,3); printf("TELEPHONE DIRE_");delay(d);
gotoxy(30,3); printf("TELEPHONE DIREC_");delay(d);
gotoxy(30,3); printf("TELEPHONE DIRECT_");delay(d);
gotoxy(30,3); printf("TELEPHONE DIRECTO_");delay(d);
gotoxy(30,3); printf("TELEPHONE DIRECTOR_");delay(d);
gotoxy(30,3); printf("TELEPHONE DIRECTORY");delay(d);
gotoxy(29,4);
for(i=0;i<21;i++)
{
printf("%c",22);
}
gotoxy(33,9);
printf("PROGRAMMED BY ");
gotoxy(31,10);
for(i=0;i<17;i++)
{
printf("%c",196);
}
gotoxy(35,12);
printf("AJAY YADAV");
gotoxy(35,14);
printf("BCE AB 010");
gotoxy(38,16); printf("2070");
gotoxy(28,20);
printf("PRESS ANY KEY TO CONTINUE...");
getch();
}
int menu()
{
int i,choice;
system("cls");
SetColorAndBackground(10,16);
rectangle(0,0,80,24);
animation("MAIN MENU",32,2);
gotoxy(57,1); printf("%c %s %s",16,s.firstname,s.lastname);
gotoxy(30,3); for(i=0;i<13;i++)
printf("%c",196);
gotoxy(20,5); SetColorAndBackground(9,16); printf("1 TO ADD RECORD IN DIRECTORY");
gotoxy(20,7); SetColorAndBackground(10,16); printf("2 TO LIST RECORD IN DIRECTORY");
gotoxy(20,9); SetColorAndBackground(11,16); printf("3 TO MODIFY RECORD IN DIRECTORY");
gotoxy(20,11); SetColorAndBackground(12,16); printf("4 TO DELETE RECORD IN DIRECTORY");
gotoxy(20,13); SetColorAndBackground(13,16); printf("5 TO SEARCH RECORD IN DIRECTORY");
gotoxy(20,15); SetColorAndBackground(14,16); printf("6 TO LOG OUT AND QUIT THE PROGRAMME");
gotoxy(17,16);
for(i=0;i<40;i++)
{
printf("%c",196);
}
gotoxy(17,17);
printf("ENTER YOUR CHOICE : ");
scanf("%d",&choice);
return(choice);
}
void add()
{
FILE *fpointer;char condition=1,color=9;
fpointer = fopen(em,"ab+");
if(fpointer==NULL)
{
printf("ERRROR IN OPENING FILE......\a\a");
}
while(1)
{
fflush(stdin);
system("cls");
SetColorAndBackground(color,16);
rectangle(0,0,80,3);
animation(">> ADD RECORD",23,1);
gotoxy(57,1); printf("%c %s %s",16,s.firstname,s.lastname);
rectangle(5,5,70,22);
gotoxy(9,8);
fflush(stdin);
printf("FIRSTNAME : ");
scanf("%s",td.firstname);
fflush(stdin);
gotoxy(9,10);
printf("LASTNAME : ");
scanf("%s",td.lastname);
fflush(stdin);
gotoxy(9,12);
printf("ADDRESS : ");
scanf("%s",td.address);
fflush(stdin);
gotoxy(9,14);
printf("LANDLINE : ");
readnum(td.landline);
fflush(stdin);
gotoxy(9,16);
printf("MOBILE : ");
readnum(td.mobile);
fflush(stdin);
fwrite(&td,sizeof(td),1,fpointer);
fflush(stdin);
gotoxy(9,18);
printf("ENTER ESC KEY TO EXIT AND ANY KEY TO INPUT MORE DATA : ");
condition=getche();
if(condition==27) //27 is ascii value of esc
{
gotoxy(9,24);
break;
}
color++;
if(color>=14)
{
color=9;
}
}
fclose(fpointer);
}
void list()
{
FILE *fpointer; int i=7,j,record=0,color=9;
system("cls");
SetColorAndBackground(11,16);
rectangle(0,0,80,3);
animation(">> LIST OF RECORD",2,1);
gotoxy(57,1); printf("%c %s %s",16,s.firstname,s.lastname);
fpointer=fopen(em,"rb");
if(fpointer==NULL)
{
printf("Can't access.....");
}
SetColorAndBackground(14,16);
gotoxy(2,5); printf("FIRST NAME");
gotoxy(17,5); printf("LAST NAME");
gotoxy(32,5); printf("ADDRESS");
gotoxy(47,5); printf("LANDLINE");
gotoxy(62,5); printf("MOBILE");
gotoxy(1,6);
for(j=0;j<78;j++)
{
printf("%c",205);
}
while(fread(&td,sizeof(td),1,fpointer)==1)
{
SetColorAndBackground(color,16);
fflush(stdin);
gotoxy(2,i);
printf("%s",td.firstname);
fflush(stdin);
gotoxy(17,i);
printf("%s",td.lastname);
fflush(stdin);
gotoxy(32,i);
printf("%s",td.address);
fflush(stdin);
gotoxy(47,i);
printf("%s",td.landline);
fflush(stdin);
gotoxy(62,i);
printf("%s",td.mobile);
fflush(stdin);
printf("\n ");
for(j=0;j<78;j++)
{
printf("%c",196);
}
i=i+2;
record++;
color++;
if(color>=14)
{
color=9;
}
}
gotoxy(24,1); printf("No. OF RECORDS IS %d",record);
gotoxy(30,record*2+9); printf("PRESS ANY KEY....");
fclose(fpointer);
getch();
}
void search()
{
system("cls");
int i,j,k=6,srch,fr=0,lr=0,ar=0,liner=0,mr=0;
FILE *fpointer; char frstname[50],lstname[50],adrs[50],line[15],mo[15];
system("cls");
fpointer=fopen(em,"rb");
if(fpointer==NULL)
{
printf("ACCESS DENIED.......\a");
}
rectangle(0,0,80,24);
SetColorAndBackground(10,16);
animation("SEARCH MENU",32,2);
gotoxy(57,1); printf("%c %s %s",16,s.firstname,s.lastname);
gotoxy(30,3); for(i=0;i<15;i++)
printf("%c",196);
gotoxy(20,5); printf("1 TO SEARCH THE RECORD BY FIRST NAME");
gotoxy(20,7); printf("2 TO SEARCH THE RECORD BY LAST NAME");
gotoxy(20,9); printf("3 TO SEARCH THE RECORD BY ADDRESS");
gotoxy(20,11); printf("4 TO SEARCH THE RECORD BY LANDLINE");
gotoxy(20,13); printf("5 TO SEARCH THE RECORD BY MOBILE NO.");
gotoxy(20,15); printf("%c OR PRESS OTHER NUM KEY FOLLOWED BY ENTER",5);
gotoxy(17,16);
for(i=0;i<50;i++)
{
printf("%c",196);
}
gotoxy(17,17);
printf("HOW DO YOU WANT TO SERACH ? : ");
scanf("%d",&srch);
if(srch==1)
{
system("cls"); SetColorAndBackground(11,16);
rectangle(0,0,80,3);
gotoxy(20,1); printf("==>> SEARCH BY FIRST NAME");
gotoxy(5,5); rectangle(5,5,60,10);
gotoxy(7,7); printf("ENTER FIRST NAME : ");
scanf("%s",frstname);
system("cls");
while(fread(&td,sizeof(td),1,fpointer)==1)
{
if(strcmp(td.firstname,frstname)==0)
{
gotoxy(2,3); printf("FIRST NAME");
gotoxy(17,3); printf("LAST NAME");
gotoxy(32,3); printf("ADDRESS");
gotoxy(47,3); printf("LANDLINE");
gotoxy(62,3); printf("MOBILE");
gotoxy(1,4);
for(j=0;j<78;j++)
{
printf("%c",205);
}
gotoxy(2,k);
printf("%s",td.firstname);
fflush(stdin);
gotoxy(17,k);
printf("%s",td.lastname);
fflush(stdin);
gotoxy(32,k);
printf("%s",td.address);
fflush(stdin);
gotoxy(47,k);
printf("%s",td.landline);
fflush(stdin);
gotoxy(62,k);
printf("%s",td.mobile);
fflush(stdin);
printf("\n ");
for(j=0;j<78;j++)
{
printf("%c",196);
}
k=k+2;
fr++;
}
}
if(fr==0)
{
rectangle(10,10,60,17);
gotoxy(20,13); printf("\aRECORD NOT FOUND....");
}
gotoxy(19,k+12); printf("TOTAL SEARCH RESULT BY FIRST NAME IS %d",fr);
gotoxy(24,k+14); printf("PRESS ANY KEY ....");getch();
}
if(srch==2)
{
system("cls"); SetColorAndBackground(12,16);
rectangle(0,0,80,3);
gotoxy(20,1); printf("==>> SEARCH BY LAST NAME");
gotoxy(5,5); rectangle(5,5,60,10);
gotoxy(7,7); printf("ENTER LAST NAME : ");
scanf("%s",lstname);
system("cls");
while(fread(&td,sizeof(td),1,fpointer)==1)
{
if(strcmp(td.lastname,lstname)==0)
{
gotoxy(2,3); printf("FIRST NAME");
gotoxy(17,3); printf("LAST NAME");
gotoxy(32,3); printf("ADDRESS");
gotoxy(47,3); printf("LANDLINE");
gotoxy(62,3); printf("MOBILE");
gotoxy(1,4);
for(j=0;j<78;j++)
{
printf("%c",205);
}
gotoxy(2,k);
printf("%s",td.firstname);
fflush(stdin);
gotoxy(17,k);
printf("%s",td.lastname);
fflush(stdin);
gotoxy(32,k);
printf("%s",td.address);
fflush(stdin);
gotoxy(47,k);
printf("%s",td.landline);
fflush(stdin);
gotoxy(62,k);
printf("%s",td.mobile);
fflush(stdin);
printf("\n ");
for(j=0;j<78;j++)
{
printf("%c",196);
}
k=k+2;
lr++;
}
}
if(lr==0)
{
rectangle(10,10,60,17);
gotoxy(20,13); printf("\aRECORD NOT FOUND....");
}
gotoxy(19,k+12); printf("TOTAL SEARCH RESULT BY LAST NAME IS %d",lr);
gotoxy(24,k+14); printf("PRESS ANY KEY ....");getch();
}
if(srch==3)
{
system("cls"); SetColorAndBackground(13,16);
rectangle(0,0,80,3);
gotoxy(20,1); printf("==>> SEARCH BY ADDRESS");
gotoxy(5,5); rectangle(5,5,60,10);
gotoxy(7,7); printf("ENTER ADDRESS : ");
scanf("%s",adrs);
system("cls");
while(fread(&td,sizeof(td),1,fpointer)==1)
{
if(strcmp(td.address,adrs)==0)
{
gotoxy(2,3); printf("FIRST NAME");
gotoxy(17,3); printf("LAST NAME");
gotoxy(32,3); printf("ADDRESS");
gotoxy(47,3); printf("LANDLINE");
gotoxy(62,3); printf("MOBILE");
gotoxy(1,4);
for(j=0;j<78;j++)
{
printf("%c",205);
}
gotoxy(2,k);
printf("%s",td.firstname);
fflush(stdin);
gotoxy(17,k);
printf("%s",td.lastname);
fflush(stdin);
gotoxy(32,k);
printf("%s",td.address);
fflush(stdin);
gotoxy(47,k);
printf("%s",td.landline);
fflush(stdin);
gotoxy(62,k);
printf("%s",td.mobile);
fflush(stdin);
printf("\n ");
for(j=0;j<78;j++)
{
printf("%c",196);
}
k=k+2;
ar++;
}
}
if(ar==0)
{
rectangle(10,10,60,17);
gotoxy(20,13); printf("\aRECORD NOT FOUND....");
}
gotoxy(19,k+12); printf("TOTAL SEARCH RESULT BY ADDRESS IS %d",ar);
gotoxy(24,k+14); printf("PRESS ANY KEY ....");getch();
}
if(srch==4)
{
system("cls"); SetColorAndBackground(14,16);
rectangle(0,0,80,3);
gotoxy(20,1); printf("==>> SEARCH BY LANDLINE");
gotoxy(5,5); rectangle(5,5,60,10);
gotoxy(7,7); printf("ENTER LANDLINE NUMBER : ");
scanf("%s",line);
system("cls");
while(fread(&td,sizeof(td),1,fpointer)==1)
{
if(strcmp(td.landline,line)==0)
{
gotoxy(2,3); printf("FIRST NAME");
gotoxy(17,3); printf("LAST NAME");
gotoxy(32,3); printf("ADDRESS");
gotoxy(47,3); printf("LANDLINE");
gotoxy(62,3); printf("MOBILE");
gotoxy(1,4);
for(j=0;j<78;j++)
{
printf("%c",205);
}
gotoxy(2,k);
printf("%s",td.firstname);
fflush(stdin);
gotoxy(17,k);
printf("%s",td.lastname);
fflush(stdin);
gotoxy(32,k);
printf("%s",td.address);
fflush(stdin);
gotoxy(47,k);
printf("%s",td.landline);
fflush(stdin);
gotoxy(62,k);
printf("%s",td.mobile);
fflush(stdin);
printf("\n ");
for(j=0;j<78;j++)
{
printf("%c",196);
}
k=k+2;
liner++;
}
}
if(liner==0)
{
rectangle(10,10,60,17);
gotoxy(20,13); printf("\aRECORD NOT FOUND....");
}
gotoxy(19,k+12); printf("TOTAL SEARCH RESULT BY LANDLINE IS %d",liner);
gotoxy(24,k+14); printf("PRESS ANY KEY ....");getch();
}
if(srch==5)
{
system("cls"); SetColorAndBackground(9,16);
rectangle(0,0,80,3);
gotoxy(20,1); printf("==>> SEARCH BY MOBILE NUMBER");
gotoxy(5,5); rectangle(5,5,60,10);
gotoxy(7,7); printf("ENTER MOBILE NUMBER : ");
scanf("%s",mo);
system("cls");
while(fread(&td,sizeof(td),1,fpointer)==1)
{
if(strcmp(td.mobile,mo)==0)
{
gotoxy(2,3); printf("FIRST NAME");
gotoxy(17,3); printf("LAST NAME");
gotoxy(32,3); printf("ADDRESS");
gotoxy(47,3); printf("LANDLINE");
gotoxy(62,3); printf("MOBILE");
gotoxy(1,4);
for(j=0;j<78;j++)
{
printf("%c",205);
}
gotoxy(2,k);
printf("%s",td.firstname);
fflush(stdin);
gotoxy(17,k);
printf("%s",td.lastname);
fflush(stdin);
gotoxy(32,k);
printf("%s",td.address);
fflush(stdin);
gotoxy(47,k);
printf("%s",td.landline);
fflush(stdin);
gotoxy(62,k);
printf("%s",td.mobile);
fflush(stdin);
printf("\n ");
for(j=0;j<78;j++)
{
printf("%c",196);
}
k=k+2;
mr++;
}
}
if(mr==0)
{
rectangle(10,10,60,17);
gotoxy(20,13); printf("\aRECORD NOT FOUND....");
}
gotoxy(19,k+12); printf("TOTAL SEARCH RESULT BY MOBILE NUMBER IS %d",mr);
gotoxy(24,k+14); printf("PRESS ANY KEY ...."); getch();
}
fclose(fpointer);
}
void modify()
{
FILE *fpointer,*temp; char mo[15],line[15]; int msrch,chk=0,i;
system("cls");
rectangle(0,0,80,24);
animation("MODIFICATION MENU",32,2);
gotoxy(57,1); printf("%c %s %s",16,s.firstname,s.lastname);
gotoxy(30,3); for(i=0;i<21;i++)
printf("%c",196);
gotoxy(20,5); printf("1 TO MODIFY RECORDS BY MOBILE NUMBER");
gotoxy(20,7); printf("2 TO MODIFY RECORDS BY LANDLINE NUMBER");
gotoxy(20,9); printf("%c OR PRESS OTHER NUM KEY FOLLOWED BY ENTER",16);
gotoxy(17,10);
for(i=0;i<50;i++)
{
printf("%c",196);
}
gotoxy(17,12);
printf("HOW DO YOU WANT TO PREFORM MODIFICATION ? : ");
scanf("%d",&msrch);
system("cls");
fpointer = fopen(em,"rb");
temp=fopen("temp","ab+");
if(fpointer==NULL)
{
printf("ERRROR IN OPENING FILE......\a\a");
}
if(temp==NULL)
{
printf("ERROR IN OPENING FILE.......\a");
}
switch(msrch)
{
case 1:
{
rectangle(0,0,80,3);
gotoxy(20,1); printf("==>> MODIFICATION BY MOBILE NUMBER");
gotoxy(5,5); rectangle(5,5,60,10);
gotoxy(7,7); printf("ENTER MOBILE NUMBER : ");
readnum(mo);//to accept (number,*,#,+,-) as input
system("cls");
while(fread(&td,sizeof(td),1,fpointer)==1)
{
if(strcmp(td.mobile,mo)==0)
{
chk=1; SetColorAndBackground(12,16);
system("cls");
rectangle(0,0,80,3);
gotoxy(20,1); printf("MODIFICATION OF MOBILE NUMBER %s",mo);
rectangle(5,5,70,21);
gotoxy(9,8); fflush(stdin);
printf("FIRSTNAME : ");
scanf("%s",&td.firstname);
fflush(stdin);
gotoxy(9,10);
printf("LASTNAME : ");
gets(td.lastname);
fflush(stdin);
gotoxy(9,12);
printf("ADDRESS : ");
gets(td.address);
fflush(stdin);
gotoxy(9,14);
printf("LANDLINE : ");
readnum(td.landline);
fflush(stdin);
gotoxy(9,16);
printf("MOBILE : ");
readnum(td.mobile);
fflush(stdin);
fwrite(&td,sizeof(td),1,temp);
}
else
{
fwrite(&td,sizeof(td),1,temp);
}
}
fclose(fpointer);
fclose(temp);
remove(em);
rename("temp",em);
if(chk!=0)
{
system("cls");
rectangle(10,10,60,20);
gotoxy(12,12); wait();
gotoxy(15,15); printf("\aRECORD SUCCESSFULLY MODIFIED.....");
gotoxy(17,17); printf("PRESS ANY KEY...");
}
if(chk==0)
{
system("cls");
rectangle(10,10,60,20);
gotoxy(12,12); wait();
gotoxy(15,15); printf("\aRECORD NOT FOUND....");
gotoxy(17,17); printf("PRESS ANY KEY...");
}
break;
}
case 2:
{
rectangle(0,0,80,3);
gotoxy(20,1); printf("==>> MODIFICATION BY LANDLINE NUMBER");
gotoxy(5,5); rectangle(5,5,60,10);
gotoxy(7,7); printf("ENTER LANDLINE NUMBER : ");
readnum(line);//scanf("%s",line);
system("cls");
while(fread(&td,sizeof(td),1,fpointer)==1)
{
if(strcmp(td.landline,line)==0)
{
chk=1; SetColorAndBackground(13,16);
system("cls");
rectangle(0,0,80,3);
gotoxy(20,1); printf("MODIFICATION OF LANDLINE NUMBER %s",line);
rectangle(5,5,70,21);
gotoxy(9,8);
printf("FIRSTNAME : ");
scanf("%s",&td.firstname);
fflush(stdin);
gotoxy(9,10);
printf("LASTNAME : ");
gets(td.lastname);
fflush(stdin);
gotoxy(9,12);
printf("ADDRESS : ");
gets(td.address);
fflush(stdin);
gotoxy(9,14);
printf("LANDLINE : ");
readnum(td.landline);
fflush(stdin);
gotoxy(9,16);
printf("MOBILE : ");
readnum(td.mobile);
fflush(stdin);
fwrite(&td,sizeof(td),1,temp);
}
else
{
fwrite(&td,sizeof(td),1,temp);
}
}
fclose(fpointer);
fclose(temp);
remove(em);
rename("temp",em);
if(chk!=0)
{
system("cls");
rectangle(10,10,60,20);
gotoxy(12,12); wait();
gotoxy(15,15); printf("\aRECORD SUCCESSFULLY MODIFIED.....");
gotoxy(17,17); printf("PRESS ANY KEY...");
}
if(chk==0)
{
system("cls");
rectangle(10,10,60,20);
gotoxy(12,12); wait();
gotoxy(15,15); printf("\aRECORD NOT FOUND....");
gotoxy(17,17); printf("PRESS ANY KEY...");
}
break;
}
default:
{
system("cls");
main();
break;
}
}
getch();
}
void del()
{
FILE *fpointer,*temp; char fname[50],mo[15],ch;int dsrch,i,check=0;
system("cls"); SetColorAndBackground(9,16);
rectangle(0,0,80,24);
animation("DELETE MENU",32,2);
gotoxy(57,1); printf("%c %s %s",16,s.firstname,s.lastname);
gotoxy(30,3); for(i=0;i<15;i++)
printf("%c",196);
gotoxy(20,5); printf("1 TO DELETE SELECTIVE RECORDS");
gotoxy(20,7); printf("2 TO DELETE ALL RECORDS");
gotoxy(20,9); printf("%c OR PRESS OTHER NUM KEY FOLLOWED BY ENTER",4);
gotoxy(17,10);
for(i=0;i<50;i++)
{
printf("%c",196);
}
gotoxy(17,12);
printf("HOW DO YOU WANT TO PREFORM DELETION ? : ");
scanf("%d",&dsrch);
system("cls");
if(dsrch==1)
{
fpointer = fopen(em,"rb");
temp=fopen("temp","ab+");
if(fpointer==NULL)
{
printf("ERRROR IN OPENING FILE......\a\a");
}
if(temp==NULL)
{
printf("ERROR IN OPENING FILE.......\a");
}
SetColorAndBackground(11,16);
rectangle(0,0,80,3);
gotoxy(20,1); printf("==>> SELECTIVE DELETION");
gotoxy(5,5); rectangle(5,5,60,15);
gotoxy(7,7); printf("ENTER FIRST NAME : ");
scanf("%s",fname);
gotoxy(7,10); printf("ENTER MOBILE NUMBER : ");
readnum(mo);
system("cls");
while(fread(&td,sizeof(td),1,fpointer)==1)
{
if((strcmp(td.firstname,fname)==0) && (strcmp(td.mobile,mo)==0))
{
check++;
continue;
}
else
{
fwrite(&td,sizeof(td),1,temp);
}
}
fclose(fpointer);
fclose(temp);
remove(em);
rename("temp",em);
if(check!=0)
{
rectangle(10,10,60,20);
gotoxy(12,12); wait();
gotoxy(15,15); printf("\aRECORD SUCCESSFULLY DELETED.....");
gotoxy(17,17); printf("PRESS ANY KEY...");
getch();
}
if(check==0)
{
rectangle(10,10,60,20);
gotoxy(12,12); wait();
gotoxy(15,15); printf("\aRECORD NOT FOUND....");
gotoxy(17,17); printf("PRESS ANY KEY...");
getch();
}
}
if(dsrch==2)
{
system("cls"); SetColorAndBackground(12,16);
rectangle(10,10,65,15);
gotoxy(12,12);
printf("ARE YOU SURE YOU WANT TO CLEAR ALL RECORDS ? ");
if(getche()=='y' || getche()=='Y')
{
getch();
system("cls");
fpointer = fopen(em,"rb");
temp=fopen(em,"ab+");
if(fpointer==NULL)
{
printf("ERRROR IN OPENING FILE......\a\a");
}
if(temp==NULL)
{
printf("ERROR IN OPENING FILE.......\a");
}
fclose(fpointer);
fclose(temp);
remove(em);
rename("temp",em);
rectangle(10,10,60,20);
gotoxy(12,12); wait();
gotoxy(15,15); printf("\aRECORD SUCCESSFULLY DELETED.....");
gotoxy(17,17); printf("PRESS ANY KEY...");
getch();
}
}
else
{
main();
}
}
void end()
{
system("cls");
SetColorAndBackground(12,16);
rectangle(12,7,60,15);
gotoxy(14,9); printf("PRESS SPACE BAR TO RETURN TO PROGRAM");
gotoxy(15,12); printf("OR ANY OTHER KEY TO END.....");
gotoxy(17,17);
if(getch()==32)
main();
else
exit(0);
}
void main()
{
int mchoice;char x;
if(f==0)
{
frontscreen();
f++;
system("cls");
}
if(ls==0)
{
SetColorAndBackground(13,16);
rectangle(25,7,60,15);
gotoxy(28,9); printf("1 FOR LOG IN");
gotoxy(28,12); printf("2 FOR SIGN UP");
gotoxy(25,20);
x=getche();
switch(x)
{
case '1':
{
inlog();
break;
}
case '2':
{
upsign();
break;
}
}
ls++;
}
mchoice=menu();
switch(mchoice)
{
case 1:
{
add();
main();
break;
}
case 2:
{
list();
main();
break;
}
case 3:
{
modify();
main();
break;
}
case 4:
{
del();
main();
break;
}
case 5:
{
search();
main();
break;
}
case 6:
{
end();
break;
}
default:
{
main();
break;
}
}
getch();
}