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

(NOTES) C-Program PDF

The document contains code examples demonstrating different programming concepts in C including: 1) A Fahrenheit to Celsius conversion table generated using a while loop. 2) A Fahrenheit to Celsius conversion table generated using a for loop. 3) An if statement example to test for leap years. 4) Examples of functions including ones to check for leap years, swap variables, and sort numbers.

Uploaded by

yeehaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views

(NOTES) C-Program PDF

The document contains code examples demonstrating different programming concepts in C including: 1) A Fahrenheit to Celsius conversion table generated using a while loop. 2) A Fahrenheit to Celsius conversion table generated using a for loop. 3) An if statement example to test for leap years. 4) Examples of functions including ones to check for leap years, swap variables, and sort numbers.

Uploaded by

yeehaa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

While Statement:

C = (5/9)(F -32)
0 -17
20 -6
40 4

300 148

#include <stdio.h>
#include <stdlib.h>
/* print Fahrenheit-Celsius table for fahr = 0, 20, Comments: explains what program does.
, 300 */ Anything between /* and */, or after // are
ignored by the compiler
int main ( int argc, char * argv[])
{
int fahr, celsius; This is a declaration which announces the
int lower, upper, step; properties of variables; it consists of a type
name and a list of variables.
int means that the variables listed are integers.
lower = 0; /*lower limit of temp table*/ Assignment statements: cause arithmetic to
upper = 300; /* upper limit */ be carried out.
step = 20; /* step size */ Individual statements are separated by
semicolons.
fahr = lower;
while (fahr <= upper) { While loop: loop that repeats one per output
line.
<=, >=, == are to compare.
The condition in parentheses is tested. If it is
true ( fahr <= upper) when the body of the loop
(the 3 statements enclosed in braces) is
executed. Then the condition is retested and if
true, executed again. When the test becomes
false, the loop ends.
celsius = 5 * (fahr-32) / 9;
printf (%d\t&d\n, fahr, celsius); %d specifies an integer argument.
\t is a tab
fahr = fahr + step
}
return EXIT_SUCCESS
}
For Statement

#include <stdio.h>

/* print Fahr-Celsius table */

int main (int argc, char * argv[])


