Skip to content

Instantly share code, notes, and snippets.

@YarGnawh
Created September 27, 2016 14:45
Show Gist options
  • Save YarGnawh/589621341aa6dc8210704839c7044114 to your computer and use it in GitHub Desktop.
Save YarGnawh/589621341aa6dc8210704839c7044114 to your computer and use it in GitHub Desktop.

Programming Setup Tutorial

JavaScript (node.js for mac and windows)

Setup

Overview

// single line comment
/*
	multi line comment
*/
// declare variable
var integer = 1352;
var float = 3.1415;
var txt = 'Hi Hi';
// if / else if / else statement
var i = 10;
if (i === 0) {

} else if (i === 1) {

} else {

}
// for loop
for (var x=0; x<10; x++) {

}
// while loop
var y = 0;
while (y < 10) {
	y++;
}
// do while loop
var z = 0;
do {
	z++;
} while (z < 10);
// function declaration
function add(num1, num2) {
	return num1 + num2;
}
var sum = add(5, 7);

Tutorial

sample.js

function drawPyramid(height) {
	for (var i=0; i<height; i++) {
		var row = '';
		for (var s=0; s<height; s++) {
			row += ' ';
		}
		var k = 0;
		while () {
		
		}
		console.log(row);
	}
}

drawPyramid(5);
drawPyramid(10);
bash$ nano sample.js
bash$ node sample.js

C / C++ (gcc for mac, dev-c++ for windows)

Setup

Overview

// single line comment
/*
	multi line comment
*/
// declare variable

char a = 'a';
short b = 6;

// 16-bit integer
int c = 3446;
signed int d = -475;
unsigned int e = 973;

// 32-bit integer
long f = 243578;
signed long g = 
unsigned long h = 

// 64-bit integer
long long i = 
signed long long j = 
unsigned long long k =

// float (IEEE 754 single-precision binary floating-point format)
float l = 3.1415

// double (IEEE 754 double-precision binary floating-point format)
float m = 2.6245762346;

// character string
char n = "Hello World!";
// if / else if / else statement
int i = 10;
if (i === 0) {

} else if (i === 1) {

} else {

}
// for loop
for (int x=0; x<10; x++) {

}
// while loop
int y = 0;
while (y < 10) {
	y++;
}
// do while loop
int z = 0;
do {
	z++;
} while (z < 10);
// function declaration
int add(int num1, int num2) {
	return num1 + num2;
}
int sum = add(5, 7);

Tutorial

fib.c

#include<stdio.h>
 
int main()
{
   int n, first = 0, second = 1, next, c;
 
   printf("Enter the number of terms\n");
   scanf("%d",&n);
 
   printf("First %d terms of Fibonacci series are :-\n",n);
 
   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
 
   return 0;
}
bash$ nano fib.c
bash$ gcc fib.c -o fib
bash$ ./fib

Java (JDK for mac and windows)

Setup

Overview

// single line comment
/*
	multi line comment
*/
// declare variable

char a = 'a';
short b = 6;

// 16-bit integer
int c = 3446;
signed int d = -475;
unsigned int e = 973;

// 32-bit integer
long f = 243578;
signed long g = 
unsigned long h = 

// 64-bit integer
long long i = 
signed long long j = 
unsigned long long k =

// float (IEEE 754 single-precision binary floating-point format)
float l = 3.1415

// double (IEEE 754 double-precision binary floating-point format)
float m = 2.6245762346;

// character string
String str = "abc";
// if / else if / else statement
int i = 10;
if (i === 0) {

} else if (i === 1) {

} else {

}
// for loop
for (int x=0; x<10; x++) {

}
// while loop
int y = 0;
while (y < 10) {
	y++;
}
// do while loop
int z = 0;
do {
	z++;
} while (z < 10);
// function declaration
int add(int num1, int num2) {
	return num1 + num2;
}
int sum = add(5, 7);

Tutorial

PrimeNumber.java

class PrimeNumber {

	// Checks whether an int is prime or not.
	static boolean isPrime(int n) {
		//check if n is a multiple of 2
		if (n%2 == 0) {
			return false;
		}
		//if not, then just check the odds
		for(int i = 3; i*i <= n; i += 2) {
			if (n%i == 0) {
				return false;
			}
		}
		return true;
	} 
	
	// Main function
	public static void main(String[] args) {
		if (isPrime(16) == true) {
			System.out.println("Is Prime Number");
		} else {
			System.out.println("Is NOT Prime Number");
		}
	}
}
bash$ javac PrimeNumber.java 
bash$ java PrimeNumber

Ruby

Setup

Overview

# This is a single line comment.
=begin
	This is a multiline comment and con spwan as many lines as you
	like. But =begin and =end should come in the first line only. 
=end
// declare variable
var integer = 1352;
var float = 3.1415;
var txt = 'Hi Hi';
# if / else if / else statement

x=1
if x > 2
   puts "x is greater than 2"
elsif x <= 2 and x!=0
   puts "x is 1"
else
   puts "I can't guess the number"
end
# until statement

x=1
unless x>2
   puts "x is less than 2"
 else
  puts "x is greater than 2"
end
# for statement
for i in 0..5
	puts "Value of local variable is #{i}"
end

(0..5).each do |i|
	puts "Value of local variable is #{i}"
end
# while loop
# the [do] is optional

$i = 0
$num = 5

while $i < $num  do
	puts("Inside the loop i = #$i" )
	$i +=1
end
# while modifier

$i = 0
$num = 5
begin
   puts("Inside the loop i = #$i" )
   $i +=1
end while $i < $num
# until statement

$i = 0
$num = 5

until $i > $num  do
	puts("Inside the loop i = #$i" )
	$i +=1;
end
# until modifier

$i = 0
$num = 5
begin
	puts("Inside the loop i = #$i" )
	$i +=1;
end until $i > $num
# method declaration

def test(a1="Ruby", a2="Perl")
   puts "The programming language is #{a1}"
   puts "The programming language is #{a2}"
end
test "C", "C++"
test

Tutorial

mulTable.rb

x = (1..12)
y = (1..12)

print '     '
x.each {|i| print "%-3d  " % i}
print "\n     "
x.each {|i| print '---- '}
print "\n"

y.each do |j| 
    print "%-3d| " % j
    x.each {|i| print "%-3d  " % (i*j)}
    print "\n"
end
bash$ nano mulTable.rb
bash$ ruby mulTable.rb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment