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

Modulo 2

The document discusses code examples for basic data types, operations, and flow control in C programming. It includes multiple code snippets showing the use of variables, operators, character values, conditions, and formatted input/output.

Uploaded by

HUNTER Y TANK
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)
58 views

Modulo 2

The document discusses code examples for basic data types, operations, and flow control in C programming. It includes multiple code snippets showing the use of variables, operators, character values, conditions, and formatted input/output.

Uploaded by

HUNTER Y TANK
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/ 37

Universidad Tecnológica de Querétaro

DTAI. TSU en Mecatrónica-Area de automatización

Módulo 2 - Tipos de datos básicos, operaciones y control de flujo (declaraciones


de toma de decisiones) Herramienta externa

Lenguaje de programación
Alumno:
Alan Eduardo Ortega Herrera

Grupo:
E187

Profesor:

Yara Odeth Sainz García.


Fecha:
Septiembre 17 del 2023.
 2.1.1.6 LAB: Floating point: Part 1

Código:

#include <stdio.h>

int main()
{
printf("The value of seven is: %f\n", 7.000000);
printf("The value of eight and a half is: %f\n", 8.500000);
return 0;
}

 2.1.1.7 LAB: Variables: Continued

Código:

#include <stdio.h>

int main()
{
float tenValue = 10.000000;
printf("The value of nine is: %f\n", 9.000000);
printf("The value of ten is: %f\n", tenValue);
return 0;
}
 2.1.1.8 LAB: Variables: Continued

Código:

#include <stdio.h>

int main()
{
float halfValue = 0.500000;
float piValue = 3.141593;
printf("The value of half is: %f\n", halfValue);
printf("The value of Pi is: %f\n", piValue);
return 0;
}
 2.1.2.12 LAB: Operators: Part 1

Código:

#include <stdio.h>

int main()
{
float halfValue = 0.500000;
float piValue = 3.141593;
printf("The value of half is: %f\n", halfValue);
printf("The value of Pi is: %f\n", piValue);
return 0;
}

 2.1.2.13 LAB: Operators: Part 2

Código:

#include <stdio.h>

int main()
{
int fourValue, fiveValue;

fourValue = 2 + 2;
fiveValue = 2 + 3;
printf("The value of four is: %d\n", fourValue);
printf("The value of five is: %d\n", fiveValue);
return 0;
}

 2.1.2.14 LAB: Operators: Part 3

Código:

#include <stdio.h>

int main()
{
float tenValue, twelveValue;

tenValue = 2.000000 + 3.000000 + 5.000000;


twelveValue = 12.500000 - 0.500000;

printf("The value of ten is: %f\n", tenValue);


printf("The value of twelve is: %f\n", twelveValue);
return 0;
}
 2.1.2.14 LAB: Operators: Part 3

Código:

#include <stdio.h>

int main()
{
float tenValue, twelveValue;

tenValue = 2.000000 + 3.000000 + 5.000000;


twelveValue = 12.500000 - 0.500000;

printf("The value of ten is: %f\n", tenValue);


printf("The value of twelve is: %f\n", twelveValue);
return 0;
}
 2.1.2.15 LAB: Operators: Part 4

Código:

#include <stdio.h>

int main()
{
int tenValue = 3 * 8 % 14;
int twentyValue = 2 * tenValue + 10 % 5;

printf("The value of ten is: %d\n", tenValue);


printf("The value of twenty is: %d\n", twentyValue);
return 0;
}
 2.1.2.16 LAB: Operators: Part 5

Código:

#include <stdio.h>

int main()
{
int xValue = 4 * 6 % 5;
int eightValue = 2 * xValue + 10 % 5;
printf("The value of eight is: %d\n", eightValue);
return 0;
}
 2.1.2.17 LAB: Operators: Part 6

Código:

#include <stdio.h>

int main(void)
{
int xValue = 5;
int yValue = 9;
int result;
int bigResult;

xValue = xValue + 3;
yValue = yValue - xValue;

result = xValue * yValue;


result = result + result;
result = result - 1;

yValue = result % result;

result = result + xValue + result;


bigResult= result * result * result;
result = result + xValue * yValue;

printf("result: %d\n", result);


printf("big result: %d\n", bigResult);
return 0;
}
 2.1.2.18 LAB: Priorities and parentheses: Part 1