{
int fahr;

for (fahr = 0; fahr <= 300; fahr = fahr + 20)

printf (%3d %6.1f\n, fahr, (5.0/9.0)*(fahr-32);


}

%d print as decimal integer


%6d print as decimal integer, at least 6 characters wide
%f print as floating point
%.2f print as floating point, 2 characters after decimal point
%6.2f print as floating point, at least 6 wide and 2 after decimal point
IF Statement

//To test for leap year >= 1582


#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define START_OF_GREG_CALENDAR 1582


int main ( int argc, char * argv[])
{
int year; scan(%d, &variable)
printf (please enter the year you are interested\n);
scanf (%d, &year);

assert (year >= START_OF_GREG_CALENDAR);


if ((year % 4) == 0) % is the remainder (i.e. the remainder
{ when year divided by 100)

if ((year % 100) == 0) == to compare

{ ==, <=, >= are always used in if and


if ((year % 400) == 0) while statements.
{
printf( IS A LEAP YEAR!\n);
}

else
{
printf (IS NOT A LEAP YEAR!\n)
}
}

else
{
printf( IS A LEAP YEAR!\n);
}

else
{
printf( NOT A LEAP YEAR!\n);
}

return EXIT_SUCCESS;
}
isLeapYear function

#include <stdio.h>
#include <stdlib.h>

int isLeapYear (int year) {


int result;

if (year < 1582)


result = 0;

if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)


result = 1;

else
result = 0;

return result;
}

int main (int argc, char * argv []) {


int year;

printf ("please enter the year you are interested in\n");


scanf ("%d", &year);

if (isLeapYear(year))
printf ("%d is a leap year!\n", year);
else
printf ("%d is not a leap year!\n", year);

return EXIT_SUCCESS;

}
Swap Function

//Swap Function

#include <stdio.h>
#include <stdlib.h>

void swap (int *first, int *second) {


int m;

m = *first;
*first = *second;
*second = m;
}

int main (int argc, char * argv[]) {


int first = 1;
int second = 2;

swap (&first, &second);

return first;
}

//Sort 3 numbers

#include <stdio.h>
#include <stdlib.h>

int main (int argc, char * argv[]) {


int first, second, third;
int m , n, o;
scanf ("%d %d %d", &first, &second, &third);

if (first > second) {


m = first;
first = second;
second = m;
}

if (first > third) {


n = first;
first = third;
third = n;
}

if (second > third) {


o = second;
second = third;
third = o;
}

printf ("%d\n%d\n%d\n", first, second, third);

return EXIT_SUCCESS;
}
//ROT13

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#define FIRST_L_LETTER 'a'


#define MID_L_LETTER 'm'
#define LAST_L_LETTER 'z'

#define FIRST_U_LETTER 'A'


#define MID_U_LETTER 'M'
#define LAST_U_LETTER 'Z'

char encode (char letter) {

if (letter <= LAST_L_LETTER && letter >= FIRST_L_LETTER) {


if (letter <= MID_L_LETTER) {
letter = letter + 13;
}
else {
letter = letter - 13;
}
}
else if (letter <= LAST_U_LETTER && letter >= FIRST_U_LETTER) {
if (letter <= MID_U_LETTER) {
letter = letter + 13;
}
else {
letter = letter - 13;
}
}

printf ("%c", letter);


return letter;
}

int main (int argc, char * argv[]) {


char letter;
scanf ("%c", &letter);

while (letter) {
scanf ("%c", &letter);
encode (letter);
}

return EXIT_SUCCESS;
}

void testEncode (void) {


assert (encode ('A') == ('A'));
assert (encode ('1') == ('1'));
assert (encode (' ') == (' '));
}
Void and while functions

Void Function
#include <stdio.h>
#include <stdlib.h>

#define LINE ******

void printChapter (int chapterNum) {


printf (LINE);
printf ("chapter %d", chapterNum);
printf (LINE);
printf ("\n\n\n");

int main (int argc, char * argv[]) {


printChapter (1);
printChapter (2);
return EXIT_SUCCESS;
}

While Loop
#include <stdio.h>
#include <stdlib.h>
#define LINE "***"

void printChapter (int chapterNum) {


printf (LINE);
printf ("chapter %d", chapterNum);
printf (LINE);
printf (\n\n\n");
}

int main (int argc, char * argv[]) {


int chapterNum;
chapterNum = 1;

while (chapterNum <= 11) { chapterNum = chapterNum + 1;


printChapter (chapterNum); can be replaced with:
chapterNum = chapterNum + 1; chapterNum += 1;

}
return EXIT_SUCCESS;
}
While loop

#include <stdio.h>
#include <stdlib.h>

int printWondrous (int start) {


int counter = 1;

while (start != 1) {

counter ++;

if (start % 2 == 0) {
start = start/2;
printf (" %d", start);
}
else {
start = (3*start + 1);
printf (" %d", start);
}
}

if (start == 1) {
printf ("\n");
}
return counter;
}

int main (int argc, char * argv[]) {


int start;
scanf ("%d", &start);
printWondrous (start);
return EXIT_SUCCESS;
}
simpleEncode.c
// simpleEncode.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define STOP -1
#define ALPHABET_SIZE 27

char encode (char plainChar, char *permutation);


void testEncode (void);

int main (int argc, char * argv[]) {


char permutation[ALPHABET_SIZE];

scanf ("%s", permutation);


char plainChar = getchar();

while (plainChar != STOP) {


int encodedChar = encode (plainChar, permutation);
printf ("%c", encodedChar);
plainChar = getchar();
}

testEncode();

return EXIT_SUCCESS;
}

void testEncode (void) {


assert (encode ('A',"abcdefghijklmnopqrstuvwxyz") == 'A');
assert (encode ('?',"abcdefghijklmnopqrstuvwxyz") == '?');
assert (encode (' ',"abcdefghijklmnopqrstuvwxyz") == ' ');
assert (encode ('\n',"abcdefghijklmnopqrstuvwxyz") == '\n');

assert (encode ('a',"abcdefghijklmnopqrstuvwxyz") == 'a');


assert (encode ('m',"abcdefghijklmnopqrstuvwxyz") == 'm');
assert (encode ('z',"abcdefghijklmnopqrstuvwxyz") == 'z');

assert (encode ('a',"bcdefghijklmnopqrstuvwxyza") == 'b');


assert (encode ('m',"bcdefghijklmnopqrstuvwxyza") == 'n');
assert (encode ('z',"bcdefghijklmnopqrstuvwxyza") == 'a');

assert (encode ('a',"qwertyuiopasdfghjklzxcvbnm") == 'q');


assert (encode ('b',"qwertyuiopasdfghjklzxcvbnm") == 'w');
assert (encode ('z',"qwertyuiopasdfghjklzxcvbnm") == 'm');
}

char encode (char plainChar, char *permutation) {

char encodedChar;

if ( (plainChar >= 'a') && (plainChar <= 'z') ) {


encodedChar = permutation [plainChar - 'a'];
} else {
encodedChar = plainChar;
}
return encodedChar;
}

You might also like