Posts

Blackjack Console game program in Python

Image
Console Games Games that can be played using the console. Here is the program for the Blackjack Game of the cards. BlackJack Game Blackjack  is the American variant of a globally popular  banking game  known as  Twenty-One. in this game, one player has to make the total sum of their hands to lower than 21 to win while another player has the total sum is 21 or more, All the cards greater than 10 are considered as the 10 and Ace is considered as the 11 or 1 depending on conditions. The person who has less total of the cards is the winner (one player should have scored more than 21). import random class blackJack: player1Hand = 0 player2Hand = 0 deck ={'Ace of Spades':1, '2 of Spades':2, '3 of Spades':3, '4 of Spades':4, '5 of Spades':5, '6 of Spades':6, '7 of Spades':7, '8 of Spades':8, '9 of Spades':9, '10 of Spades':10, 'Jack of Spades':10, 'Queen of Spades':10, 'King of

File Handling in C (Reading a file line by line)

Image
In this tutorial, we are working with the files in the C. File handling in any language requires Three main steps to follow Open the file in Read/Write mode. Do various operation that you want to do. Close the file. We are using fopen(file stream). there is some modes to open a file. Read mode  opens file into the read mode if file not found returns -1. Write mode opens file into the write mode if the file does not present creates a new file if there is previous data it will be overwritten. Append mode opens the file in the append mode and append data to it Without wasting much time lets start working. Create a New C file of whatever name you want mine is files.c. create the basic schema of C language. #include <stdio.h> int main(int argc, char const *argv[]) { /* code */ return 0; } Read From the file Create a FILE pointer to point a stream and open a file into the read mode using "r". you may need to include the string.h to your headers. the data of the file is #in

Basics of Pointers In C

Image
Pointers in C A pointer is a variable which contains the address of the variable specified. for example:  int x = 100; int *data; data = &x; this variable data contains the address of the block in the memory which contains the value 100. * specifies that this is the pointer variable. & gives the address. that is the what I am doing here assigning the address of the integer variable x to the pointer variable data. to get the data from the pointers we have to use the * sign. for the previous example to get the data of the memory block of the printf("%d\n",*data );  this should print the output like the image. Further digging into deep for the pointers. we can use them for the array. if we change content in the memory block pointed by the data, then this change should reflect the in the content of the x. Because variable x has the content of that memory block. (this is the great advantage of the using the pointers) we can read, change anything without copying the data fr

How to Get the length of an Array in C

Image
Array in C an array is a collection of a variable of a fixed number of the same data type in a sequential memory. an array can be declared in various ways. one of them is given below int x[] = {200,300,522,655}; char y[] = {'x','s','r'}; int n[10],j; // first declaring the array // then assigning the value. for ( j = 0; j < 10; j++ ) { n[ j ] = j + 100; } indexing in C array starts from the 0. so if you want to access the 4th element then you have to use x[3]. that's enough intro about the array. Getting Length of the Array in C to get the length of the array first we divide the size of the full array by the size of the one element of the array. Please note there is another way of getting the length of the char array (you can use strlen). you can see this in the given program. #include <stdio.h> int main(){ int x[] = {200,300,522,655}; char y[] = {'x','s','r'}; // determining the size of the array // getting the

C/C++ program to check the Palindrome string.

Image
Palindrome string: A string or word which are same from the start and end means word or program which look like same as original and reversed. To do that we have to first reverse the string. Check out our post on the String reverse in C/C++. To write the program of the Palindrome steps used are. Get the input from the user. convert the string to the Lower case to avoid any case sensitivity. apply the logic of the reverse the string. compare the reversed and original string, if same then the string is a palindrome. if different string is not a palindrome. print the result to the user. without wasting much time lets start. First of all the C schema. #include <stdio.h> #include <string.h> #include <ctype.h> int main(int argc, char const *argv[]) { return 0; } ctype.h is used to get the advantage of the function tolower() later in the program. Now take the input from the user. char name[150],rev[150] ; int l=0; printf("Enter a string to check palindrome\n");

C/C++ program to Reverse a String

Image
String In C language string is an Array of the Char variable. to write a program of the string reverse we have to break this program into steps as always. Take input from the user. Reverse the string using the for loop print the final answer to the user. First of All, create a schema for the program. #include <stdio.h> #include <string.h> int main(int argc, char const *argv[]) { return 0; } Now write some code for the Taking input from the user. scanf is fine here but keep in mind it can take input up to first space. char name[150],rev[150] ; int l=0; printf("Enter a string to reverse\n"); // taking input from the user // string should not have the space in between scanf("%s",&name); This code will take a string from the user, now implement the main logic. what we are doing here is that first, we are counting the length of the string and then storing the last variable into the first element of another variable. l = strlen(name); // main logic

How to Use For Each loop in JavaScript

Image
For Each Loop In JavaScript Hello Guys, Having trouble in understanding the concept of the for each loop in javascript. or having trouble finding the correct source code. Then don't worry this post is for you. What is For Each Loop: this is the loop which automatically iterates through all the elements of an array, it helps in the working with the dynamic array. Let's start working. In javascript for-each loop works in a different way. Here we have to create a function which will be passed to the foreach function of the array. Enough discussion. Start working. First, create an array of names of the actor. var heros = ["Tony","Captain","Batman","SuperMan"] Now create the function with the logic that has to be passed in the for each loop. function myfunc(item){ console.log(item + " is a very good superhero"); }  Now call the function in the foreach function of array // running the foreach loop 4 times heros.forEach(myfunc); You c