Posts

Showing posts from 2021

C# - Calculate EMI

C# - Calculate EMI CODE using System ; class Program { static void Main () { // Princile Amount double p = 20000 ; // Number of Months double n = 24 ; // Rate of Interest per annum; Example: 7% double r = 10 ; //Calculate rate per month double rm = r / 12 / 100 ; // EMI can be calculated using the formula below. // [P x R x (1+R)^N]/[(1+R)^N - 1)] double monthlyPayment = ( p * rm * Math . Pow (( 1 + rm ), n )) / ( Math . Pow (( 1 + rm ), n ) - 1 ); double yearlyPayment = monthlyPayment * n ; Console . WriteLine ( "Monthly Payment: {0:0.00}" , monthlyPayment ); Console . WriteLine ( "Yearly Payment: {0:0.00}" , yearlyPayment ); Console . WriteLine ( "{0, -10} {1, 10} {2, 20} {3, 20}" , "Month" , "Interest" , "Principle" , ...

Python - Simple TCP Server and Client

Exported from Notepad++ # Server Code import socket , sys sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) port = 10000 sock . bind (( "127.0.0.1" , port )) sock . listen ( 1 ) print ( "Starting Server..." ) while True : conn , ( addr , port ) = sock . accept () data = conn . recv ( port ) data = ( "Server received your message: " + data . decode ()). encode () conn . sendall ( data ) conn . close () # Client Code import socket message = "Hi. How are you?" client = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) client . connect (( "127.0.0.1" , 10000 )) client . send ( message . encode ()) messageFromServer = client . recv ( 1024 ) client . close () print ( messageFromServer )

C - File Handling - Write lines to a file

C - File Handling - Write lines to a file CODE #include <stdio.h> #include <string.h> int main () { char line [ 500 ]; FILE * outputFile = fopen ( "output.txt" , "w" ); strcpy ( line , "This is line 1\n" ); fprintf ( outputFile , "%s" , line ); fprintf ( outputFile , "%s" , "This is line 2" ); fclose ( outputFile ); return 0 ; }

C - File Handling - Read from a file line by line

C - File Handling - Read from a file line by line CODE #include <stdio.h> int main () { char line [ 500 ]; FILE * filePTR = fopen ( "data.txt" , "r" ); while ( fgets ( line , sizeof ( line ), filePTR )) { printf ( "%s" , line ); } fclose ( filePTR ); return 0 ; }

Java - Console program with a set of menu options

