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

Modulo 3

The document contains code snippets from programming exercises on control flow (loops), data types (int and float), type conversions, and logical computing in C programming language. The code examples demonstrate using conditional statements, reading input, loops (while, for, do-while), finding maximum values, and converting between data types. They appear to be examples for students learning basic C programming concepts.

Uploaded by

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

Modulo 3

The document contains code snippets from programming exercises on control flow (loops), data types (int and float), type conversions, and logical computing in C programming language. The code examples demonstrate using conditional statements, reading input, loops (while, for, do-while), finding maximum values, and converting between data types. They appear to be examples for students learning basic C programming concepts.

Uploaded by

HUNTER Y TANK
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Universidad Tecnológica de Querétaro

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

Módulo 3 - Control de flujo (bucles), tipos int y float, conversión de tipos y


lógica informática herramienta externa

Lenguaje de programación
Alumno:
Alan Eduardo Ortega Herrera

Grupo:
E187

Profesor:

Yara Odeth Sainz García.


Fecha:
Septiembre 22 del 2023.
 3.1.1.4 LAB: The conditional statement - more conditional

Código:

#include <stdio.h>

int main(void)
{
int a = 10;

if (a > 9)
puts("First condition is true");
else
puts("First condition is false");

if (a < 9)
puts("Second condition is true");
else
puts("Second condition is false");

if (a == 9 + 1)
puts("Third condition is true");
else
puts("Third condition is false");
return 0;
}

 3.1.2.5 LAB: Big numbers


Código:

#include <stdio.h>

int main(void)
{
int ipAddressPart1, ipAddressPart2, ipAddressPart3, ipAddressPart4;

printf("Introduce el primer valor: ");


scanf("%d", &ipAddressPart1);
printf("Introduce el segundo valor: ");
scanf("%d", &ipAddressPart2);
printf("Introduce el tercer valor: ");
scanf("%d", &ipAddressPart3);
printf("Introduce el cuarto valor: ");
scanf("%d", &ipAddressPart4);

if (ipAddressPart1 >= 0 && ipAddressPart1 <= 255 &&


ipAddressPart2 >= 0 && ipAddressPart2 <= 255 &&
ipAddressPart3 >= 0 && ipAddressPart3 <= 255 &&
ipAddressPart4 >= 0 && ipAddressPart4 <= 255)
{
unsigned long int ipAddress32Bit = 0;

ipAddress32Bit += ipAddressPart1 * 256 * 256 * 256;


ipAddress32Bit += ipAddressPart2 * 256 * 256;
ipAddress32Bit += ipAddressPart3 * 256;
ipAddress32Bit += ipAddressPart4;

printf("Human readable IP address is: %d.%d.%d.%d\n", ipAddressPart1,


ipAddressPart2, ipAddressPart3, ipAddressPart4);
printf("IP address as a 32-bit number : %lu\n", ipAddress32Bit);
}
else
puts("Incorrect IP Address.");

return 0;
}
#include <stdio.h>
int main(void)
{
int ipAddressPart1, ipAddressPart2, ipAddressPart3, ipAddressPart4;

printf("Enter the first value: ");


scanf("%d", &ipAddressPart1);
printf("Enter the second value: ");
scanf("%d", &ipAddressPart2);
printf("Enter the third value: ");
scanf("%d", &ipAddressPart3);
printf("Enter the fourth value: ");
scanf("%d", &ipAddressPart4);

if (ipAddressPart1 >= 0 && ipAddressPart1 <= 255 &&


ipAddressPart2 >= 0 && ipAddressPart2 <= 255 &&
ipAddressPart3 >= 0 && ipAddressPart3 <= 255 &&
ipAddressPart4 >= 0 && ipAddressPart4 <= 255)
{
unsigned long int ipAddress32Bit = 0;

ipAddress32Bit += ipAddressPart1*256*256*256;
ipAddress32Bit += ipAddressPart2*256*256;
ipAddress32Bit += ipAddressPart3*256;
ipAddress32Bit += ipAddressPart4*1;

printf("Human readable IP address is: %d.%d.%d.%d\n", ipAddressPart1,


ipAddressPart2, ipAddressPart3, ipAddressPart4);
printf("IP address as a 32-bit number : %lu\n", ipAddress32Bit);
}
else
puts("Incorrect IP Address.");

return 0;
}

 3.1.3.3 LAB Conversions

Código:

/* finding the larger of two numbers */

#include <stdio.h>

int main(void)
{
/* the two numbers */
int number1, number2;

/* we will save the larger number here */


int max;

/* read two numbers */


scanf("%d", &number1);
scanf("%d", &number2);

/* we temporarily assume that the first number is the larger one */


/* we will check it soon */
max = number1;

/* we check if the assumption was true */


if(number2 > max)
max = number2;

/* we print the result */


printf("The largest number is %d \n",max);

/* we finish the program successfully */


return 0;
}

 3.1.3.4 LAB Conversions

Código:

/* finding the largest of three numbers */

#include <stdio.h>

int main(void)
{
/* the three numbers */
int number1, number2, number3;

/* we will save the largest number here */


int max;

/* read three numbers */


scanf("%d", &number1);
scanf("%d", &number2);
scanf("%d", &number3);

/* we temporarily assume that the first number is the largest one */


/* we will check it soon */
max = number1;

/* we check if the second value is the largest */


if(number2 > max)
max = number2;

/* we check if the third value is the largest */


if(number3 > max)
max = number3;

/* we print the result */


printf("The largest number is %d \n",max);

/* we finish the program successfully */


return 0;
}
 3.1.3.6 LAB Conversions

Código:

