0% found this document useful (0 votes)
3 views2 pages

C Programming Sample Programs

The document contains sample C programming programs demonstrating basic functionalities such as displaying text, reading user input, performing arithmetic operations, checking even or odd numbers, and determining if a character is a vowel or consonant. Each program includes necessary headers, user prompts, and output statements. These examples serve as a foundational guide for beginners learning C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

C Programming Sample Programs

The document contains sample C programming programs demonstrating basic functionalities such as displaying text, reading user input, performing arithmetic operations, checking even or odd numbers, and determining if a character is a vowel or consonant. Each program includes necessary headers, user prompts, and output statements. These examples serve as a foundational guide for beginners learning C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

C Programming Sample Programs

1. C Program to Display "Hello, World!"

#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}

2. C Program to Print an Integer (Entered by the user)

#include <stdio.h>
int main() {
int number;

printf("Enter an integer: ");

// reads and stores input


scanf("%d", &number);

// displays output
printf("You entered: %d", number);

return 0;
}

3. C Program to Add Two Integers

#include <stdio.h>
int main() {

int number1, number2, sum;

printf("Enter two integers: ");


scanf("%d %d", &number1, &number2);

// calculate the sum


sum = number1 + number2;

printf("%d + %d = %d", number1, number2, sum);


return 0;
}
4. C Program to Check Whether a Number Even or Odd

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

// true if num is perfectly divisible by 2


if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}

5. C Program to Check Whether a Character is a Vowel or Consonant

#include <stdio.h>
int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);

// evaluates to 1 if variable c is a lowercase vowel


lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

// evaluates to 1 if variable c is a uppercase vowel


uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

// evaluates to 1 (true) if c is a vowel


if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}

You might also like