Código:

#include <stdio.h>

int main(void)
{
int xValue = 5;
int yValue = 9;
int result = (xValue + yValue) * 2;
int bigResult = xValue + yValue * 6;

printf("the result is: %d\n", result);


printf("the big result is: %d\n", bigResult);
return 0;
}
 2.1.2.19 LAB: Priorities and parentheses: Part 2

Código:

#include <stdio.h>

int main(void)
{
int xValue = 3;
int yValue = 2;
int result = (xValue + yValue) * (2 + yValue);
int smallResult = xValue + yValue * (4 - xValue);

printf("the result is: %d\n", result);


printf("the small result is: %d\n", smallResult);
return 0;
}
 2.1.2.20 LAB: Priorities and parentheses: Part 3

Código:

#include <stdio.h>

int main(void)
{
int xValue = 5;
int yValue = 3;
int result = (xValue % yValue) * (14 % yValue);
int smallResult = xValue + (10 % 4 % xValue);

printf("the result is: %d\n", result);


printf("the small result is: %d\n", smallResult);
return 0;
}
 2.1.2.20 LAB: Priorities and parentheses: Part 4 – your own computation

Código:

#include <stdio.h>

int main(void)
{
float startValue = 100;
float interestRate = 0.015;
float firstYearValue = startValue + startValue * interestRate;
float secondYearValue = firstYearValue + firstYearValue * interestRate;
float thirdYearValue = secondYearValue + firstYearValue * interestRate;

printf("After first year: %f\n", firstYearValue);


printf("After second year: %f\n", secondYearValue);
printf("After third year: %f\n", thirdYearValue);
return 0;
}
 2.1.2.22 LAB: Operators and shortcuts

 Código:

#include <stdio.h>

int main(void)
{
int xValue = 5;
int yValue = 9;
int result;
int bigResult;

xValue += 3;
yValue -= xValue;

result = xValue * yValue;


result += result;
result--;

yValue = result % result;


result += xValue + result;
bigResult= result * result * result;
result += xValue * yValue;

printf("result: %d\n", result);


printf("big result: %d\n", bigResult);
return 0;
}

 2.1.3.8 LAB: Character types and values: Part 1

 Código:

#include <stdio.h>

int main(void)
{
printf("Diff beetween '%c' and '%c' is : %d\n", 'c', 'a', 'c' - 'a');
printf("Diff beetween '%c' and '%c' is : %d\n", 'a', 'c', 'a' - 'c');
return 0;
}
 2.1.3.9 LAB: Character types and values: Part 2

 Código:

#include <stdio.h>

int main(void)
{
char firstLetter = 'A';
char firstSmallLetter = 'a';
char lastLetter = 'Z';
char lastSmallLetter = 'z';

printf("Upper case letters beetween (and with) '%c' and '%c' is : %d\n",
lastLetter, firstLetter, lastLetter - firstLetter + 1);
printf("Lower case letters beetween (and with) '%c' and '%c' is : %d\n",
lastSmallLetter, firstSmallLetter, lastSmallLetter - firstSmallLetter + 1);
return 0;
}
 2.1.3.10 LAB: Character types and values: Part 3

 Código:

#include <stdio.h>

int main(void)
{
char zero = '0';

printf("'1' - '0' is: %d\n", '1' - zero);


printf("'2' - '0' is: %d\n", '2' - '0');
printf("'3' - '0' is: %d\n", '3' - zero);
printf("'4' - '0' is: %d\n", '4' - zero);
printf("'5' - '0' is: %d\n", '5' - zero);
printf("'6' - '0' is: %d\n", '6' - zero);
printf("'7' - '0' is: %d\n", '7' - zero);
printf("'8' - '0' is: %d\n", '8' - zero);
printf("'9' - '0' is: %d\n", '9' - zero);
printf("'0' - '0' is: %d\n", '0' - zero);
return 0;
}
 2.1.4.8 LAB: Conditions and conditional executions: Part 1

 Código:

#include <stdio.h>

