Simple C Programs Part 2

Gokul
3 min readJan 11, 2023

--

  1. Multiplication Table

Input :Enter a number: 7

Output :
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

#include <stdio.h>

int main(void) {
int num;

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

for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num,i, num * i);
}

return 0;
}

2. Print the values from 1 to 9

Input : Counter =0

Output: 0,1,2,3,4,5,6,7,8,9

#include <stdio.h>

int main(void) {
// Initialize a counter variable
int counter;

// Use a for loop to repeat a block of code
for (counter = 0; counter < 10; counter++) {
printf("%d , ", counter);
}

return 0;
}

3.Fibonacci series program

This program generates and prints the Fibonacci series for a given number of terms. The user is prompted to enter the number of terms using the printf() and scanf() functions, and the value is stored in a variable called n. The program then uses a for loop to generate the Fibonacci series by adding the previous two numbers in the series, and printing each new number.

Input : 5

Output : 0,1,1,2,3

#include <stdio.h>

int main() {
int n, first = 0, second = 1, next, c;

printf("Enter the number of terms: ");
scanf("%d", &n);

printf("Fibonacci Series: %d, %d, ", first, second);

for (c = 0; c < n - 2; c++) {
next = first + second;
printf("%d, ", next);
first = second;
second = next;
}

return 0;
}

4.Prime or Not Program

This program checks if a given integer is a prime number or not. The user is prompted to enter a positive integer using the printf() and scanf() functions, and the value is stored in a variable called n. The program then uses a for loop to check if the number is divisible by any number from 2 to n/2. If the number is divisible by any number from 2 to n/2 then the number is not prime otherwise it is prime.

#include <stdio.h>

int main() {
int n, i, flag = 0;

printf("Enter a positive integer: ");
scanf("%d", &n);

for(i = 2; i <= n/2; ++i) {
if(n%i == 0) {
flag = 1;
break;
}
}

if (flag == 1)
printf("%d is not a prime number.", n);
else
printf("%d is a prime number.", n);

return 0;
}

5. Array Program

This program takes in a number of elements(n) as input and user is prompted to enter n numbers and stores them in an array. After storing the numbers the program prints all the numbers in the array.

#include <stdio.h>

int main() {
int n, i, arr[100];

printf("Enter the number of elements (1-100): ");
scanf("%d", &n);

printf("Enter %d elements: ", n);
for(i = 0; i < n; ++i) {
scanf("%d", &arr[i]);
}

printf("Elements in array are: ");
for(i = 0; i < n; ++i) {
printf("%d ", arr[i]);
}

return 0;
}

6. String Program

This program takes two strings as input from the user and compares them using the strcmp() function from the string.h library. If the function returns 0, the strings are equal, otherwise, they are not equal.

#include <stdio.h>
#include <string.h>

int main() {
char str1[20], str2[20];

printf("Enter first string: ");
scanf("%s", str1);

printf("Enter second string: ");
scanf("%s", str2);

int result = strcmp(str1, str2);

if(result == 0) {
printf("Both strings are equal.");
} else {
printf("Both strings are not equal.");
}

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