LAB - C Programming 1-15
LAB - C Programming 1-15
"
#include <stdio.h>
#include<conio.h>
int main( ) {
printf("Hello, World!");
return 0;
}
2. C Program to Print an Integer (Entered by the User)
#include <stdio.h>
#include<conio.h>
int main( ) {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
3. C Program to Add Two Integers
#include <stdio.h>
#include<conio.h>
int main( )
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
4. C Program to Multiply Two Floating-Point Numbers
#include <stdio.h>
#include<conio.h>
int main() {
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
product = a * b;
printf("Product = %lf", product);
return 0; }
5. C Program to Find ASCII Value of a Character
#include <stdio.h>
#include<conio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
printf("ASCII value of %c = %d", c, c);
return 0; }
6. C Program to Find the Size of int, float, double and char
#include<stdio.h>
#include<conio.h>
int main() {
int intType;
float floatType;
double doubleType;
char charType;
printf("Size of int: %d bytes\n", sizeof(intType));
printf("Size of float: %f bytes\n", sizeof(floatType));
printf("Size of double: %lf bytes\n", sizeof(doubleType));
printf("Size of char: %c byte\n", sizeof(charType));
return 0; }
7. C Program to Compute Quotient and Remainder
#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0; }
8. C Program to Swap Two Numbers with using temporary variables
#include<stdio.h>
int main() {
int first, second, temp;
clrscr();
printf("Enter first number: ");
scanf("%d”, &first);
printf("Enter second number: ");
scanf("%d", &second);
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, first number = %d\n", first);
printf("After swapping, second number = %d", second);
return 0; }
9. C program to Swap Numbers Without Using Temporary Variables
#include <stdio.h>
int main()
{
int a, b;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
a = a - b;
b = a + b;
a = b - a;
printf("After swapping, a = %d\n", a);
printf("After swapping, b = %d", b);
return 0;
}
10. C Program to Check Whether a Number is Even or Odd
#include <stdio.h>
#include<conio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is even", num);
else
printf("%d is odd", num);
return 0;
}
11. 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);
lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
12. C Program to Find the Largest Number Among Three Numbers
#include <stdio.h>
int main() {
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);
if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);
if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);
return 0;
}
13. C Program to Check Leap Year
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// leap year if perfectly divisible by 400
if (year % 400 == 0)
{
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0)
{
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0)
{
printf("%d is a leap year.", year);
}
// all other years are not leap years
else
{
printf("%d is not a leap year.", year);
}
return 0;
}
14. C Program to Check Whether a Number is Positive or Negative
#include <stdio.h>
int main()
{
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num <= 0.0)
{
if (num == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
}
else
printf("You entered a positive number.");
return 0;
}
15. C Program to Check Whether a Character is an Alphabet or not
#include <stdio.h>
#include<conio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
printf("%c is an alphabet.", c);
else
printf("%c is not an alphabet.", c);
return 0;
}