int main(void)
{
int a = 10;
if (a > 9)
puts("First condition is true");
if (a < 9)
puts("Second condition is true");
if (a == 9 + 1)
puts("Third condition is true");
return 0;
}
 2.1.4.9 LAB: Conditions and conditional executions: Part 2

 Código:

#include <stdio.h>

int main(void)
{
int n = -3;
if (n < 0)
{
printf("The absolute value of %d is %d\n", n, -n);
}
printf("The value of n is %d\n", n);
return 0;
}
 2.1.4.10 LAB: Conditions and conditional executions: Part 3

 Código:

#include <stdio.h>

int main(void)
{
int dayOfWeek = 1;

if (dayOfWeek == 1)
puts("The day of the week is: Monday");

if (dayOfWeek == 2)
puts("The day of the week is: Tuesday" );

if (dayOfWeek == 3)
puts("The day of the week is: Wednesday");

if (dayOfWeek == 4)
puts("The day of the week is: Thursday");

if (dayOfWeek == 5)
puts("The day of the week is: Friday");

if (dayOfWeek == 6)
puts("The day of the week is: Saturday");

if (dayOfWeek == 0)
puts("The day of the week is: Sunday");

return 0;
}

 2.1.5.3 Formatted input/output

 Código:

#include <stdio.h>

int main(void) {
int VarInt;
char VarChar;
float VarFloat;

VarInt = 2012;
VarChar = 'r';
VarFloat = 3.1415;

printf("The year is %d. The radius is denoted as %c while PI is equal to %f",


VarInt, VarChar, VarFloat);
return 0;
}

 2.1.5.4 Formatted input/output

 Código:

#include <stdio.h>

int main(void) {
int i;
i = 31;
printf("%d %x %o", i, i, i);
return 0;
}
 2.1.5.9 Formatted input/output

 Código:

#include <stdio.h>

int main(void)
{
int value, square;

printf("Give me a number and I will square it!\n");


scanf("%d", &value);
square = value * value;
printf("You've given me: %d\n", value);
printf("The squared value is: %d\n", square);
return 0;
}
 2.1.5.10 Formatted input/output

 Código:

#include <stdio.h>
#include <math.h>

int main(void) {
float value, squareroot;

printf("Give me a number and I will find its square root:\n");


scanf("%f", &value);
if(value >= 0.0) {
squareroot = sqrt(value);
printf("You have given me: %f\n", value);
printf("The sqaure root is: %f\n", squareroot);
}
return 0;
}
 2.1.5.11 LAB: Printing data: Part 1

 Código:

#include <stdio.h>

int main(void)
{
int day = 20;
int month = 2;
int year = 2016;

printf("%04d-%02d-%02d - YYYY-MM-DD format - ISO 8601\n", year, month, day);


printf("%02d-%02d-%04d - MM-DD-YYYY format\n", month, day, year);
printf("%02d-%02d-%04d - DD-MM-YYYY format\n", day, month, year);
printf("%d-%d-%d - D-M-Y format\n", day, month, year);
return 0;
}
 2.1.5.12 LAB: Printing data: Part 2

 Código:

#include <stdio.h>

int main(void)
{
float studentAYear1 = 4.2;
float studentAYear2 = 4.5;
float studentAYear3 = 4.2;

float studentBYear1 = 4.3;


float studentBYear2 = 4.4;
float studentBYear3 = 4.7;

float studentCYear1 = 4.3;


float studentCYear2 = 4.8;
float studentCYear3 = 4.9;

float studentAAvg = (studentAYear1 + studentAYear2 + studentAYear3) / 3;


float studentBAvg = (studentBYear1 + studentBYear2 + studentBYear3) / 3;
float studentCAvg = (studentCYear1 + studentCYear2 + studentCYear3) / 3;
printf("Student name: 1stYGrade 2ndYGrade 3rdYGrade Avg\n");
printf("Student A %9.2f %9.2f %9.2f %5.2f\n", studentAYear1, studentAYear2,
studentAYear3, studentAAvg);
printf("Student B %9.2f %9.2f %9.2f %5.2f\n", studentBYear1, studentBYear2,
studentBYear3, studentBAvg);
printf("Student C %9.2f %9.2f %9.2f %5.2f\n", studentCYear1, studentCYear2,
studentCYear3, studentCAvg);
return 0;
}

 2.1.5.13 LAB: Printing data: Part 3

 Código:

