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

C Programming Examples

The document provides a series of C programming examples covering basic concepts such as printing messages, checking prime numbers, performing arithmetic operations, and evaluating conditions. Each example includes code snippets along with explanations of the logic behind them. The programs demonstrate fundamental programming skills and concepts in C, making it a useful resource for beginners.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C Programming Examples

The document provides a series of C programming examples covering basic concepts such as printing messages, checking prime numbers, performing arithmetic operations, and evaluating conditions. Each example includes code snippets along with explanations of the logic behind them. The programs demonstrate fundamental programming skills and concepts in C, making it a useful resource for beginners.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

C Programming Examples

1. C Hello World Program


#include <stdio.h>

int main() {

printf(“Hello, World!\n”);

return 0;

2. C Program to Print Your Own Name


#include <stdio.h>

int main() {

printf(“Your Name\n”);

return 0;

3. C Program to Check Whether a Number is Prime or Not

#include <stdio.h>

int main() {

int num, i, isPrime = 1; // Assume the number is prime initially

// Input from the user

printf("Enter a number: ");

scanf("%d", &num);

// Check if the number is less than 2 (not prime)


if (num <= 1) {

isPrime = 0; // Numbers less than or equal to 1 are not prime

} else {

// Loop through numbers from 2 to num/2

for (i = 2; i <= num / 2; i++) {

if (num % i == 0) {

isPrime = 0; // num is divisible by i, so it's not prime

break;

// Output the result

if (isPrime) {

printf("%d is a prime number.\n", num);

} else {

printf("%d is not a prime number.\n", num);

return 0;

4. C Program to Multiply two Floating-Point Numbers


#include <stdio.h>
int main() {
float num1, num2, product;
// Input two floating-point numbers
printf("Enter two floating-point numbers: ");
scanf("%f %f", &num1, &num2);

// Multiply the numbers


product = num1 * num2;

// Output the product


printf("Product: %f\n", product);

return 0;
}
5. C Program to Print the ASCII Value of a Character
#include <stdio.h>
int main() {
char character;

// Input a character from the user


printf("Enter a character: ");
scanf("%c", &character);

// Print the ASCII value of the character


printf("The ASCII value of '%c' is %d\n", character, character);

return 0;
}
6. C Program to Swap Two Numbers
#include <stdio.h>
int main() {
int num1, num2, temp;

// Input two numbers


printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Swap the numbers using a temporary variable


temp = num1;
num1 = num2;
num2 = temp;

// Output the swapped values


printf("After swapping:\n");
printf("num1 = %d\n", num1);
printf("num2 = %d\n", num2);

return 0;
}
7. C Program to Calculate Fahrenheit to Celsius
#include <stdio.h>
int main() {
float fahrenheit, celsius;
// Input temperature in Fahrenheit
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);

// Convert Fahrenheit to Celsius


celsius = (fahrenheit - 32) * 5 / 9;

// Output the result


printf("Temperature in Celsius: %.2f\n", celsius);

return 0;
}

8. C Program to Print Prime Numbers From 1 to N


#include <stdio.h>
int main() {
int i, j, n;

// Input the number N


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

// Print prime numbers between 1 and N


printf("Prime numbers between 1 and %d are: ", n);
for (i = 2; i <= n; ++i) {
int isPrime = 1;
// Check if i is divisible by any number between 2 and i/2
for (j = 2; j <= i / 2; ++j) {
if (i % j == 0) {
isPrime = 0; // i is divisible by j, so it's not prime
break;
}
}

// If i is prime, print it
if (isPrime)
printf("%d ", i);
}

return 0;
}
9. C Program to Check Whether a Number is Positive, Negative, or Zero
#include <stdio.h>
int main() {
int num;
// Input the number
printf("Enter a number: ");
scanf("%d", &num);

// Check if the number is positive, negative, or zero


if (num > 0) {
printf("%d is a positive number.\n", num);
} else if (num < 0) {
printf("%d is a negative number.\n", num);
} else {
printf("%d is zero.\n", num);
}
return 0;
}
10.C Program to Check Whether Number is Even or Odd
#include <stdio.h>
int main() {
int num;

// Input the number


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

// Check if the number is even or odd


if (num % 2 == 0) {
printf("Even number\n");
} else {
printf("Odd number\n");
}
return 0;
}
11.C Program to Calculate Sum of Natural Numbers
#include <stdio.h>
int main() {
int n, sum;

// Input the value of N


printf("Enter a number (N): ");
scanf("%d", &n);
// Calculate the sum of natural numbers up to N
sum = n * (n + 1) / 2;
// Output the result
printf("The sum of natural numbers up to %d is: %d\n", n, sum);

return 0;
}
12.C Program to Print Alphabets From A to Z Using Loop
#include <stdio.h>
int main() {
char ch;

// Loop through the alphabets from A to Z


for (ch = 'A'; ch <= 'Z'; ch++) {
printf("%c ", ch);
}
printf("\n"); // Move to the next line after printing all characters
return 0;
}
13.Program to calculate simple interest
#include <stdio.h>

int main() {
float principal, rate, time, interest;

// Input from user


printf("Enter principal amount: ");
scanf("%f", &principal);

printf("Enter rate of interest: ");


scanf("%f", &rate);

printf("Enter time in years: ");


scanf("%f", &time);

// Calculate simple interest


interest = (principal * rate * time) / 100;

// Display the result


printf("Simple Interest: %.2f\n", interest);

return 0;
}
14.C Program to Check Armstrong Number:
#include <stdio.h>
int main() {
int num, temp, remainder, sum = 0;
// Input from user
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
// Calculate the sum of the cubes of the digits
while (temp != 0) {
remainder = temp % 10;
sum += remainder * remainder * remainder; // Cube of the digit
temp /= 10;
}
// Check if the sum is equal to the original number
if (sum == num) {
printf("%d is an Armstrong number.\n", num);
} else {
printf("%d is not an Armstrong number.\n", num);
}
return 0;
}
15.Program to find the largest number among three numbers
#include <stdio.h>
int main() {
int num1, num2, num3;
// Taking input from the user
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
// Compare the numbers to find the largest
if (num1 >= num2 && num1 >= num3) {
printf("%d is the largest number.\n", num1);
}
else if (num2 >= num1 && num2 >= num3) {
printf("%d is the largest number.\n", num2);
}
else {
printf("%d is the largest number.\n", num3);
}
return 0;
}

You might also like