31.7.25

Installation & Execution: Running C Programs in Visual Studio Code 2022 & Installation


Installation & Execution: Running C Programs in Visual Studio Code 2022


Program 1: Hello World




File name: myfirstprogram.c

Code:

#include <stdio.h> int main() { printf("Hello, world!\n"); return 0; }

What happened:

I opened VS Code, wrote this little program, pressed Run or the green play button.
It said:


Hello, world!


Program 2: Show the Larger Number Between Two Numbers






File name: shows_largerno._bet2no.c 

Code:


#include <stdio.h> int main() { int num1, num2; // Ask for two numbers printf("Enter first number: "); scanf_s("%d", &num1); printf("Enter second number: "); scanf_s("%d", &num2); // Compare the numbers if (num1 > num2) { printf("The larger number is: %d\n", num1); } else if (num2 > num1) { printf("The larger number is: %d\n", num2); } else { printf("Both numbers are equal.\n"); } return 0; }

What happened:

I ran it, gave it two numbers like 50 and 57, and my program told me:



Enter first number: 50 Enter second number: 57 Larger number is: 57

Yup, it worked perfectly. 

Program 3: Calculator in C (Basic 4 Operations +-*/)






File name: calculator_in_c.c

Code:


#include <stdio.h> int main() { float a, b; // gets the input from user printf("Enter first number: "); scanf_s("%f", &a); printf("Enter second number: "); scanf_s("%f", &b); // perform calculations part printf("Addition: %.f\n", a + b); printf("Subtraction: %.f\n", a - b); printf("Multiplication: %.f\n", a * b); // division if (b == 0) { printf("Division: cannot divide by zero\n"); } else { printf("Division: %.2f\n", a / b); } return 0; }

What happened:

I typed 10 and 2 as input.
And my mini-calculator replied:



Addition: 12 Subtraction: 8 Multiplication: 20 Division: 5.00



No comments:

Post a Comment

rating System

Loading...

Understanding Arrays in C

The Bookshelf Analogy & Book-Author Example Arrays are one of the most essential concepts in C programming. They let you store multiple ...