Quote:By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?
Try to solve it by yourself and then look at the solution.
Solution:
Spoiler (Click to View)
Quote:By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?
#include <iostream>
#include <cmath>
bool IsPrime(int num)
{
if(num % 2 == 0 && num != 2)
return false;
for(int i = 3; i <= sqrt((long double)num); i += 2)
if(num % i == 0)
return false;
return true;
}
int main()
{
unsigned long i = 2, n;
std::cout << "Find the nth prime number: ";
std::cin >> n;
for(int j = 0; j < n; i++)
{
if(IsPrime(i))
j++;
}
std::cout << i - 1;
std::cin.ignore();
std::cin.get();
return 0;
}
.data
Hello:
.asciz "1. Add\n2. Subtract\n3. Divide\n4. Multiplication\nOption:"
first:
.asciz "First Number:"
second:
.asciz "Second Number:"
answer:
.asciz "The answer is: %d\n"
d:
.ascii "%d\0"
int1:
.int 0
int2:
.int 0
option:
.int 1
.text
.globl _main
_main:
pushl $first
call _printf
addl $4, %esp
pushl $int1
pushl $d
call _scanf
addl $8, %esp
pushl $second
call _printf
addl $4, %esp
pushl $int2
pushl $d
call _scanf
addl $8, %esp
pushl $Hello
call _printf
addl $4, %esp
pushl $option
pushl $d
call _scanf
addl $8, %esp
cmpl $1, option
jz toadd
cmpl $2, option
jz subtract
cmpl $3, option
jz divide
cmpl $4, option
jz multiplication
toadd:
movl int1, %eax
addl int2, %eax
pushl %eax
pushl $answer
call _printf
addl $8, %esp
jmp end
subtract:
movl int1, %eax
subl int2, %eax
pushl %eax
pushl $answer
call _printf
addl $8, %esp
jmp end
divide:
movl int1, %eax
movl int2, %ebx
xorl %edx, %edx
idivl %ebx
pushl %eax
pushl $answer
call _printf
addl $8, %esp
jmp end
multiplication:
movl int1, %eax
movl int2, %ebx
imull %ebx
pushl %eax
pushl $answer
call _printf
addl $8, %esp
end:
call _getchar
call _getchar
retas -o main.o main.sld main.o -o main.exe -e_main -L"C:\Dev-cpp\lib" -lmsvcrtas -o main.o main.s
ld main.o -o main.exe -e_main -L"C:\Dev-cpp\lib" -lmsvcrt/**
Program: Simple chat program
Author: Sri Krishna
**/
#include <iostream>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
SOCKET sock;
void recvdata()
{
while(1)
{
char msg[256];
memset(msg, 0, 256);
int r = recv(sock, msg, 256, 0);
if(r == SOCKET_ERROR)
{
std::cout << "\n\nServer is disconnected\n\n";
std::cin.ignore();
std::cin.get();
closesocket(sock);
WSACleanup();
exit(1);
}
else
std::cout << "Friend: " << msg << std::endl;
}
}
void senddata()
{
while(1)
{
char msg[256];
memset(msg, 0, 256);
std::cin.get(msg, 256);
int ret = send(sock, msg, strlen(msg), 0);
if(ret == SOCKET_ERROR)
{
std::cout << "\n\nServer is disconnected\n\n";
std::cin.ignore();
std::cin.get();
closesocket(sock);
WSACleanup();
exit(1);
}
std::cin.ignore();
}
}
int main()
{
WSADATA wsData;
if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
{
std::cout << "WSAStartup!\n";
}
else
{
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET)
{
std::cout << "socket()\n";
WSACleanup();
}
else
{
sockaddr_in sin;
sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
sin.sin_port = htons(626);
sin.sin_family = AF_INET;
while(connect(sock, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
connect(sock, (sockaddr*)&sin, sizeof(sin));
std::cout << "Connected to your friend\n\n";
HANDLE hThread[2];
DWORD thread1, thread2;
hThread[0] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)recvdata, NULL, NULL, &thread1);
hThread[1] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)senddata, NULL, NULL, &thread2);
WaitForMultipleObjects(2, hThread, true, INFINITE);
}
}
std::cin.ignore();
std::cin.get();
return 0;
}/**
Program: Simple chat program
Author: Sri Krishna
**/
#include <iostream>
#include <windows.h>
#pragma comment(lib, "ws2_32.lib")
SOCKET sock, acceptsock;
void recvdata()
{
while(1)
{
char msg[256];
memset(msg, 0, 256);
int r = recv(sock, msg, 256, 0);
if(r == SOCKET_ERROR)
{
std::cout << "\n\nServer is disconnected\n\n";
std::cin.ignore();
std::cin.get();
closesocket(sock);
WSACleanup();
exit(1);
}
else
std::cout << "Friend: " << msg << std::endl;
}
}
void senddata()
{
while(1)
{
char msg[256];
memset(msg, 0, 256);
std::cin.get(msg, 256);
int ret = send(sock, msg, strlen(msg), 0);
if(ret == SOCKET_ERROR)
{
std::cout << "\n\nServer is disconnected\n\n";
std::cin.ignore();
std::cin.get();
closesocket(sock);
WSACleanup();
exit(1);
}
std::cin.ignore();
}
}
int main()
{
WSADATA wsData;
if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0)
{
std::cout << "WSAStartup!\n";
}
else
{
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock == INVALID_SOCKET)
{
std::cout << "socket()\n";
WSACleanup();
}
else
{
sockaddr_in sin;
sin.sin_addr.S_un.S_addr = INADDR_ANY;
sin.sin_port = htons(626);
sin.sin_family = AF_INET;
while(bind(sock, (sockaddr*)&sin, sizeof(sin)) == SOCKET_ERROR)
bind(sock, (sockaddr*)&sin, sizeof(sin));
while(listen(sock, 1) == SOCKET_ERROR)
{
listen(sock, 1);
}
while(1)
{
acceptsock = SOCKET_ERROR;
while(acceptsock == SOCKET_ERROR)
{
acceptsock = accept(sock, NULL, NULL);
}
std::cout << "Connected to your friend!\n\n";
sock = acceptsock;
break;
}
}
}
HANDLE hThread[2];
DWORD thread1, thread2;
hThread[0] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)recvdata, NULL, NULL, &thread1);
hThread[1] = CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)senddata, NULL, NULL, &thread2);
WaitForMultipleObjects(2, hThread, true, INFINITE);
std::cin.ignore();
std::cin.get();
return 0;
}Make a program like this:
1. Add to database
2. Display databse
Option:
You should be able to add as many databases as you want. It should ask you for name, age, gender, and after it is registered, put the computer time in a string(which should be a string or whatever). All this information should be taken into a structure
When displaying the database, it should display like this:
--
<The registered time>
Name: Name
Age: 15
Gender: M
The 'Display database' option should print all the databases added.#include <iostream>
#include <string>
#include <ctime>
int counter = 0, option = 0;
struct data_base
{
std::string name;
int age;
char gender;
std::string curtime;
};
int main()
{
data_base **array = new data_base*;
data_base *data = new data_base;
while(1)
{
system("CLS"); //don't care
std::cout << "1. Append to Database\n2. Display Database\nChoose(1/2):";
std::cin >> option;
if(option == 1)
{
data = new data_base;
system("CLS"); //don't care
std::cout << "Enter the name: ";
std::cin.ignore();
std::getline(std::cin, data->name);
std::cout << "Enter the Age: ";
std::cin >> data->age;
std::cout << "Enter the gender: ";
std::cin >> data->gender;
while(data->gender != 'm' && data->gender != 'M' && data->gender != 'f' && data->gender != 'F')
{
std::cout << "\nEnter the correct gender type\n";
std::cout << "Enter the gender: ";
std::cin >> data->gender;
}
time_t raw_time;
tm *time_info;
time(&raw_time);
time_info = localtime(&raw_time);
data->curtime = asctime(time_info);
array[counter] = data;
counter++;
}
else if(option == 2)
{
system("CLS"); //don't care
int c = 0;
while(c < counter) //problem is heere
{
std::cout << "\n--\n";
std::cout << "Time appended: " << array[c]->curtime << std::endl;
std::cout << "Name: " << array[c]->name << std::endl;
std::cout << "Age: " << array[c]->age << std::endl;
std::cout << "Gender: " << array[c]->gender << std::endl;
c++;
}
std::cin.ignore();
std::cin.get();
}
else
{
std::cout << "Can you read?";
}
}
std::cin.ignore();
std::cin.get();
return 0;
}01002FF5 - FF 05 9C570001 - inc [0100579C](*timeAddress)++;01002FE0 /$ 833D 64510001 >CMP DWORD PTR DS:[1005164],001001D6C |. E8 6F120000 CALL winmine.01002FE0 ; Case 113 (WM_TIMER) of switch 01001D5BVirtualProtect(WhichAddress, Size, WhatTypeOfProtection, ActualProtection); DWORD dwOld;
VirtualProtect((LPVOID)WM_TIMER_ADDRESS, 5, PAGE_EXECUTE_READWRITE, &dwOld);BYTE nops[] = {0x90, 0x90, 0x90, 0x90, 0x90};memcpy((void *)WM_TIMER_ADDRESS, nops, 5); VirtualProtect((LPVOID)WM_TIMER_ADDRESS, 5, dwOld, &dwOld);#include <windows.h>
#define WM_TIMER_ADDRESS 0x01001D6C
BYTE nop[5] = {0x90, 0x90, 0x90, 0x90, 0x90};
void patch()
{
DWORD dwOld;
VirtualProtect((LPVOID)WM_TIMER_ADDRESS, 5, PAGE_EXECUTE_READWRITE, &dwOld);
memcpy((void *)WM_TIMER_ADDRESS, nop, 5);
VirtualProtect((LPVOID)WM_TIMER_ADDRESS, 5, dwOld, &dwOld);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved)
{
if(dwReason == DLL_PROCESS_ATTACH)
{
MessageBox(NULL, "Injected to Process!", "Injected!", MB_OK);
patch();
}
return true;
}#include <windows.h>
void timezero()
{
while(1)
{
__asm
{
mov eax, 0x0100579c
mov [eax], 0
}
}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved)
{
if(dwReason == DLL_PROCESS_ATTACH)
{
MessageBox(NULL, "Injected to Process!", "Injected!", MB_OK);
timezero();
}
return true;
}#include#include #pragma comment(lib, "ws2_32.lib"); int main () { int option; int numOfPorts; int *portsArr; sockaddr_in sin; std::cout << "1. List(ex: x - y)\n"; std::cout << "2. Specific\n"; std::cout << "Option: "; std::cin >> option; if(option == 1) { WSADATA wsData; if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0) { std::cout << "Error: Cannot startup\n"; std::cin.get(); std::cin.get(); return 0; } int firstPort, secondPort; std::cout << "First port: "; std::cin >> firstPort; std::cout << "Second port: "; std::cin >> secondPort; for(int i = firstPort; i <= secondPort; i++) { SOCKET pSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(pSocket == INVALID_SOCKET) { std::cout << "Error: Invalid Socket\n"; std::cin.get(); std::cin.get(); return 0; } ZeroMemory(&sin, sizeof(sin)); sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); sin.sin_family = AF_INET; sin.sin_port = htons(i); if(connect(pSocket, (sockaddr*)&sin, sizeof(sin)) != SOCKET_ERROR) { std::cout << "Port: " << i << " is open!\n"; } else { std::cout << "Port: " << i << " is closed!\n"; } closesocket(pSocket); } } else if(option == 2) { WSADATA wsData; if(WSAStartup(MAKEWORD(2, 2), &wsData) != 0) { std::cout << "Error: Cannot startup\n"; std::cin.get(); std::cin.get(); return 0; } std::cout << "How many ports would you like to scan?:"; std::cin >> numOfPorts; if(numOfPorts <= 0) { std::cout << "Ports must be greater than 0"; } else { portsArr = new int[numOfPorts]; for(int i = 0; i < numOfPorts; i++) { std::cout << "Port[" << i << "]: "; std::cin >> portsArr[i]; } for(int i = 0; i < numOfPorts; i++) { SOCKET pSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if(pSocket == INVALID_SOCKET) { std::cout << "Error: Invalid Socket\n"; std::cin.get(); std::cin.get(); return 0; } ZeroMemory(&sin, sizeof(sin)); sin.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); sin.sin_family = AF_INET; sin.sin_port = htons(portsArr[i]); if(connect(pSocket, (sockaddr*)&sin, sizeof(sin)) != SOCKET_ERROR) { std::cout << "Port: " << portsArr[i] << " is open!\n"; } else { std::cout << "Port: " << portsArr[i] << " is closed!\n"; } closesocket(pSocket); } } delete [] portsArr; } else { std::cout << "Enter a valid option!\n"; std::cin.get(); std::cin.get(); return 0; } std::cin.get(); std::cin.get(); return 0; }
#include#include #define b_col 3 #define b_row 3 void clear_screen() { HANDLE output_handle = GetStdHandle(STD_OUTPUT_HANDLE); DWORD bytes_write, size; COORD coord = {0, 0}; CONSOLE_SCREEN_BUFFER_INFO csbi; GetConsoleScreenBufferInfo(output_handle, &csbi); size = csbi.dwSize.X * csbi.dwSize.Y; FillConsoleOutputCharacter(output_handle, ' ', size, coord, &bytes_write); SetConsoleCursorPosition(output_handle, coord); } void clear_board(char game_board[][b_col]) { for(int i = 0; i < b_row; i++) for(int j = 0; j < b_col; j++) game_board[i][j] = '-'; } void print_board(char game_board[][b_col]) { for(int i = 0; i < b_row; i++) { for(int j = 0; j < b_col; j++) printf("%c ", game_board[i][j]); printf("\n\n\n"); } } bool place_xo(char game_board[][b_col], int row, int col, char x_o) { if(game_board[row - 1][col - 1] != '-') //if coordinates given is not empty return false; else { game_board[row - 1][col - 1] = x_o; clear_screen(); //clear screen. don't use system("CLS"); print_board(game_board); //print board. return true; } } int check_row_col(char game_board[][b_col], char x_o, unsigned int f_row = 0, unsigned int f_col = 0) { int counter = 0; if(f_row != 0) //checks rows { for(int i = 0; i < b_col; i++) if(game_board[f_row - 1][i] == x_o) counter++; } else if(f_col != 0) //checks columns { for(int i = 0; i < b_row; i++) if(game_board[i][f_col - 1] == x_o) counter++; } else if(f_col == 0 && f_col == 0) //diagonal { int d_col = 0; for(int d_row = 0; d_row < b_row; d_row++, d_col++) if(game_board[d_row][d_col] == x_o) counter++; if(counter == b_row) return counter; else { d_col = b_col; counter = 0; for(int d_row = 0; d_row < b_row; d_row++, d_col--) if(game_board[d_row][d_col - 1] == x_o) counter++; } } return counter; } bool win_or_not(char game_board[][b_col], char x_o) { for(int i = 1; i < b_col; i++) //checks all columns { if(check_row_col(game_board, x_o, 0, i) == b_row) { printf("\n\n\n%c won the game", x_o); return true; } } for(int i = 1; i < b_row; i++) //checks all rows { if(check_row_col(game_board, x_o, i, 0) == b_row) { printf("\n\n\n%c won the game", x_o); return true; } } if(check_row_col(game_board, x_o, 0, 0) == b_row) //diagonal checks { printf("\n\n\n%c won the game", x_o); return true; } return false; } int main() { if(b_row != b_col) return -1; char game_board[b_row][b_col]; bool win_not = false; int row, col; clear_board(game_board); //clear board print_board(game_board); //print board bool x_turn = true; do { printf("--It's %c's turn--\n\n", x_turn ? 'X' : 'O'); printf("Row: "); scanf("%d", &row); printf("Column: "); scanf("%d", &col); if(row > b_row || col > b_col) { printf("\nOut of bounds.\n\n"); continue; } if(!place_xo(game_board, row, col, x_turn ? 'X' : 'O')) //place the 'x' and 'o'. x_turn ? 'X' : 'O' returns based on the bool value of x_turn { printf("\nCannot put %c into row %d, col %d.\n\n", x_turn ? 'X' : 'O', row, col); continue; } win_not = win_or_not(game_board, x_turn ? 'X' : 'O'); x_turn = !x_turn; }while(win_not == false); getchar(); getchar(); return 0; }
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. Try it yourself before you look at the solution:
#includeSolution source: ProjectEuler #1 Output:int main() { unsigned int sum = 0; for(int i = 0; i < 1000; i++) //go through until 1000 if(i % 3 == 0 || i % 5 == 0) //if it is divisble by 3 or 5 sum += i; //add the number to sum. printf("Sum of natural numbers below 1000, that are divisible by 3 or 5 is %d", sum); //print the sum getchar(); return 0; }
Sum of natural numbers below 1000, that are divisible by 3 or 5 is 233168I'll start uploading more ProjectEulers; if you have any suggestions, comments, or questions, post 'em. ...Sri Krishna.
OpenProcess(HowToOpen, InheritEverything(BOOL), Process ID);
int ProcessID; std::cin >> ProcessID; HANDLE hProcess; //This will contain the handle to the process. hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, ProcessID);
if(hProcess == NULL) std::cout << "OpenProcess: " << GetLastError(); else //continue
VirtualAllocEx(HandleToTheProcess, WhereDoYouWantToAllocate, HowMuchDoYouWantToAllocate, AllocationType, ProtectionType);
LPVOID vAlloc; //Stores our base address where the allocation occured. vAlloc = VirtualAllocEx(hProcess, NULL, strlen(DLL_PATH) + 1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); /*NULL in the second parameter lets the function choose where to allocate memory. We want to allocate the string length of DLL path, so we can write to it. It's strlen(DLL_PATH) +1 because of the null-terminator. Thanks to PoZHx for reminding me! :)*/
WriteProcessMemory(WhichProcess, WhereInMemory, WhatToWrite, HowLongIsTheData, HowMuchDidIWrite); //the last parameter is not really needed
WriteProcessMemory(hProcess, vAlloc, DLL_PATH, strlen(DLL_PATH), NULL); //this will pretty much write the DLL path to the process.
CreateRemoteThread(HandleToProcess, ThreadAttributes, StackSize, FunctionToExecute, ParametersToFunction, Flags, ThreadID); /*For this task, we'll only worry about HandleToProcess, FunctionToExecute, and ParametersToFunction because that's all we need to execute our thread in the targeted process*/
CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)LoadLibraryA, vAlloc, NULL, NULL);
GetProcAddress(HandleToTheModuleWhereTheFunctionIsLocated, FunctionName);
LPVOID pAddr; //This will contain the address of LoadLibrary, if suceeded.
pAddr = GetProcAddress(GetModuleHandle("kernel32.dll"), "LoadLibraryA");
CreateRemoteThread(hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)pAddr, vAlloc, NULL, NULL);
VirtualFreeEx(hProcess, vAlloc, NULL, MEM_RELEASE); //Must call this before closing handle. CloseHandle(hProcess);
#include <windows.h>
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved)
{
if(dwReason == DLL_PROCESS_ATTACH)
MessageBox(NULL, "Injected to process!", "Injected!", MB_OK);
return true;
}