0% found this document useful (0 votes)
222 views

Star STAR : Nur Gunawan Sastranegara, Ir., M.Eng. 1

This document contains the source code for several C programs and the output when each program is compiled and run. It explores basic C programming concepts like printing text, variables, arithmetic operations, conditional statements, loops, functions and input/output. The programs demonstrate the use of printf(), getchar() and other standard C functions. Overall, the document serves as an example reference for learning basic C programming.

Uploaded by

nuel_evans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
222 views

Star STAR : Nur Gunawan Sastranegara, Ir., M.Eng. 1

This document contains the source code for several C programs and the output when each program is compiled and run. It explores basic C programming concepts like printing text, variables, arithmetic operations, conditional statements, loops, functions and input/output. The programs demonstrate the use of printf(), getchar() and other standard C functions. Overall, the document serves as an example reference for learning basic C programming.

Uploaded by

nuel_evans
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Nur Gunawan Sastranegara, Ir., M.Eng.

Email: [email protected]

/* no1-1.c */
main()
{
printf (" * ");
}
C> no1-1

*
/* no1-2.c */
main()
{
printf (" * ");
printf (" \n ");
printf (" * ");
}
C> no1-2

*
*
/* no1-3.c */
main()
{
printf (" \"*\" ");
}
C> no1-3

"*"
/* no1-4.c */
main()
{
printf (" *****\
*****\n ");
}

C> no1-5

STAR
***STAR***
/* no2-1.c */
main()
{
int i ;
i = 123 ;
printf (" i = %d \n", i );
}
C> no2-1

i = 123
/* no2-2.c */
main()
{
float x ;
x = 3.14159 ;
printf (" x = %f \n", x) ;
}
C> no2-2

x = 3.141590
/* no2-3.c */
main()
{
float x ;
x = 123.456 ;
printf (" x = %f \n", x) ;
printf (" x = %e \n", x);
}
C> no2-3

C> no1-4

**********
/* no1-5.c */
main()
{
printf (" S ");
printf (" T ");
printf (" A ");
printf (" R ");
printf (" \n***STAR***\n ");
}

x = 123.456001
x = 1.234560E+002
/* no2-4.c */
main()
{
char n ;
n = 'A' ;
printf (" n = %c \n", n);
}

Page 1 of 6
Saturday, 23 April 2005

Nur Gunawan Sastranegara, Ir., M.Eng.


Email: [email protected]

C> no2-4

n=A
/* no2-5.c */
main()
{
int a ;
long b ;
a = 40000 ; b = 40000 ;
printf (" a = %d \n", a) ;
printf (" b = %ld \n", b) ;
}

a = 30 ;
b = 20 ;
c=a+b;
d = a-b;
e=a*b;
f=a%b;
printf ("%d + %d = %d\n", a, b, c) ;
printf ("%d-%d = %d\n", a,b,d) ;
printf ("%d*%d = %d\n", a,b,e) ;
printf ("%d %% %d = %d\n", a,b,f) ;
}
C> no2-8

C> no2-5

a = -25536
b = 40000
/* no2-6.c */
main()
{
char x1, x2, x3 ;
x1 = 'a' ;
x2 = 'b' ;
x3 = 'n' ;
printf ("%c%c%c%c%c%c\n" ,
x2, x1, x3, x1, x3, x1) ;
}

30 + 20 = 50
30 - 20 = 10
30 * 20 = 600
30 % 20 = 10
/* no2-9.c */
main()
{
char n = ' * ' ;
printf (" * = %d \n", n ) ;
printf (" * = %x \n", ' * ' ) ;
printf (" * = %c \n", n ) ;
}
C> no2-9

C> no2-6

banana
/* no2-7.c */
main()
{
float x, y, z ;
x = 32.0 ;
y = 20.0 ;
z=x/y;
printf ("%4.1f/%4.1f = %5.3f \n",
x, y, z) ;
}
C> no2-7

32.0 / 20.0 = 1.600


