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

Practical Lab-I

Uploaded by

cs.anil788
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Practical Lab-I

Uploaded by

cs.anil788
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Practical Lab (C language)

1 C Program to display "Hello, World!" output


#include<stdio.h>
int main()
{
clrscr();
printf("Hello, World!");
return 0;
}
2 C Program to Print an Integer (Entered by the User)
#include <stdio.h>
int main()
{
int number;
clrscr();
printf("Enter an integer: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
3 C Program to Multiply Two Floating-Point Numbers
#include <stdio.h>
int main()
{
double a, b, product;
clrscr();
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
product = a * b;
printf("Product = %.2lf", product);
return 0;
}
4 C Program to Find ASCII Value of a Character
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
printf("ASCII value of %c = %d", c, c);
return 0;
}
5 C Program to Compute Quotient and Remainder

#include <stdio.h>
int main()
{
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0;
}

6 C Program to Find the Size of int, float, double and char

#include<stdio.h>
int main()
{
int intType;
float floatType;
double doubleType;
char charType;
clrscr();
printf("Size of int: %d bytes\n", sizeof(intType));
printf("Size of float: %f bytes\n", sizeof(floatType));
printf("Size of double: %llf bytes\n", sizeof(doubleType));
printf("Size of char: %c byte\n", sizeof(charType));
return 0;
}
7 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;
}

8 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;
}
9 C Program to Check Whether a Number is Even or Odd
#include <stdio.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;
}

10 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;
}
11 C Program to Find the Largest Number Among Three Numbers

#include <stdio.h>
int main()
{
int 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;
}

12 C Program to Check Whether a Year is leap year or not


#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
}
13 C Program to Check Whether a Character is an Alphabet or not
#include <stdio.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;
}
14 C Program to Calculate the Sum of Natural Numbers
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}
15 C Program to Find Factorial of a Number

#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else {
for (i = 1; i <= n; ++i) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}
return 0;
}

16 C Program to Display Factors of a Number


#include <stdio.h>
int main() {
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (i = 1; i <= num; ++i) {
if (num % i == 0) {
printf("%d ", i); } }
return 0; }
17 C Program to Make a Simple Calculator Using switch...case
#include <stdio.h>
int main() {
char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first + second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first - second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first * second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first / second);
break;
default:
printf("Error! operator is not correct");
}
return 0;
}
18 C Program to Generate Multiplication Table
#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
for (int i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
19 C Program to Display Fibonacci Sequence
#include <stdio.h>
int main() {
int i, n;
int t1 = 0, t2 = 1;
int nextTerm = t1 + t2;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);
for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
20 C Program to Count Number of Digits in an Integer
#include <stdio.h>
int main() {
long long n;
int count = 0;
printf("Enter an integer: ");
scanf("%lld", &n);
do {
n /= 10;
++count;
} while (n != 0);
printf("Number of digits: %d", count);
}
21 C Program to Check Whether a Number is Palindrome or Not
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0)
{
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);
return 0;
}

You might also like