#include <stdio.h>

int main(void)
{
printf(" ^\n");
printf(" / \\\n");
printf(" / \\\n");
printf("< >\n");
printf(" \\ /\n");
printf(" \\ /\n");
printf(" v\n");
return 0;
}

 2.1.5.14 LAB: Getting data from the user: Part 1

 Código:

#include <stdio.h>

int main(void)
{
int daysCount;
float valuePi;

printf("How many days in the week: ");


scanf("%d", &daysCount);
printf("The value of Pi to two points: ");
scanf("%f", &valuePi);
printf("There are %d in a week.\n", daysCount);
printf("Pi value is %f.\n", valuePi);
return 0;
}
 2.1.5.15 LAB: Getting data from the user: Part 2

 Código:

#include <stdio.h>

int main(void)
{
float valueA;
float valueB;

printf("Value A: ");
scanf("%f", &valueA);
printf("Value B: ");
scanf("%f", &valueB);
printf("%f + %f = %f.\n", valueA, valueB, valueA + valueB);
printf("%f - %f = %f.\n", valueA, valueB, valueA - valueB);
printf("%f * %f = %f.\n", valueA, valueB, valueA * valueB);

return 0;
}
 2.1.5.16 LAB: Getting data from the user: Part 3

 Código:

#include <stdio.h>

int main(void)
{
int day, month;

puts("day:");
scanf("%d", &day);
puts("month:");
scanf("%d", &month);

int dayOfYear = 0;

if (month > 1)
dayOfYear += 31;

if (month > 2)
dayOfYear += 29;
if (month > 3)
dayOfYear += 31;

if (month > 4)
dayOfYear += 30;

if (month > 5)
dayOfYear += 31;

if (month > 6)
dayOfYear += 30;

if (month > 7)
dayOfYear += 31;

if (month > 8)
dayOfYear += 31;

if (month > 9)
dayOfYear += 30;

if (month > 10)


dayOfYear += 31;

if (month > 11)


dayOfYear += 30;

dayOfYear += day;
printf("The day of the year : %d.\n", dayOfYear);
return 0;
}
 2.1.5.17 LAB: Getting data from the user: Part 4

 Código:

#include <stdio.h>

int main(void)
{
int day;

puts("day:");
scanf("%d", &day);
if (day == 1)
printf("The day of the weak is: Monday");

if (day == 2)
printf("The day of the weak is: Tuesday");

if (day == 3)
printf("The day of the weak is: Wednesday");

if (day == 4)
printf("The day of the weak is: Thursday");

if (day == 5)
printf("The day of the weak is: Friday");

if (day == 6)
printf("The day of the weak is: Saturday");

if (day == 7)
printf("The day of the weak is: Sunday");

if (day >= 8)
printf("There is no such day: %d.\n", day);
if (day >= 8)
printf("Input value must be from 0 to 6.");
return 0;

}
 2.1.5.18 LAB: Getting data from the user: Part 5

 Código:

#include <stdio.h>

int main(void)
{
int day, month, year;

puts("day:");
scanf("%d", &day);
puts("month:");
scanf("%d", &month);
puts("year:");
scanf("%d", &year);

int dayOfYear = 0;

if (month > 1)
dayOfYear += 31;

if (month > 2)
{
if (year / 10 == 4)
dayOfYear += 29;
else
dayOfYear += 28;
}

if (month > 3)
dayOfYear += 31;

if (month > 4)
dayOfYear += 30;

if (month > 5)
dayOfYear += 31;

if (month > 6)
dayOfYear += 30;

if (month > 7)
dayOfYear += 31;

if (month > 8)
dayOfYear += 31;

if (month > 9)
dayOfYear += 30;

if (month > 10)


dayOfYear += 31;

if (month > 11)


dayOfYear += 30;
dayOfYear += day;
printf("The day of the year : %d.\n", dayOfYear);

return 0;
}

You might also like