Posts

Showing posts from 2024

[Tutorial 7] 15.The figure gives a rough sketch of a running track. It includes a rectangular shape and two semi-circles. The length of the rectangular part is 67m and breadth is 21m.Calculate the distance of the running track.

Image
  #include <stdio.h> #define PI 3.142 int main()  { float distance_rectangular, distance_semi_circles, total_distance; float  length_rectangular = 67.0;  float  breadth_rectangular = 21.0;  distance_rectangular = 2 * length_rectangular ; distance_semi_circles = 2 * PI * (breadth_rectangular/2); total_distance = distance_rectangular +distance_semi_circles; /* Display the total distance*/ printf( "The distance of the running track is %.2f meters\n" , total_distance); return 0; }

[Tutorial 7] 14.Write a program that converts inches to centimeters. For example, if the user enters 16.9 for a Length in inches, the output would be 42.926cm. (Hint: 1 inch = 2.54 centimeters.)

#include <stdio.h> int main() { float inches, centimeters; /*Input length in inches from the user*/      printf( "Enter length in inches: " );      scanf( "%f" , &inches); /*Convert inches to centimeters*/      centimeters = inches * 2.54; /* Display the result */      printf( "%.2f inches is equal to %.3f centimeters\n" , inches, centimeters); return 0; }

[Tutorial 7] 13.Enter the name, height, weight and gender of a person and calculate his/her BMI in Kg. BMI = weight/ height2

#include <stdio.h> int main() { char name[50], gender; float height, weight, bmi; /* Input data from user */      printf( "Enter name: " );      scanf( "%s" , name);      printf( "Enter height in meters: " );      scanf( "%f" , &height);      printf( "Enter weight in kilograms: " );      scanf( "%f" , &weight);      printf( "Enter gender (M/F): " );      scanf( " %c" , &gender);  /* Calculate BMI*/      bmi = weight / (height * height); /* Display the result*/      printf( "\n%s's BMI: %.2f\n" , name, bmi); return 0; }

[Tutorial 7] 12.Find the cost of 5 items if the unit price is 10.50 Rupees.

#include <stdio.h> int main()  {      float unit_price = 10.50;      int num_items = 5;      float cost; /* Calculate the cost*/      cost = unit_price * num_items; /* Display the result*/      printf( "The cost of %d items is %.2f Rupees\n" , num_items, cost); return 0; }

[Tutorial 7] 11.Find the value of y using y = 3.5x+5 at x = 5.23.

#include <stdio.h> int main() { float x, y; /* Given value of x*/      x = 5.23; /*Calculate y using the equation y = 3.5x + 5*/      y = 3.5 * x + 5; /*Display the result*/      printf( "When x = %.2f, the value of y is %.2f\n" , x, y); return 0; }

[Tutorial 7] 10.Convert the given temperature in Celsius to Fahrenheit. T(°F) = T(°C) × 1.8 + 32

#include <stdio.h> int main() { float celsius, fahrenheit; /* Input temperature in Celsius from the user*/      printf( "Enter temperature in Celsius: " );      scanf( "%f" , &celsius); /* Convert Celsius to Fahrenheit*/      fahrenheit = celsius * 1.8 + 32; /* Display the temperature in Fahrenheit*/      printf( "%.2f Celsius is equal to %.2f Fahrenheit\n" , celsius, fahrenheit); return 0; }

[Tutorial 7] 9.Calculate average marks of 4 subjects which, entered separately.

#include <stdio.h> int main() { float subject1, subject2, subject3, subject4, average;      /* Input marks of each subject from the user*/      printf( "Enter marks of subject 1: " );      scanf( "%f" , &subject1);      printf( "Enter marks of subject 2: " );      scanf( "%f" , &subject2);      printf( "Enter marks of subject 3: " );      scanf( "%f" , &subject3);      printf( "Enter marks of subject 4: " );      scanf( "%f" , &subject4); /* Calculate average marks*/      average = (subject1 + subject2 + subject3 + subject4) / 4; /* Display the average marks*/      printf( "The average marks of the four subjects is: %.2f\n" , average); return 0; }

[Tutorial 7] 8.Calculate the volume of a cylinder. PI * r2 h

#include <stdio.h> #define PI 3.14159 int main()  { float radius, height, volume; /* Input radius and height from the user*/      printf( "Enter the radius of the cylinder: " );      scanf( "%f" , &radius);      printf( "Enter the height of the cylinder: " );      scanf( "%f" , &height); /* Calculate volume*/      volume = PI * radius * radius * height; /* Display the volume*/      printf( "The volume of the cylinder is: %.2f cubic units\n" , volume); return 0; }

[Tutorial 7] 7.Convert given value in Meter to centimeter.

#include <stdio.h> int main() {      double meters, centimeters;      /* Input length in meters*/      printf( "Enter length in meters: " );      scanf( "%lf" , &meters);      /* Convert meters to centimeters*/      centimeters = meters * 100;      /* Display the result*/      printf( "%.2f meters is equal to %.2f centimeters.\n" , meters, centimeters); return 0; }

[Tutorial 7] 6.What output would you expect from the following program?

#include <stdio.h> int main ( void ) { char c, d; c = 'd'; d = c; printf ( "d = %c\n" , d); return 0; }

[Tutorial 7] 5.Which of the following are invalid constants? Why?

123.456                     0996 0x10.5                     -12E-12 0X0G1                     07777 0001                          1234uL 0xFFFF                   1.2Fe-7 123L                           15,000 0Xab05                   1.234L 0L -597.25              197u 123.5e2                   1 00U .0001                        0XABCDEFL +12                            0xabcu 98.6F                        +123 98.7U 17777s

