Online Web Compiler and cmd line C/Java programmes

Don't have a Linux box yet or emulator, but want to programme?

You can try running simple programs in an online virtual terminal here:

http://www.compileonline.com/compile_c_online.php

For example, the Prime Numbers C programme example on my Linux Basics page here:

http://www.stevepedwards.com/OldLinuxPages/C_and_Java_Info.html

can be seen working, by pasting the text into the terminal window, running the program, and entering arguments on the STD INPUT line at the page bottom.

Paste the programme text between the stars below into the left window of the compiler:

*******************************************************************

#include <stdio.h>
#include <math.h>
int main()
{
int i,j;
int a;
int input;
printf("Input prime limit eg < 1000..");
scanf("%d", &input);
for( i=2; i<input; i=i+1 )
{
a=0;
for(j=2; j<i; j=j+1)
if (i%j == 0)
a=1;
if (a == 0)
printf("%d ", i);
}
return 0;
}

****************************************************************

TTY

 

Click the Compile and Execute button top left:

TTY2

Use the STD In Line for the argument e.g. a number less than 1000, or more if you like, say 999 then click the Compile button again, and Prime Numbers up to 997 are listed:

TTY3

Cool Eh?

-------------------------------------------------------

If you wish to run the C programme in Linux Mint (and most others) you need to install "build-essential" that includes "gcc" and "c++" amongst other things:

MintAspire-5630 / # sudo apt-get install --no-install-recommends build-essential
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
dpkg-dev g++ g++-4.8 libc-dev-bin libc6-dev libstdc++-4.8-dev
Suggested packages:
debian-keyring g++-multilib g++-4.8-multilib gcc-4.8-doc libstdc++6-4.8-dbg
glibc-doc libstdc++-4.8-doc
Recommended packages:
libalgorithm-merge-perl
The following NEW packages will be installed
build-essential dpkg-dev g++ g++-4.8 libc-dev-bin libc6-dev
libstdc++-4.8-dev
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 14.4 MB of archives.
After this operation, 44.0 MB of additional disk space will be used.
Do you want to continue [Y/n]?
Get:1 http://archive.ubuntu.com/ubuntu/ saucy/main libc-dev-bin amd64 2.17-93ubuntu4 [80.9 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ saucy/main libc6-dev amd64 2.17-93ubuntu4 [3,091 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ saucy-updates/main libstdc++-4.8-dev amd64 4.8.1-10ubuntu9 [1,752 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ saucy-updates/main g++-4.8 amd64 4.8.1-10ubuntu9 [8,690 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ saucy/main g++ amd64 4:4.8.1-2ubuntu3 [1,448 B]
Get:6 http://archive.ubuntu.com/ubuntu/ saucy-updates/main dpkg-dev all 1.16.12ubuntu1.3 [769 kB]
Get:7 http://archive.ubuntu.com/ubuntu/ saucy/main build-essential amd64 11.6ubuntu5 [5,606 B]
Fetched 14.4 MB in 23s (606 kB/s)
Selecting previously unselected package libc-dev-bin.
(Reading database ... 147836 files and directories currently installed.)
Unpacking libc-dev-bin (from .../libc-dev-bin_2.17-93ubuntu4_amd64.deb) ...
Selecting previously unselected package libc6-dev:amd64.
Unpacking libc6-dev:amd64 (from .../libc6-dev_2.17-93ubuntu4_amd64.deb) ...
Selecting previously unselected package libstdc++-4.8-dev:amd64.
Unpacking libstdc++-4.8-dev:amd64 (from .../libstdc++-4.8-dev_4.8.1-10ubuntu9_amd64.deb) ...
Selecting previously unselected package g++-4.8.
Unpacking g++-4.8 (from .../g++-4.8_4.8.1-10ubuntu9_amd64.deb) ...
Selecting previously unselected package g++.
Unpacking g++ (from .../g++_4%3a4.8.1-2ubuntu3_amd64.deb) ...
Selecting previously unselected package dpkg-dev.
Unpacking dpkg-dev (from .../dpkg-dev_1.16.12ubuntu1.3_all.deb) ...
Selecting previously unselected package build-essential.
Unpacking build-essential (from .../build-essential_11.6ubuntu5_amd64.deb) ...
Processing triggers for man-db ...
Setting up libc-dev-bin (2.17-93ubuntu4) ...
Setting up libc6-dev:amd64 (2.17-93ubuntu4) ...
Setting up libstdc++-4.8-dev:amd64 (4.8.1-10ubuntu9) ...
Setting up g++-4.8 (4.8.1-10ubuntu9) ...
Setting up g++ (4:4.8.1-2ubuntu3) ...
update-alternatives: using /usr/bin/g++ to provide /usr/bin/c++ (c++) in auto mode
Setting up dpkg-dev (1.16.12ubuntu1.3) ...
Setting up build-essential (11.6ubuntu5) ...

