Simple C Programs Part 1

Gokul
2 min readJan 10, 2023

--

It is a series, I started to give more information to my followers and also improve my knowledge.

  1. Hello World
#include <stdio.h>

int main(void) {
printf("Hello, world!\n");
printf("Wellcome to My Profile !! Keep Reading")
return 0;
}

2. Data Types and Declaration

x = 10
y = 3.140000
c = A
b = 1

//How did you declare these data to that desired variables in c programming

Don’t Worry I will Guide You. Just Read,

Example 1

#include <stdio.h>

int main(void) {
// Declare and initialize variables of different data types
int x = 10;
float y = 3.14;
char c = 'A';
_Bool b = 1;

// Print the values of the variables
printf("x = %d\n", x);
printf("y = %f\n", y);
printf("c = %c\n", c);
printf("b = %d\n", b);

return 0;
}

Example 2

#include <stdio.h>
#include <stdbool.h>

int main(void) {
// Declare and initialize variables of different data types
short s = 32767;
long l = 2147483647;
long long ll = 9223372036854775807;
unsigned int ui = 4294967295;
double d = 3.14;
long double ld = 3.14159265358979323846;
bool b = true;

// Print the values of the variables
printf("s = %hd\n", s);
printf("l = %ld\n", l);
printf("ll = %lld\n", ll);
printf("ui = %u\n", ui);
printf("d = %f\n", d);
printf("ld = %Lf\n", ld);
printf("b = %d\n", b);

return 0;
}

3. Leap Year

#include <stdio.h>

int main(void) {
int year;

printf("Enter a 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;
}

4. Factorial

If I give 5 it will need to print 125.

Input: 5

Process: 5*4*3*2*1

Output: 125

#include <stdio.h>

int main(void) {
int num, factorial = 1;

printf("Enter a number: ");
scanf("%d", &num);

for (int i = 1; i <= num; i++) {
factorial *= i;
}

printf("The factorial of %d is %d\n", num, factorial);

return 0;
}

5. Odd or Even

Input: 2

Output: 2 is even

#include <stdio.h>

int main(void) {
int num;

printf("Enter an integer: ");
scanf("%d", &num);

if (num % 2 == 0) {
printf("%d is even\n", num);
} else {
printf("%d is odd\n", num);
}

return 0;
}

Thank You for Reading This. I hope you got some knowledge from this Blog. Work Out with Your Hands

--

--

Gokul
Gokul

Written by Gokul

Cybersecurity Enthusiast | Smart India Hackathon |TN Police Hackathon Finalist | Linux | WebApp Penetration Tester | CCNA |Intern At Coimbatore CyberCrime Dept

No responses yet