[Tutorial 7] 4.Which of the following are invalid variable names? Why?

1. Int char 6_05 2. Calloc Xx alpha_beta_routine 3. floating _1312 z 4. ReInitialize _ A$

[Tutorial 7] 3. What output might you expect from the following program?

#include <stdio.h> int main (void) {       int answer, result;     answer = 100;     result = answer - 10;     printf ( "The result is %i\n" , result + 5); return 0; }

[Tutorial 7] 2.Identify the syntactic errors in the following program. Then type in and run the corrected program to ensure you have correctly identified all the mistakes.

#include <stdio.h> int main (Void) ( INT sum; /* COMPUTE RESULT sum = 25 + 37 – 19 /* DISPLAY RESULTS // printf ( "The answer is %i\n" sum); return 0; }

[Tutorial 7] 1.Write a program that subtracts the value 15 from 87 and displays the result, together with an appropriate message, at the terminal.

#include <stdio.h> int main(void) { int sum; /* COMPUTE RESULT */ sum = 87 - 15; /* DISPLAY RESULTS */ printf ( "The answer is %i \n" , sum); return 0; }

[Tutorial 6 ] 4. What output would you expect from the following program?

#include <stdio.h> int main (void) {      printf ( "Testing..." );      printf ( "....1" );      printf ( "...2" );      printf ( "..3" );      printf ( "\n" ); return 0; } OUTPUT => Testing.......1...2..3 This C program defines a main() function that sequentially prints out the strings "Testing...", "....1", "...2", and "..3" each on a separate line using the printf() function. It concludes with a newline character. The program then returns 0, indicating successful execution to the operating system.

[Tutorial 6 ] 2. Write a program that prints the following text at the terminal.

i) * * * * * * * * * * * * * * * * * * * * * #include <stdio.h> int main( void ) { int row,column; for (row=1;row<=6;row++) { for( column=1;column<=row;column++) { printf( "*" ); } printf( "\n" ); } return 0; } This C program prints a pattern resembling a right triangle with asterisks, where the number of asterisks in each row increases from 1 in the first row to 6 in the last row. It achieves this using nested loops: the outer loop controls the number of rows, and the inner loop determines the number of asterisks to print in each row. After printing the required number of asterisks in each row, a newline character is printed to move to the next row. Thus, the result is a right triangle pattern made of asterisks.

[Tutorial 6 ] 1. Write a program that prints the following text at the terminal.

#include <stdio.h> int main ( void ) { printf ( "* In C, lowercase letters are significant. \n" ); printf ( "* main is where program execution begins.\n" ); printf ( "* Opening and closing braces enclose program statements in a routine.\n" ); printf ( "* All program statements must be terminated by a semicolon.\n" ); return 0; }

Variables in C

A variable is a symbolic name that refers to a location in memory where data is stored. It can be used for various purposes and its value can be changed. It is a way of using symbols to identify memory locations so that they can be accessed and manipulated more conveniently. Type variable_list; The example of declaring the variable is given below:int a; float b; char c; Here a, b, c are variables. The int, float, char are the data types. Alternatively, we can assign values to the variables when we declare them, as shown below: int a=10,b=20;//declaring 2 variable of integer type float f=20.8; char c='A'; Rules for defining variables A variable is a name that can consist of letters, numbers, and underscores.  A variable name must begin with a letter or an underscore, not a number.  A variable name cannot contain any spaces.  A variable name cannot be the same as any of the reserved words or keywords in the programming language, such as int, float, etc.

printf() and scanf() in C

In C language, input and output operations are performed by the functions printf() and scanf(), which are built-in library functions defined in the header file stdio.h.

First C Program

Image
Before starting the abcd of C language, you need to learn how to write, compile and run the first c program. To write the first c program, open the C console and write the following code: #include <stdio.h> :-includes the standard input output library functions. The printf() function is defined in stdio.h . int main() :-The main() function is the entry point of every program in c language. printf() :-The printf() function is used to print data on the console. return 0 :-The return 0 statement, returns execution status to the OS. The 0 value is used for successful execution and 1 for unsuccessful execution. You will see the following output on user screen.

Compilation process in c

Image
   What is a compilation?        C is a programming language that converts source code into object code or machine code. The conversion process has four steps: Pre-processing, Compiling, Assembling, and Linking.      The preprocessor eliminates all the comments from the source code and carries out the preprocessor directives. For instance, it substitutes the directive <stdio.h> with the contents of the 'stdio.h' file.       The following are the phases through which our program passes before being transformed into an executable form:         Preprocessor         Compiler         Assembler         Linker Preprocessor A source code file is a text file that contains instructions written in the C programming language. The file name has a ".c" extension to indicate its format. Before compiling the source code, a preprocessor analyzes and modifies it according to certain directives. The preprocessor then passes the expanded code to the compiler, which translates it in

Introduction To C

Image
Dennis Ritchie C programming is considered as the base for other programming languages, that is why it is known as mother language . Because most compilers, JVMs, kernels, etc. are written in C, and because most programming languages (such C++, Java, C#, etc.) adopt C syntax, C language is regarded as the mother tongue of all current programming languages. It offers the fundamental ideas used in numerous languages, including C++, Java, C#, and others, such as the array, strings, functions, file management, etc. C programming language  was developed in 1972 by  Dennis Ritchie  at bell laboratories of AT&T (American Telephone & Telegraph), located in the U.S.A. It was developed to overcome the problems of previous languages such as B, BCPL, etc. Initially, C language was developed to be used in UNIX operating system. It inherits many features of previous languages such as B and BCPL. Let's see the programming languages that were developed before C language. Lan