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

CProgramming 1

Uploaded by

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

CProgramming 1

Uploaded by

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

COMPUTER

PROGRAMMING 1

PUPSMB, 1st Sem.,


A.Y. 2024-2025

Engr. Christopher Cunanan

1
C Basic Construct
#include<stdio.h>
#include<math.h>

int main(void)
{

return 0;
} 2
First Program
#include<stdio.h> -header.contains library for input/output
int main(void) -main function. Every C program must
have a main function and/or subfunctions
-int-because it returns an integer
-void-needs no input for it to run
{ -begin
printf("\n\n\n\t\t\t Hello World! \n\n\t");
-print function, message, terminator
return 0; -return function. 0 is error free execution.
} -end
3
printf & scanf Function
printf("\n\n\t\t\t Enter an integer: ");
-printf (“ message here “ );
- \n = new line, \t = tab
scanf (" %d " , &a );
- scanf( “ % placeholder “ , & variable );
- d - integer (whole numbers)
- f - float ( real numbers, + or -, with decimal
- c - character ( letters A – Z, 0 – 9)
- s - string ( words like names, etc)
4
printf for output
printf("\n\n\n\t\t\t The sum is equal to: %d
\n\n\t", sum );

Syntax: printf (“ placeholder”, variable);


- printf( “ % d ” , sum);

d - integer (whole numbers)

-number of placeholder = number of variable/s

5
printf for output
printf("\n\n\n\t\t\t The sum of %d and %d
is equal to: %d \n\n\t",a,b,sum );

Syntax: printf (“ placeholder”, variable);


- printf( “%d %d %d ”, a, b, sum);
d - integer (whole numbers)

-number of placeholder = number of variable/s


-3 integer placeholders = 3 variables(a,b,sum)

6
Add Program (revised)
#include<stdio.h>
int main(void) {

int a,b;
printf("\n\n\t\t\t Enter an integer: ");
scanf ("%d", &a);
printf("\n\n\t\t\t Enter another integer: ");
scanf ("%d", &b);

printf("\n\n\n\t\t\t The sum of %d and %d is equal to:


%d \n\n\t", a, b, a+b );

return 0; }
7
Hypothenuse Program
#include<stdio.h>
#include<math.h>

int main(void) {
float a,b, c;

printf("\n\n\t\t\t Enter the value of the adjacent side a: ");


scanf ("%f", &a);
printf("\n\n\t\t\t Enter the value of the opposite side b: ");
scanf ("%f", &b);

c= sqrt(a*a + b*b);
printf("\n\n\n\t\t\tThe hypothenuse is equal to: %0.2f \n\n\t",
c );
return 0; }
8
#include<stdio.h>
int main(void) {

float A,C,r;
printf("\n\t\t AREA AND CIRCUMFERENCE OF A CIRCLE ");
printf("\n\tEnter the RADIUS OF THE CIRCLE: ");
scanf ("%f", &r);

A= 3.1416*r*r;
C=2*3.1416*r;
printf("\n\t\t\t The AREA OF THE CIRCLE IS %0.2f SQUARE
UNIT/S", A);
printf("\n\t\t\t The CIRCUMFERENCE OF THE CIRCLE IS
%0.2f UNIT/S\n\n\n\n", C);

return 0; }

9
#include<stdio.h>
#define PI 3.1416
#define P printf
#define S scanf

int main(void) {
float A,C,r;
P("\n\n\t\tAREA AND CIRCUMFERENCE OF A CIRCLE ");
P("\n\t\tEnter the value of the RADIUS OF THE CIRCLE: ");
S ("%f", &r);
A= PI*r*r;
C=2*PI*r;

P("\n\tAREA OF THE CIRCLE IS %0.2f SQUARE UNIT/S", A);


P("\n\\tTHE CIRCUMFERENCE IS %0.2f UNIT/S\n\n\n\n", C);
return 0; }

10
Relational and Logical Operators
> - greater than
>= - greater than or equal
< - less than
<= - less than or equal
= - assignment operator (X=5)
== - is equal to (use to compare)
! - not (!= - not equal to)
&& - and operator(both condition = T)
|| - or operator(at least one condition is T)11
Conditional Statements
• if Condition

Syntax: (two alternatives)

if (condition/s)
statement/s
else
statement/s

Ex.
If (BEP==10)
printf(“ This is the Break-Even Point!”);
else
printf(“ You need more sales!”);
12
if Condition (sample program)
#include<stdio.h>
int main(void) {

int Inv;

printf("\n\t\t Enter the current value of stock # AB143: ");


scanf ("%d", &Inv);

if (Inv<=20)
printf("\n\t\tTHIS STOCK HAS REACHED ITS CRITICAL
LEVEL! YOU SHOULD ORDER THIS STOCK ASAP! \n\n\n\n" );

else
printf("\n\n\t\t\t THIS STOCK IS STILL ABOVE THE
TRESHOLD VALUE of 20 UNITS. \n\n\n\n" );

return 0; }
13
Conditional Statements
• if Condition

Syntax: (multiple alternatives)

if condition/s
statement/s
else if condition/s
statement/s
else if condition/s
statement/s

else
statement/s

14
Conditional Statements
• if Condition : (problem application using && operator)

Grade Range | Equivalent Grade Range | Equivalent


98-100 | 1.0 84-86 | 2.25
95-97 | 1.25 81-83 | 2.50
93-94 | 1.50 78-80 | 2.75
90-92 | 1.75 75-77 | 3.0
87-89 | 2.0 74 below | 5.00
Use G for Grade variable ( T ) && ( T) – will result to TRUE
If ((G>=98) && (G<=100)) - this is how you should write your
first if condition!

15
if Condition (sample program)
#include<stdio.h>
int main(void) {
int G;

printf("\n\n\t\t\t Enter your Grade: ");


scanf ("%d", &G);

if ((G>=98) && (G<=100))


printf("\n\n\t\t\tYOUR GRADE IS EQUIVALENT TO 1.0! \n\n\n\n " );
else if ((G>=95) && (G<=97))
printf("\n\n\t\t\tYOUR GRADE IS EQUIVALENT TO 1.25! \n\n\n\n " );

else if ((G>=75) && (G<=77))


printf("\n\n\t\t\tYOUR GRADE IS EQUIVALENT TO 3.0! \n\n\n\n " );
else
printf("\n\n\t\t\tYOUR GRADE IS EQUIVALENT TO 5.0! SEE YOU NEXT
SEMESTER! \n\n\n\n " );

return 0; }
16
if Condition (sample program)
if ( (NCEE>=90) || (EE > 89) )

printf("\n\n\t\t\you are qualified for the scholarship! \n\


n\n\n " );

17
Conditional Statements
• while Repetition Structure (Looping)

Syntax:

while (condition)
{
statement/s
}

18
Conditional Statements
• while Repetition Structure (sample program)

#include<stdio.h>
int main(void) {
int P=2;

printf("\n\n\t\t\t PROGRAM SHOWING THE POWERS OF 2 LESS


THAN 100 ");

while (P<=100)
{
printf("\n\n\t\t\t\t %d", P );
P=P*2;
}

return 0; }
19

You might also like