/* no2-8.c */
main()
{
int a, b, c, d, e, f ;

* = 42
* = 2A
*=*
/* no3-1.c */
main()
{
int i, z ;
i=5;
z = i < 10 ;
printf (" z = %d \n", z ) ;
}
C> no3-1

z=1
/* no3-2.c */
main()
{
int i, z ;
i = 12 ;
Page 2 of 6

Saturday, 23 April 2005

Nur Gunawan Sastranegara, Ir., M.Eng.


Email: [email protected]

z = i < 10 ;
printf (" z = %d \n", z ) ;
}
C> no3-2

z=0
/* no3-3.c */
main()
{
int i, z ;
i=0;
z = i < 10 ;
while ( z ) {
printf (" * ") ;
i=i+1;
z = i < 10 ;
}
}
C> no3-3

/* no3-6.c */
main()
{
while ( 0 )
printf (" * ") ;
}
C> no3-6

/* no3-7.c */
main()
{
char c = 'A' ;
while ( c <= 'Z' ) {
printf ("%c", c );
c++ ;
}
printf(\n);
}

**********
C> no3-7
/* no3-4.c */
main()
{
int i = 0 ;
while ( i < 10 ) {
printf (" * ") ;
i ++ ;
}
printf (" \n ") ;
}

ABCDEFGHIJKLMNOPQRS
TUVWXYZ
/* no3-8.c */
main()
{
int n, i ;
n=i=0;
while ( n < 5 ) {
while ( i < 10 ) {
printf (" * ") ;
i++ ;
}
printf("\n") ;
i = 0;
n++ ;
}

C> no3-4

**********
/* no3-5.c */
main ()
{
while ( 1 )
printf (" * ") ;
}

}
C> no3-8

C> no3-5

*************.............
*************.............
*************.............
*************.............

**********
**********
**********
**********
**********
Page 3 of 6

Saturday, 23 April 2005

Nur Gunawan Sastranegara, Ir., M.Eng.


Email: [email protected]

{
/* no3-9.c */
#include "stdio.h"
main()
{
int n, i ;
n=i=0;
while ( n < 10 ) {
while ( i <= n ) {
putchar (' * ') ;
i++ ;
}
putchar(' \n ') ;
i = 0;
n++ ;
}
}
C> no3-9

*
**
***
****
*****
******
*******
********
*********
**********
/* no3-10.c */
main()
{
int n, w ;
n=w=0;
while( n <= 100 ) {
w += n ;
n++ ;
}
printf(" w = %d \n", w) ;
}
C> no3-10

w = 5050
/* no4-1.c */
main()

int k ;
for (k = 0; k < 5; k++)
printf("k = %d \n", k) ;
}
C> no4-1

k=0
k=1
k=2
k=3
k=4
/* no4-2.c */
#define PRN(k) printf("%d \n", k)
main()
{
int k ;
for ( k = 5; k ; k-- )
PRN(k) ;
}
C> no4-2

5
4
3
2
1
/* no4-3.c */
main()
{
int k, w = 0 ;
for (k = 0; k <= 100; k++)
w += k ;
printf("w = %d \n", w) ;
}
C> no4-3

w = 5050
/* no4-4.c */
main()
{
int i, j ;
for( i=0, j=1; i + j <= 5; i++, j++)
printf("i=%d j=%d
i+j=%d\n", i, j, i+j ) ;
Page 4 of 6

Saturday, 23 April 2005

Nur Gunawan Sastranegara, Ir., M.Eng.


Email: [email protected]

}
C> no4-4

i=0 j=1 i+j=1


i=1 j=2 i+j=3
i=2 j=3 i+j=5
/* no4-5.c */
#include "stdio.h"
main()
{
int n, i ;
for ( n = 0; n < 5; n++ ){
for ( i = 0; i < 10; i++)
putchar(' * ') ;
putchar(' \n ') ;
}
}
C> no4-5

**********
**********
**********
**********
**********
/* no4-6.c */
main()
{
int n, w = 0 ;
for( n = 0; n <= 100; n += 2 )
w += n ;
printf(" w = %d \n", w) ;
}
C> no4-6

w = 2550
/* no5-1.c */
#include "stdio.h"
main()
{
int c ;
c = getchar() ;
putchar(c) ;
}
C> no5-1

abcdef [enter]
a
/* no5-2.c */
#include "stdio.h"
main()
{
int c ;
while( ( c = getchar() ) != EOF )
putchar(c) ;
}
C> no5-2

abcdefg
abcdefg
^Z
/* no5-3.c */
#include "stdio.h"
main()
{
int c ;
while( ( c = getchar() ) != EOF )
putchar( c - 'a' + 'A' ) ;
}
C> no5-3

abcdefg
ABCDEFG

/* no5-4.c */
#include "stdio.h"
main()
{
int c ;
c = getchar() ;
if ( c == 'a' )
printf(" 'a' \n") ;
else
printf( "not 'a' \n" ) ;
}
C> no5-4

s
not 'a'
/* no5-5.c */
#include "stdio.h"
main()
Page 5 of 6

Saturday, 23 April 2005

Nur Gunawan Sastranegara, Ir., M.Eng.


Email: [email protected]

{
int c ;
c = getchar() ;
if ( 'a' <= c && c <= 'z' )
printf(" a small letter \n") ;
}
C> no5-5

g
a small letter
/* no5-6.c */
#include "stdio.h"
main()
{
int c ;
c = getchar() ;
if ( c == '1' || c == '2' )
printf(" '1' or '2' \n") ;
}
C> no5-6

2
'1' or '2'
/* no5-7.c */
#include "stdio.h"
main()
{
int c ;
c = getchar() ;
if ( c == 'a' )
printf(" apple \n") ;
else if ( c == 'b' )
printf( " banana \n" ) ;
else if ( c == 'c' )
printf(" cherry \n" ) ;
else
printf(" ???
\n" );
}
C> no5-7
b [enter]

banana
C> no5-7
d [enter]

???
Page 6 of 6
Saturday, 23 April 2005

You might also like