------------------------------------------------------------

Now you can run the primes programme from the cmd line, after creating the binary:

MintAspire-5630 stevee # gcc -v prime.c -o print-primes -lm
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.8.1-10ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)
COLLECT_GCC_OPTIONS='-v' '-o' '/print-primes' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/cc1 -quiet -v -imultiarch x86_64-linux-gnu /prime.c -quiet -dumpbase prime.c -mtune=generic -march=x86-64 -auxbase prime -version -fstack-protector -Wformat -Wformat-security -o /tmp/cc5BGE7J.s
GNU C (Ubuntu/Linaro 4.8.1-10ubuntu9) version 4.8.1 (x86_64-linux-gnu)
compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.1-p2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-linux-gnu/4.8/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
GNU C (Ubuntu/Linaro 4.8.1-10ubuntu9) version 4.8.1 (x86_64-linux-gnu)
compiled by GNU C version 4.8.1, GMP version 5.1.2, MPFR version 3.1.1-p2, MPC version 1.0.1
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: b946adc090b5605f865521be8b22959c
COLLECT_GCC_OPTIONS='-v' '-o' '/print-primes' '-mtune=generic' '-march=x86-64'
as -v --64 -o /tmp/ccgEExAg.o /tmp/cc5BGE7J.s
GNU assembler version 2.23.52 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.23.52.20130913
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/4.8/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-o' '/print-primes' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.8/collect2 --sysroot=/ --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -z relro -o /print-primes /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/4.8/crtbegin.o -L/usr/lib/gcc/x86_64-linux-gnu/4.8 -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/4.8/../../.. /tmp/ccgEExAg.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/x86_64-linux-gnu/4.8/crtend.o /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crtn.o

--------------------------------------------

Now you can see the created binary "print-primes":

MintAspire-5630 stevee # ls 
Desktop Documents Downloads Music Pictures primes.c print-primes Public Templates Videos

Now run the binary:

MintAspire-5630 stevee # ./print-primes
Input prime limit eg < 1000..999
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997

Now go and find out how to install the right JDK for your install, and do the same for the java version of the prime programme shown in the Old Pages Example here...

http://www.stevepedwards.com/OldLinuxPages/C_and_Java_Info.html

For Java, there are many Development Kit (JDKs) and Runtime environments (JREs) as can be seen with a wide search of what's available:

# apt-get install jdk*

Reading package lists... Done

Building dependency tree

Reading state information... Done

Note, selecting libkjdsp-java for regex jdk*

Note, selecting openjdk-6-demo for regex jdk*

Note, selecting jdresolve for regex jdk*

Note, selecting jd for regex jdk*

Note, selecting libjdom1-java for regex jdk*

Note, selecting libpostgis-jdbc for regex jdk*

Note, selecting gjdoc-native for regex jdk*

Note, selecting openjdk-6-dbg for regex jdk*

.......blah......

Research your needs if you are a programmer, or go trial and error.

