Posts

Showing posts from July, 2021

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...

C# - Find the mode of numbers in an array - Simple Method

C# - Find the mode of numbers in an array - Simple Method CODE using System ; class Program { static void Main ( string [] args ) { int [] numbers = { 3 , 4 , 5 , 6 , 7 , 23 , 12 , 34 , 4 }; int mode = GetMode ( numbers ); Console . WriteLine ( mode ); //Output //4 } private static int GetMode ( int [] numbers ) { int count = numbers . Length ; //Sort the numbers for ( int i = 0 ; i < count - 1 ; i ++) { for ( int j = i ; j < count ; j ++) { if ( numbers [ i ] > numbers [ j ]) { int temp = numbers [ i ]; numbers [ i ] = numbers [ j ]; numbers [ j ] = temp ; } } } int mode = numbers [ 0 ]; int modeOccurences = 1 ; in...

Shell - Basic usage of floating point expressions

Shell - Basic usage of floating point expressions CODE echo "Enter Salary: " read salary awk "BEGIN {print $salary * 2.5 / 100}"