/*
1. max = -999999999;
2. read number
3. if(number == -1) print max next stop;
4. if(number > max) max = number
5. go to 2
*/
 3.1.3.6 LAB: Type conversions: Part 1

Código:

#include <stdio.h>

int main(void)
{
float gradeAverage;
int finalGrade;

scanf("%f", &gradeAverage);
finalGrade = (int)gradeAverage;

if (finalGrade == 1)
puts("Very bad");

if (finalGrade == 2)
puts("Bad");

if (finalGrade == 3)
puts("Neutral");

if (finalGrade == 4)
puts("Good");

if (finalGrade == 5)
puts("Very good");
return 0;
}
 3.1.3.7 LAB: Type conversions: Part 2

Código:

#include <stdio.h>

int main(void)
{
float notExactFive = 5.4;
float notExactNumber = 6.7;
int exactFive;
int roundedNumber;

if (notExactNumber - (int)notExactNumber > 0.5)


{
roundedNumber = (int)notExactNumber + 1;
}
else
{
roundedNumber = (int)notExactNumber;
}
exactFive = (int)notExactFive;

printf("Five is: %d\n", exactFive);


printf("Rounded to seven: %d\n", roundedNumber);
return 0;
}

 3.1.4.4 Loops

Código:

#include <stdio.h>

int main(void)
{
/* temporary storage for incoming numbers */
int number;

/* we will store the currently greatest number here */


int max = -100000;

/* get the first value */


scanf ("%d", &number);

/* if the number is not equal to -1 we will continue */


while (number != -1) {

/* is the number greater than max? */


if (number > max)
/* yes – update max */
max = number;

/* get next number */


scanf ("%d", &number);

/* print the largest number */


printf("The largest number is %d \n", max);

/* finish the program successfully */


return 0;
}

 3.1.4.5 Loops

Código:

/* the program reads a sequence of numbers


and counts how many numbers are even and odd;
the program terminates when "0" is entered */
#include <stdio.h>

int main(void)
{
/* count the numbers here */
int Evens = 0, Odds = 0;

/* store the incoming numbers here */


int Number;

/* read the first number */


scanf("%d", &Number);

/* 0 terminates execution */
while(Number != 0) {
/* check if the number is odd */
if(Number % 2)
/* increase the odd counter */
Odds++;
else
/* increase the even counter */
Evens++;
/* read the next number */
scanf("%d", &Number);
}
/* print results */
printf("Even numbers: %d\n", Evens);
printf("Odd numbers: %d\n", Odds);
return 0;
}
 3.1.4.9 Loops

Código:

#include <stdio.h>

int main(void) {
int exp;
int pow = 1;

for(exp = 0; exp < 16; exp++) {


printf("2 to the power of %d is %d\n", exp, pow);
pow *= 2;
}
return 0;
}
 3.1.4.11 Loops

Código:

/* The break variant */

#include <stdio.h>

int main(void) {
int number;
int max = -100000;
int counter = 0;

for( ; ; ){
scanf("%d",&number);
if(number == -1)
break;
counter++;
if(number > max)
max = number;
}
if(counter)
printf("The largest number is %d \n",max);
else
printf("Are you kidding? You haven't entered any number!");
return 0;
}

 3.1.4.12 Loops

Código:

/* The continue variant */

#include <stdio.h>

int main(void) {
int number;
int max = -100000;
int counter = 0;

do {
scanf("%d",&number);
if(number == -1)
continue;
counter++;
if(number > max)
max = number;
} while (number != -1);
if(counter)
printf("The largest number is %d\n",max);
else
printf("Are you kidding? You haven't entered any number!");
return 0;
}

 3.1.4.13 LAB: Loops: while

Código:

#include <stdio.h>

int main(void)
{
int input1;
int input2 = -1;

while(input2 != 0)
{
scanf("%d", &input1);
scanf("%d", &input2);
printf("Sum: %d\n", input1 + input2);
}

if(input1==99)
puts("Finish.");

return 0;
}

 3.1.4.14 LAB: Loops: while

Código:

#include <stdio.h>

int main(void)
{
int input;
int counter = 0;

scanf("%d", &input);
do
{
int innerCounter=-1;
do
{
printf("*#");
innerCounter++;
} while (counter>innerCounter);
printf("\n");
counter++;
} while (input > counter && counter<20);

return 0;
}

 3.1.4.15 LAB: Loops: while

Código:

#include <stdio.h>

int main(void)
{
int input;
int i, j;
scanf("%d", &input);
for (i = 0; i < input && i < 20; i++)
{
printf("*");
for (j = 0; j < i; j++)
printf(" ");
printf("*\n");
}
if (input > 20)
input = 20;
for (i = input-1; i >= 0; i--)
{
printf("*");
for (j = 0; j < i; j++)
printf(" ");
printf("*\n");
}
return 0;
}

 3.1.5.10 Computer logic

Código:

/* equivalent to division by 2 –> VarS == -4 */


VarS = Signed >> 1;

/* equivalent to multiplication by 4 –> VarS == -32 */


VarS = Signed << 2;

/* equivalent to division by 4 –> VarU == 1 */


VarU = Unsigned >> 2;

/* equivalent to multiplication by 2 –> VarU == 12 */


VarU = Unsigned << 1;

 3.1.5.11 LAB: Logical expressions

Código:

#include <stdio.h>

int main(void)
{
int year;

scanf("%d", &year);

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


printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}

 3.1.5.11 LAB: Single bits

Código:

#include <stdio.h>

int main(void)
{
int input;
int nibbleH, nibbleL;
scanf("%d", &input);
nibbleH = (input >> 4) & 15;
nibbleL = input & 15;

printf("H nibble: %d\n", nibbleH);


printf("L nibble: %d\n", nibbleL);
return 0;
}

You might also like