All that is required for java compilation on the cmd line, is:

# javac

and

# java

along with the necessary libraries, that will come with the correct JDK choice made above. I found the openjdk-6-source worked for me for a previously written java program, primes_findpubKey.java

which is a slightly modified version of the C programme above.

(For the current Mint, I found  openjdk-7-jdk to try which listed javac, once installed)

MintAspire-5630 / # apt-get install openjdk-7-jdk

Create a text file of the java code below in:

MintAspire-5630 / # vi /primes_findpubKey.java

*********************************************************

//** primes_findpubKey.java

import java.util.Scanner;

public class primes_findpubKey

{

public static void main ( String args[ ] )

{

Scanner stdin = new Scanner ( System.in) ;

int i ;

int j ;

int a ;

int k = 0 ;

//int pubKey ;

int logic ;

// int limit;

System.out.println("Input prime upper limit eg < 1000..");

int limit = stdin.nextInt();

System.out.println("Input pubKey...");

int pubKey = stdin.nextInt();

for( i = 2 ; i <= limit ; i = i + 1 )

{

a=0;

for( j = 2 ; j < i ; j = j+1 )

if ( i % j == 0)

a=1;

if (a == 0)

System.out.print( "," + i );

System.out.print("") ;

if ( pubKey % i == 0)

//k = 1 ;

System.out.println( "A Private KEY IS : " + i );

}

}

}

****************************************************************

Now run the java compiler on the text file:

MintAspire-5630 / # javac /primes_findpubKey.java

This generates the file /primes_findpubKey.class

To run the .class file, cd into that same directory, then use java with the class extension dropped (note no fwd slash either for the root dir where the .class file is!)

MintAspire-5630 / # java primes_findpubKey
Input prime upper limit eg < 1000..
225
Input pubKey...
50963
,2,3,5,7,11A Private KEY IS : 11
,13,17,19,23,29,31,37,41A Private KEY IS : 41
,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113A Private KEY IS : 113
,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223

This programme was originally thought about when studying shared key encryption in college Network Security classes, that uses this large prime number as an example in the theory OU Powerpoint here:

ISCW_Cryptography_and_VPNs_OU_template_v1.ppt

# java primes_findpubKey

Input prime upper limit eg < 1000..

1374

Input pubKey...

1506181

,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1013,1019,1021,1031,1033,1039,1049,1051,1061,1063,1069,1087,1091,1093,1097

A Private KEY IS : 1097

,1103,1109,1117,1123,1129,1151,1153,1163,1171,1181,1187,1193,1201,1213,1217,1223,1229,1231,1237,1249,1259,1277,1279,1283,1289,1291,1297,1301,1303,1307,1319,1321,1327,1361,1367,1373

A Private KEY IS : 1373

This can be verified with the linux command factor. When run and 1506181 input, it shows these 2 prime numbers as the only 2 factors!

root@LinuxLaptop:~# factor 

1506181

1506181: 1097 1373

The factor programme will also tell you if a number is a prime number or not too - if it's only output is the input, then that number is prime by definition e.g.

MintAspire-5630 / # factor 311
311: 311

------------------------------------------------------------------------------------

*** I just found out why programs that use the math.h header link file won't compile - they need the gcc -lm switch after the executable file name to link the ACTUAL library to the compile process in this version of Linux/gcc whatever.
I did not need this in prior versions years back.
For example, running a simple text file for a programme that requires standard maths functions like sqrt(x), sin(x), cos(x), pow(x,y) etc. fails:
********************************************************************************
// roottest.c
#include <stdio.h>
#include <math.h>
int main()
{
int input;
int root;
printf("\nEnter integer number to get rooted.. ");
scanf("%d", &input);
root = sqrt(input);
//root=pow(x, 0.5) //both work !
printf("The root of that is.. %d \n", root);
return 0;
}
********************************************************************************