Java - Console program with a set of menu options CODE import java . util . Scanner ; public class Main { public static void main ( String [] args ) { Scanner sc = new Scanner ( System . in ); String userInput = "" ; while (! userInput . equalsIgnoreCase ( "x" )) { System . out . println ( "\n1. Option1" ); System . out . println ( "2. Option2" ); System . out . println ( "3. Option3" ); System . out . println ( "4. Option4" ); System . out . println ( "x. Exit" ); System . out . print ( "Enter your choice: " ); userInput = sc . nextLine (); if (! userInput . equalsIgnoreCase ( "x" )) { switch ( userInput ) { case "1" : ...

HTML - HTML Form Elements; input, text, radio button, button, textarea, date etc

HTML - HTML Form Elements; input, text, radio button, button, textarea, date etc CODE <html> <script> function submit () { document.getElementById ( "divOutput" ). innerHTML = "The button is clicked." ; } </script> <body> <input type = "text" style = "width:300px;" id = "txtURL" /> <br /> <br /> <select style = "width:300px;" > <option value = "Option 1" > Option 1 </option> <option value = "Option 2" > Option 2 </option> <option value = "Option 3" > Option 3 </option> <option value = "Option 4" > Option 4 </option> </select> <br /> <br /> <textarea rows = "10" cols = "30" style = "width:300px;" > This is a sample text...

C# - Calculate the weighted GPA of the given marks or score.

C# - Calculate the weighted GPA of the given marks or score. CODE using System ; class Program { static void Main ( string [] args ) { double marks1 , marks2 , marks3 , marks4 , marks5 ; Console . WriteLine ( "Enter the marks (Max 100)..." ); Console . Write ( "Subject 1: " ); marks1 = Convert . ToDouble ( Console . ReadLine ()); Console . Write ( "Subject 2: " ); marks2 = Convert . ToDouble ( Console . ReadLine ()); Console . Write ( "Subject 3: " ); marks3 = Convert . ToDouble ( Console . ReadLine ()); Console . Write ( "Subject 4: " ); marks4 = Convert . ToDouble ( Console . ReadLine ()); Console . Write ( "Subject 5: " ); marks5 = Convert . ToDouble ( Console . ReadLine ()); if ( marks1 < 0 || marks1 > 100 || marks2 < 0 ||...

C# - Find the power; a number raise to the power of another number

C# - Find the power; a number raise to the power of another number CODE using System ; class Program { static void Main ( string [] args ) { int number = 10 ; int raiseTo = 3 ; int power = 1 ; for ( int i = 1 ; i <= raiseTo ; i ++) { power = power * number ; } Console . WriteLine ( number + " ^ " + raiseTo + " = " + power ); // Output // 10 ^ 3 = 1000 } }

C# - Find the square of a number

C# - Find the square of a number CODE using System ; class Program { static void Main ( string [] args ) { int number = 5 ; int square = number * number ; Console . WriteLine ( "Square of " + number + " = " + square ); // Output // Square of 5 = 25 } }

Java - Read and Write Binary Integer File

Java - Read and Write Binary Integer File CODE import java . io . IOException ; import java . io . FileInputStream ; import java . io . DataInputStream ; import java . io . ByteArrayOutputStream ; import java . io . DataOutputStream ; import java . io . FileOutputStream ; public class Main { public static void main ( String args []) { try { String inputFile = "input.dat" ; int countOfNumbers = 5 ; ByteArrayOutputStream bout = new ByteArrayOutputStream ( countOfNumbers * 4 ); DataOutputStream dout = new DataOutputStream ( bout ); // Write 5 integers to the data stream dout . writeInt ( 100 ); dout . writeInt ( 200 ); dout . writeInt ( 300 ); dout . writeInt ( 400 ); dout . writeInt ( 500 ); // Write it to file. FileOutputStream fout = new FileOu...

Java - Format Output

Java - Format Output CODE public class Main { public static void main ( String args []) { System . out . printf ( "%-20s %6s\n" , "Total:" , 1000 ); System . out . printf ( "%-20s %6s\n" , "Count:" , 34 ); System . out . printf ( "%-20s %6s\n" , "Average:" , 45 ); } }

C# - Generate Fibonacci numbers

C# - Generate Fibonacci numbers CODE using System ; class Program { static void Main ( string [] args ) { int countOfNumbersToGenerate = 10 ; int n1 = 0 ; int n2 = 1 ; for ( int i = 0 ; i < countOfNumbersToGenerate ; i ++) { Console . WriteLine ( n1 ); int total = n1 + n2 ; n1 = n2 ; n2 = total ; } } }

C# - Check if a number is Fibonacci number

C# - Check if a number is Fibonacci number CODE using System ; class Program { static void Main ( string [] args ) { //Provide a number to check if it is Fibonacci number int numberToCheck = 14 ; if ( numberToCheck <= 0 ) Console . WriteLine ( "Please enter a positive integer" ); else { int n1 = 1 ; int n2 = 1 ; int i = 0 ; bool isFibonacci = false ; //Generate the Fibonacci numbers one by one, less than or equal to numberToCheck //After generation, check if the generated number is equal to the number entered while ( i <= numberToCheck ) { if ( numberToCheck == n1 ) { isFibonacci = true ; break ; } int total = n1 + n2 ; n1 = ...

Java - Find Time Elapsed

Java - Find Time Elapsed CODE import java . util .*; public class Main { public static void main ( String [] args ) { long start = System . currentTimeMillis (); long end = System . currentTimeMillis (); double timeElapsed = ( end - start ) / 1000.0 ; System . out . println ( "Time Elapsed: " + timeElapsed + " secs" ); } }

C# - A simple Quiz

C# - A simple Quiz CODE using System ; class Program { static void Main ( string [] args ) { int numberOfQuestions = 5 ; Random r = new Random (); int score = 0 ; //Simple Addition Quiz Console . WriteLine ( "Addition Quiz..." ); for ( int i = 1 ; i <= numberOfQuestions ; i ++) { int number1 = r . Next ( 1 , 100 ); int number2 = r . Next ( 1 , 100 ); int answer = number1 + number2 ; Console . Write ( "Question {0}: {1} + {2} = " , i , number1 , number2 ); string userAnswer = Console . ReadLine (); userAnswer = userAnswer . Trim (); if ( answer . ToString () == userAnswer ) score ++; } Console . WriteLine ( "Your Score: {0} / {1}" , score , numberOfQuestions ); } }

C++ - Find if 3 points or coordinates are collinear

C++ - Find if 3 points or coordinates are collinear CODE //This program takes 3 coordinates as inputs, //...calculates the area of the triangle formed by the 3 points //...and determine if these 3 points are collinear //The 3 points are collinear, if the area is 0 #include <iostream> using namespace std ; int main () { //Declare the required variables to store the 3 coordinates int x1 , y1 , x2 , y2 , x3 , y3 ; //Get the x, y coordinates of the 1st one. //Store it in the variables x1, y1 cout << "Enter x, y separated by spaces of the coordinate 1: " ; cin >> x1 >> y1 ; //Get the x, y coordinates of the 2nd one. //Store it in the variables x2, y2 cout << "Enter x, y separated by spaces of the coordinate 2: " ; cin >> x2 >> y2 ; //Get the x, y coordinates of the 3rd one. //Store it in the variables x3, y3 co...