IomegaMint semester4 # gcc -Wall /roottest.c -o /roottest.prog
/tmp/cc0Hbq2A.o: In function `main':
roottest.c:(.text+0x35): undefined reference to `sqrt'
collect2: error: ld returned 1 exit status

Adding the -lm compiles correctly:
gcc -Wall /roottest.c -o /roottest.prog -lm

The program runs:

IomegaMint semester4 # /roottest.prog
Enter integer number to get rooted.. 49
The root of that is.. 7

...and just for beginners, here's another one to try:

********************************************************************************

// command line: gcc -Wall /College/LinuxPrograms/wages.c -o /wages.prog -lm
// wages.c
#include <stdio.h>

// ignores all on this line
/* this gives take home wage for given input */
int main()
{
/* variables and objects declared here */

float hours, workrate, wage, net;
printf("\nPlease input rate in pounds per hour...");

scanf("%f", &workrate);
printf("\nPlease input total hours worked...");

scanf("%f", &hours);
wage = workrate*hours;

printf("\nYour gross wage is %2.2f pounds", wage);

net = wage - (wage*0.25);
printf("\nand your take home wage is... %2.2f pounds", net);

getchar();
getchar();
return 0;
}

********************************************************************************

You should get:

IomegaMint semester4 # /wages.prog

Please input rate in pounds per hour...7

Please input total hours worked...98

Your gross wage is 686.00 pounds
and your take home wage is... 514.50 pounds

*******************************************************************************

NB - see the Post on tmux - if you run a prog in tmux that uses iteration, it automatically counts them, so if you run the prime numbers program, it will number the actual prime number in the series - this is used in esoteric maths and number theory when finding patterns within prime numbers like Prime Quadruplets:

./primeworks.prog
Input prime limit eg < 1000..30
1: 2
2: 3
3: 5
4: 7
5: 11
6: 13
7: 17
8: 19
9: 23
10: 29

http://en.wikipedia.org/wiki/Prime_quadruplet

http://www.code144.com/2012/04/the-numbers-7129-6105195/

If you are bit more serious about learning programming then practise commenting each line so you understand it in your own words e.g. the C prog above, slightly improved by tidying up the code and commented:

#include <stdio.h> /*primes.c : this primes test method uses 2 incrementing loops, both starting at 2, that check the test prime against numbers below or equal to it that give no remainder or modulus 0; starts i at 2 and j at 2 so i/j = 1 so, so no remainder, so i is prime*/
int main()
{
int a,i,j; /* i is an incrementing test prime, where i/j has no modulus remainder ==0
/so true, so prime; a is true/false condition;
/j is incrementing divisor always <= i */
int input; /*user limit*/
printf("Input prime limit eg < 1000.."); /*low to start, does 10000 in 30secs*/
scanf("%d", &input); /*user input upper limit integer*/
for( i=2; i<=input; i=i+1 ) /*first test prime value is 2 (1 is not a prime!).
/start loop for input upper limit counter has to be more than 2.
/NOTE increment does not happen at start, so i=2 for first loop*/
{
a=0; /*initialise divisor condition at false 0 each loop test so check i/j runs*/
for(j=2; j<i; j=j+1) /*loop start 2 for first test int 2, when i=2 so 2/2 is true for first loop*/
if (i%j == 0) /*if i/j has no remainder; testprime i/j = 1 */
a=1; /*if i/j has no remainder a = true;*/
if (a == 0) /*if a = true, printf */
printf("%d Prime\n", i); /*i starts at 2 so prints 1st prime as 2*/
else
printf("%d\n", i);
}
printf("\n"); /*Stay Tidy! Give's cmd line back on newline at output end*/
return 0;
}

Doing this may make you think more and experiment with something else, like adding the final printf statement.

PrimesVim.png

Another thing I learned from experimenting was that the prog still awaits input silently if piped into grep though it appears not running due to no user prompt:

$ ./primes | grep 997
999
997 Prime 

Comments are closed.

Post Navigation