SlideShare a Scribd company logo
1. Write a C Program to Add Two Integers
#include <stdio.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;
}
OUTPUT
Enter two integers: 12
11
12 + 11 = 23
2. Write a C Program to Print an Integer (Entered by the User)
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d", &number);
printf("You entered: %d", number);
return 0;
}
OUTPUT
Enter an integer: 25
You entered: 25
3. Write a C Program to Multiply Two Floating-Point Numbers
#include <stdio.h>
int main()
{
float num1, num2, product;
printf("Enter first Number: ");
scanf("%f", &num1);
printf("Enter second Number: ");
scanf("%f", &num2);
product = num1 * num2;
printf ("Product of entered numbers is: %.3f", product);
return 0;
}
OUTPUT
Enter first Number: 12.761
Enter second Number: 89.23
Product of entered numbers is:1138.664
4. Write a 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;
}
OUTPUT
Enter a character: G
ASCII value of G = 71
5. Write a C Program to Compute Quotient and Remainder.
#include <stdio.h>
int main()
{
int dividend, divisor;
int quotient, remainder;
printf("Enter dividend: ");
scanf("%d",&dividend);
printf("Enter divisor: ");
scanf("%d",&divisor);
// Computes quotient
quotient= dividend/divisor;
// Computes remainder
remainder= dividend%divisor;
printf("quotient: %d, remainder: %dn",quotient,remainder);
return 0;
}
OUTPUT
Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1
6. Write a 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;
printf("Size of int: %ld bytesn", sizeof(intType));
printf("Size of float: %ld bytesn", sizeof(floatType));
printf("Size of double: %ld bytesn", sizeof(doubleType));
printf("Size of char: %ld byten", sizeof(charType));
return 0;
}
OUTPUT
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
7. Write a C Program to Swap Two Numbers Using Temporary Variable.
#include<stdio.h>
int main()
{
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
temp = first;
first = second;
second = temp;
printf("nAfter swapping, firstNumber = %.2lfn", first);
printf("After swapping, secondNumber = %.2lf", second);
return 0;
}
OUTPUT
Enter first number: 1.20
Enter second number: 2.45
After swapping, firstNumber = 2.45
After swapping, secondNumber = 1.20
8. Write a 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;
}
OUTPUT
Enter an integer: 7
7 is odd.
9. Write a C Program to Check Odd or Even Using the Ternary Operator.
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num);
return 0;
}
OUTPUT
Enter an integer: 33
33 is odd.
10. Write a C Program to Check Whether a Character is a Vowel or Consonant.
#include <stdio.h>
int main()
{
char c;
int lowercase, uppercase;
printf("Enter an alphabet: ");
scanf("%c", &c);
lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase || uppercase)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}
OUTPUT
Enter an alphabet: G
G is a consonant.
11. Write a 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;
}
OUTPUT
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
12. Write a C Program to Check Leap Year.
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 == 0)
{
if (year % 400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year);
}
else
printf("%d is not a leap year.", year);
return 0;
}
OUTPUT
Enter a year: 2012
2012 is a leap year.
13. Write a 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;
}
OUTPUT
Enter a character: *
* is not an alphabet
14. Write a C Program to Calculate the Sum of first ‘N’ Natural Numbers.
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
i = 1;
while (i <= n) {
sum += i;
++i;
}
printf("Sum = %d", sum);
return 0;
}
OUTPUT
Enter a positive integer: 100
Sum = 5050
15. Write a 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);
// shows error if the user enters a negative integer
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;
}
OUTPUT
Enter an integer: 10
Factorial of 10 = 3628800
16. Write a C Program to Generate Multiplication Table of a given number.
#include <stdio.h>
int main() {
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d n", n, i, n * i);
}
return 0;
}
OUTPUT
Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
17. Write a C Program to Display Fibonacci Sequence up to ‘n’ numbers.
#include <stdio.h>
int main() {
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
OUTPUT
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
18. Write a 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);
while (n != 0) {
n /= 10; // n = n/10
++count;
}
printf("Number of digits: %d", count);
}
OUTPUT
Enter an integer: 3452
Number of digits: 4
19. Write a C Program to Reverse a Number.
#include <stdio.h>
int main() {
int n, rev = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0) {
remainder = n % 10;
rev = rev * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}
OUTPUT
Enter an integer: 2345
Reversed number = 5432
20. Write a C Program to Check Whether a Number is Palindrome or Not.
#include <stdio.h>
int main() {
int n, reversedN = 0, remainder, originalN;
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;
// reversed integer is stored in reversedN
while (n != 0) {
remainder = n % 10;
reversedN = reversedN * 10 + remainder;
n /= 10;
}
// palindrome if orignalN and reversedN are equal
if (originalN == reversedN)
printf("%d is a palindrome.", originalN);
else
printf("%d is not a palindrome.", originalN);
return 0;
}
OUTPUT
Enter an integer: 1001
1001 is a palindrome.
21. Write a C Program to Check Whether a Number is Prime or Not.
#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) {
// condition for non-prime
if (n % i == 0) {
flag = 1;
break;
}
}
if (n == 1) {
printf("1 is neither prime nor composite.");
}
else {
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
OUTPUT
Enter a positive integer: 29
29 is a prime number.
22. Write a C Program to Check whether the given number is an Armstrong Number or
not.
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;
while (originalNum != 0) {
// remainder contains the last digit
remainder = originalNum % 10;
result += remainder * remainder * remainder;
// removing last digit from the orignal number
originalNum /= 10;
}
if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
OUTPUT
Enter a three-digit integer: 371
371 is an Armstrong number.
23. Write a C Program to Make a Simple Calculator Using switch...case.
#include <stdio.h>
int main() {
char operator;
double first, second;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);
switch (operator) {
case '+':
printf("%lf + %lf = %lf", first, second, first + second);
break;
case '-':
printf("%lf - %lf = %lf", first, second, first - second);
break;
case '*':
printf("%lf * %lf = %lf", first, second, first * second);
break;
case '/':
printf("%lf / %lf = %lf", first, second, first / second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
OUTPUT
Enter an operator (+, -, *,): *
Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8
24. Write a C Programming Code To Create Pyramid and Pattern.
#include<stdio.h>
int main() {
int i, space, rows, k=0;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (i=1; i<=rows; ++i,k=0) {
for (space=1; space<=rows-i; ++space)
{ printf(" "); }
while (k!=2*i-1) {
printf("* ");
++k;
}
printf("n");
}
return 0;
}
OUTPUT
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
25. Write a C program to reverse a Sentence Using Recursion.
#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence() {
char c;
scanf("%c", &c);
if (c != 'n') {
reverseSentence();
printf("%c", c);
}
}
OUTPUT
Enter a sentence: margorp emosewa
awesome program
26. Write a C Program to Display Prime Numbers Between Intervals Using
Function.
#include <stdio.h>
int checkPrimeNumber(int n);
int main()
{
int n1, n2, i, flag;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("Prime numbers between %d and %d are: ", n1, n2);
for (i = n1 + 1; i < n2; ++i)
{
flag = checkPrimeNumber(i);
if (flag == 1)
printf("%d ", i);
}
return 0;
}
int checkPrimeNumber(int n)
{
int j, flag = 1;
for (j = 2; j <= n / 2; ++j)
{
if (n % j == 0)
{
flag = 0;
break;
}
}
return flag;
}
OUTPUT
Enter two positive integers: 12
30
Prime numbers between 12 and 30 are: 13 17 19 23 29
27. Write a C Program to Convert Binary Number to Decimal and vice-versa.
a) Program to convert binary to decimal
#include <math.h>
#include <stdio.h>
int convert(long long n);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n != 0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
OUTPUT
Enter a binary number: 110110111
110110111 in binary = 439
b) Program to convert decimal to binary
#include <math.h>
#include <stdio.h>
long long convert(int n);
int main()
{
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
printf("%d in decimal = %lld in binary", n, convert(n));
return 0;
}
long long convert(int n)
{
long long bin = 0;
int rem, i = 1, step = 1;
while (n != 0) {
rem = n % 2;
printf("Step %d: %d/2, Remainder = %d, Quotient = %dn", step++, n, rem, n / 2);
n /= 2;
bin += rem * i;
i *= 10;
}
return bin;
}
OUTPUT
Enter a decimal number: 19
Step 1: 19/2, Remainder = 1, Quotient = 9
Step 2: 9/2, Remainder = 1, Quotient = 4
Step 3: 4/2, Remainder = 0, Quotient = 2
Step 4: 2/2, Remainder = 0, Quotient = 1
Step 5: 1/2, Remainder = 1, Quotient = 0
19 in decimal = 10011 in binary
28. Write a C Program to Check Prime or Armstrong Number Using User-defined
Function.
#include <math.h>
#include <stdio.h>
int checkPrimeNumber(int n);
int checkArmstrongNumber(int n);
int main()
{
int n, flag;
printf("Enter a positive integer: ");
scanf("%d", &n);
flag = checkPrimeNumber(n);
if (flag == 1)
printf("%d is a prime number.n", n);
else
printf("%d is not a prime number.n", n);
flag = checkArmstrongNumber(n);
if (flag == 1)
printf("%d is an Armstrong number.", n);
else
printf("%d is not an Armstrong number.", n);
return 0;
}
int checkPrimeNumber(int n)
{
int i, flag = 1;
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0)
{
flag = 0;
break;
}
}
return flag;
}
int checkArmstrongNumber(int num)
{
int original, rem, n = 0, flag;
double result = 0.0;
original = num;
while (original != 0)
{
original /= 10;
++n;
}
original = num;
while (original != 0)
{
rem = original % 10;
result += pow(rem, n);
original /= 10;
}
if (round(result) == num)
flag = 1;
else
flag = 0;
return flag;
}
OUTPUT
Enter a positive integer: 407
407 is not a prime number.
407 is an Armstrong number.
29. Write a C program to calculate the power using recursion.
#include <stdio.h>
int power(int n1, int n2);
int main()
{
int base, a, result;
printf("Enter base number: ");
scanf("%d", &base);
printf("Enter power number(positive integer): ");
scanf("%d", &a);
result = power(base, a);
printf("%d^%d = %d", base, a, result);
return 0;
}
int power(int base, int a) {
if (a != 0)
return (base * power(base, a - 1));
else
return 1;
}
OUTPUT
Enter base number: 3
Enter power number(positive integer): 4
3^4 = 81
30. Write a C Program to Find G.C.D Using Recursion.
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
int hcf(int n1, int n2) {
if (n2 != 0)
return hcf(n2, n1 % n2);
else
return n1;
}
OUTPUT
Enter two positive integers: 366
60
G.C.D of 366 and 60 is 6.
31. Write a C Program to Calculate Average Using Arrays.
#include <stdio.h>
int main() {
int n, i;
float num[100], sum = 0.0, avg;
printf("Enter the numbers of elements: ");
scanf("%d", &n);
while (n > 100 || n < 1)
{
printf("Error! number should in range of (1 to 100).n");
printf("Enter the number again: ");
scanf("%d", &n);
}
for (i = 0; i < n; ++i)
{
printf("%d. Enter number: ", i + 1);
scanf("%f", &num[i]);
sum += num[i];
}
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
OUTPUT
Enter the numbers of elements: 6
1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69
32. Write a C Program to Find Largest Element in an Array
#include <stdio.h>
int main() {
int i, n;
float arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
for (i = 0; i < n; ++i) {
printf("Enter number%d: ", i + 1);
scanf("%f", &arr[i]);
}
// storing the largest number to arr[0]
for (i = 1; i < n; ++i) {
if (arr[0] < arr[i])
arr[0] = arr[i];
}
printf("Largest element = %.2f", arr[0]);
return 0;
}
OUTPUT
Enter the number of elements (1 to 100): 5
Enter number1: 34.5
Enter number2: 2.4
Enter number3: -35.5
Enter number4: 38.7
Enter number5: 24.5
Largest element = 38.70
33. Write a C Program to Add Two Matrices Using Multi-dimensional Arrays.
#include <stdio.h>
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("nEnter elements of 1st matrix:n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix:n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}
printf("nSum of two matrices: n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("nn");
}
}
return 0;
}
OUTPUT
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3
Enter elements of 1st matrix:
Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element a11: -4
Enter element a12: 5
Enter element a13: 3
Enter element a21: 5
Enter element a22: 6
Enter element a23: 3
Sum of two matrices:
-2 8 7
10 8 6
34. Write a C Program to Find the Length of a String.
#include <stdio.h>
int main()
{
char s[20];
printf (“Enter any string”);
scanf(“%s”, s);
for (int i = 0; s[i] != '0'; ++i);
{
printf("Length of the string: %d", i);
}
return 0;
}
OUTPUT
Enter any String
RG Kedia College
Length of the string: 16
35. Write a C Program to Concatenate Two Strings.
#include <stdio.h>
int main()
{
char s1[100] = "RG KEDIA", s2[] = "COLLEGE OF COMMERECE";
int i, j;
// length of s1 is stored in i
for (i = 0; s1[i] != '0'; ++i)
{
printf("i = %dn", i);
}
// concatenating each character of s2 to s1
for (j = 0; s2[j] != '0'; ++j, ++i) {
s1[i] = s2[j];
}
s1[i] = '0';
printf("After concatenation: ");
puts(s1);
return 0;
}
OUTPUT
After concatenation: RG KEDIA COLLEGE OF COMMERECE
36. Write a C Program to Copy String Without Using strcpy().
#include <stdio.h>
int main() {
char s1[100], s2[100], i;
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);
for (i = 0; s1[i] != '0'; ++i) {
s2[i] = s1[i];
}
s2[i] = '0';
printf("String s2: %s", s2);
return 0;
}
OUTPUT
Enter string s1: Hey fellow programmer.
String s2: Hey fellow programmer.
37. Write a C Program to Count the Number of Vowels, Consonants and so on.
#include <stdio.h>
int main() {
char line[150];
int vowels, consonant, digit, space;
vowels = consonant = digit = space = 0;
printf("Enter a line of string: ");
fgets(line, sizeof(line), stdin);
for (int i = 0; line[i] != '0'; ++i) {
if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' ||
line[i] == 'o' || line[i] == 'u' || line[i] == 'A' ||
line[i] == 'E' || line[i] == 'I' || line[i] == 'O' ||
line[i] == 'U') {
++vowels;
} else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) {
++consonant;
} else if (line[i] >= '0' && line[i] <= '9') {
++digit;
} else if (line[i] == ' ') {
++space;
}
}
printf("Vowels: %d", vowels);
printf("nConsonants: %d", consonant);
printf("nDigits: %d", digit);
printf("nWhite spaces: %d", space);
return 0;
}
OUTPUT
Enter a line of string: adfslkj34 34lkj343 34lk
Vowels: 1
Consonants: 11
Digits: 9
White spaces: 2
38. Write a C Program to Find the Frequency of Characters in a String.
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for (int i = 0; str[i] != '0'; ++i) {
if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
OUTPUT
Enter a string: This website is awesome.
Enter a character to find its frequency: e
Frequency of e = 4
39. Write a C Program to Access Array Elements Using Pointers.
#include <stdio.h>
int main() {
int data[5];
printf("Enter elements: ");
for (int i = 0; i < 5; ++i)
scanf("%d", data + i);
printf("You entered: n");
for (int i = 0; i < 5; ++i)
printf("%dn", *(data + i));
return 0;
}
OUTPUT
Enter elements: 1
2
3
5
4
You entered:
1
2
3
5
4
40. Write a C program to create, initialize, assign and access a pointer variable.
#include <stdio.h>
int main()
{
int num;
int *pNum;
pNum=& num;
num=100;
printf("Using variable num:n");
printf("value of num: %dnaddress of num: %un",num,&num);
printf("Using pointer variable:n");
printf("value of num: %dnaddress of num: %un",*pNum,pNum);
return 0;
}
OUTPUT
Using variable num:
value of num: 100
address of num: 2764564284
Using pointer variable:
value of num: 100
address of num: 2764564284
41. Write a C program to swap two numbers using pointers
#include <stdio.h>
int main()
{
int x, y, *a, *b, temp;
printf("Enter the value of x and yn");
scanf("%d%d", &x, &y);
printf("Before Swappingnx = %dny = %dn", x, y);
a = &x;
b = &y;
temp = *b;
*b = *a;
*a = temp;
printf("After Swappingnx = %dny = %dn", x, y);
return 0;
}
OUTPUT:
Enter the value of x and y
20 30
Before Swapping
x = 20
y = 30
After Swapping
x = 30
y = 20
42. Write a C program to count vowels and consonants in a string using pointers.
#include <stdio.h>
int main()
{
char str[100];
char *p;
int vCount=0,cCount=0;
printf("Enter any string: ");
fgets(str, 100, stdin);
p=str;
while(*p!='0')
{
if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U'
||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u')
vCount++;
else
cCount++;
p++;
}
printf("Number of Vowels in String: %dn",vCount);
printf("Number of Consonants in String: %d",cCount);
return 0;
}
OUTPUT
Enter any string: RGKEDIACOLLEGE
Number of Vowels in String: 5
Number of Consonants in String:9
43. Write a C Program to Store Information of a Student Using Structure.
#include <stdio.h>
struct student
{
char name[50];
int roll;
float marks;
} s;
int main()
{
printf("Enter information:n");
printf("Enter name: ");
fgets(s.name, sizeof(s.name), stdin);
printf("Enter roll number: ");
scanf("%d", &s.roll);
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Displaying Information:n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %dn", s.roll);
printf("Marks: %.1fn", s.marks);
return 0;
}
OUTPUT
Enter information:
Enter name: Jack
Enter roll number: 23
Enter marks: 34.5
Displaying Information:
Name: Jack
Roll number: 23
Marks: 34.5
44. Write a C Program to Add Two Distances (in inch-feet system) using Structures.
#include<stdio.h>
struct Distance {
int feet;
float inch;
} d1, d2, sumOfDistances;
int main()
{
printf("Enter information for 1st distancen");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
printf("nEnter information for 2nd distancen");
printf("Enter feet: ");
scanf("%d", &d2.feet);
printf("Enter inch: ");
scanf("%f", &d2.inch);
sumOfDistances.feet=d1.feet+d2.feet;
sumOfDistances.inch=d1.inch+d2.inch;
if (sumOfDistances.inch>12.0)
{
sumOfDistances.inch = sumOfDistances.inch-12.0;
++sumOfDistances.feet;
}
printf("nSum of distances = %d'-%.1f"", sumOfDistances.feet, sumOfDistances.inch);
return 0;
}
OUTPUT
Enter information for 1st distance
Enter feet: 23
Enter inch: 8.6
Enter information for 2nd distance
Enter feet: 34
Enter inch: 2.4
Sum of distances = 57'-11.0"
45. Write a C Program to Store Information of Students Using Structure.
#include <stdio.h>
struct student
{
char firstName[50];
int roll;
float marks;
} s[10];
int main()
{
int i;
printf("Enter information of students:n");
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("nFor roll number%d,n", s[i].roll);
printf("Enter first name: ");
scanf("%s", s[i].firstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:nn");
for (i = 0; i < 5; ++i) {
printf("nRoll number: %dn", i + 1);
printf("First name: ");
puts(s[i].firstName);
printf("Marks: %.1f", s[i].marks);
printf("n");
}
return 0;
}
OUTPUT
Enter information of students:
For roll number1,
Enter name: Tom
Enter marks: 98
For roll number2,
Enter name: Jerry
Enter marks: 89
.
.
.
Displaying Information:
Roll number: 1
Name: Tom
Marks: 98
.
.
.
46. Write a C program to declare, initialize an union.
#include <stdio.h>
union pack
{
char a;
int b;
double c;
};
int main()
{
pack p;
printf("nOccupied size by union pack: %d",sizeof(pack));
p.a='A';
printf("nValue of a:%c",p.a);
p.b=10;
printf("nValue of b:%d",p.b);
p.c=12345.6790;
printf("nValue of c:%f",p.c);
p.a='A';
p.b=10;
p.c=12345.6790;
printf("nValue of a:%c, b:%d, c:%f",p.a,p.b,p.c);
return 0;
}
OUTPUT
Occupied size by union pack: 8
Value of a:A
Value of b:10
Value of c:12345.679000
Value of a:�, b:-377957122, c:12345.679000
47. Write a C++ program to implement function overloading.
#include <iostream>
void display(int);
void display(float);
void display(int, float);
int main()
{
int a = 5;
float b = 5.5;
display(a);
display(b);
display(a, b);
return 0;
}
void display(int var) {
cout << "Integer number: " << var << endl;
}
void display(float var) {
cout << "Float number: " << var << endl;
}
void display(int var1, float var2) {
cout << "Integer number: " << var1;
cout << " and float number:" << var2;
}
OUTPUT
Integer number: 5
Float number: 5.5
Integer number: 5 and float number: 5.5
48. Write a C++ program to calculate an area of rectangle using encapsulation.
#include <iostream>
int main()
{
int width, lngth, area, peri;
cout << "nn Find the Area and Perimeter of a Rectangle :n";
cout << "-------------------------------------------------n";
cout<<" Input the length of the rectangle : ";
cin>>lngth;
cout<<" Input the width of the rectangle : ";
cin>>width;
area=(lngth*width);
peri=2*(lngth+width);
cout<<" The area of the rectangle is : "<< area << endl;
cout<<" The perimeter of the rectangle is : "<< peri << endl;
cout << endl;
return 0;
}
OUTPUT
Find the Area and Perimeter of a Rectangle :
-------------------------------------------------
Input the length of the rectangle : 10
Input the width of the rectangle : 15
The area of the rectangle is : 150
The perimeter of the rectangle is : 50
49. Write a C++ program to add two numbers using data abstraction.
#include <iostream>
class Sum
{
private: int x, y, z; // private variables
public:
void add()
{
cout<<"Enter two numbers: ";
cin>>x>>y;
z= x+y;
cout<<"Sum of two number is: "<<z<<endl;
}
};
int main()
{
Sum sm;
sm.add();
return 0;
}
OUTPUT
Enter two numbers:
3
6
Sum of two number is: 9
50. Write a C++ program to overload binary operators.
#include <iostream>
using namespace std;
class Test
{
private:
int count;
public:
Test(): count(5){}
void operator ++()
{
count = count+1;
}
void Display() { cout<<"Count: "<<count; }
};
int main()
{
Test t;
// this calls "function void operator ++()" function
++t;
t.Display();
return 0;
}
OUTPUT
Count: 6
Ad

Recommended

DOCX
Practical write a c program to reverse a given number
Mainak Sasmal
 
DOCX
Practical write a c program to reverse a given number
Mainak Sasmal
 
DOC
C program to check leap year
mohdshanu
 
DOCX
C programs
Minu S
 
PDF
88 c-programs
Leandro Schenone
 
DOCX
Core programming in c
Rahul Pandit
 
DOCX
C Programming
Sumant Diwakar
 
PDF
Common problems solving using c
ArghodeepPaul
 
PDF
The solution manual of c by robin
Abdullah Al Naser
 
DOC
C lab-programs
Tony Kurishingal
 
PPTX
C Programming Example
University of Potsdam
 
PDF
C Programming Example
PRATHAMESH DESHPANDE
 
DOCX
Cs291 assignment solution
Kuntal Bhowmick
 
DOCX
C Programming
Sumant Diwakar
 
DOCX
C programming Lab 2
Zaibi Gondal
 
PDF
Progr3
SANTOSH RATH
 
PPTX
Simple c program
Ravi Singh
 
DOCX
Practical File of C Language
RAJWANT KAUR
 
DOC
Basic c programs updated on 31.8.2020
vrgokila
 
PDF
Programming in C Lab
Neil Mathew
 
PDF
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
DR B.Surendiran .
 
DOC
Programming egs
Dr.Subha Krishna
 
DOCX
Program flowchart
Sowri Rajan
 
DOCX
C Language Programs
Mansi Tyagi
 
PPTX
Compiler design lab
ilias ahmed
 
PPT
All important c programby makhan kumbhkar
sandeep kumbhkar
 
PPTX
comp2
franzneri
 
DOCX
C lab
rajni kaushal
 
PDF
PCA-2 Programming and Solving 2nd Sem.pdf
Ashutoshprasad27
 

More Related Content

What's hot (20)

PDF
The solution manual of c by robin
Abdullah Al Naser
 
DOC
C lab-programs
Tony Kurishingal
 
PPTX
C Programming Example
University of Potsdam
 
PDF
C Programming Example
PRATHAMESH DESHPANDE
 
DOCX
Cs291 assignment solution
Kuntal Bhowmick
 
DOCX
C Programming
Sumant Diwakar
 
DOCX
C programming Lab 2
Zaibi Gondal
 
PDF
Progr3
SANTOSH RATH
 
PPTX
Simple c program
Ravi Singh
 
DOCX
Practical File of C Language
RAJWANT KAUR
 
DOC
Basic c programs updated on 31.8.2020
vrgokila
 
PDF
Programming in C Lab
Neil Mathew
 
PDF
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
DR B.Surendiran .
 
DOC
Programming egs
Dr.Subha Krishna
 
DOCX
Program flowchart
Sowri Rajan
 
DOCX
C Language Programs
Mansi Tyagi
 
PPTX
Compiler design lab
ilias ahmed
 
PPT
All important c programby makhan kumbhkar
sandeep kumbhkar
 
PPTX
comp2
franzneri
 
The solution manual of c by robin
Abdullah Al Naser
 
C lab-programs
Tony Kurishingal
 
C Programming Example
University of Potsdam
 
C Programming Example
PRATHAMESH DESHPANDE
 
Cs291 assignment solution
Kuntal Bhowmick
 
C Programming
Sumant Diwakar
 
C programming Lab 2
Zaibi Gondal
 
Progr3
SANTOSH RATH
 
Simple c program
Ravi Singh
 
Practical File of C Language
RAJWANT KAUR
 
Basic c programs updated on 31.8.2020
vrgokila
 
Programming in C Lab
Neil Mathew
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
DR B.Surendiran .
 
Programming egs
Dr.Subha Krishna
 
Program flowchart
Sowri Rajan
 
C Language Programs
Mansi Tyagi
 
Compiler design lab
ilias ahmed
 
All important c programby makhan kumbhkar
sandeep kumbhkar
 
comp2
franzneri
 

Similar to B.Com 1year Lab programs (20)

DOCX
C lab
rajni kaushal
 
PDF
PCA-2 Programming and Solving 2nd Sem.pdf
Ashutoshprasad27
 
DOCX
PCA-2 Programming and Solving 2nd Sem.docx
Ashutoshprasad27
 
DOCX
Programming fundamentals
Zaibi Gondal
 
DOCX
C file
simarsimmygrewal
 
DOCX
In C Programming create a program that converts a number from decimal.docx
tristans3
 
PDF
C programs
Vikram Nandini
 
PPTX
Looping programs that runs based on conditions
Chithra720576
 
DOCX
SaraPIC
Sara Sahu
 
DOCX
Best C Programming Solution
yogini sharma
 
PPTX
Implemintation of looping programs......
anjanasharma77573
 
DOCX
Itp practical file_1-year
AMIT SINGH
 
DOCX
Hargun
Mukund Trivedi
 
PPTX
Najmul
Najmul Ashik
 
PPTX
C Language Programming Introduction Lecture
myinstalab
 
PDF
Numerical analysis
Vishal Singh
 
DOCX
Chapter 1 Programming Fundamentals Assignment.docx
Shamshad
 
PDF
C faq pdf
DebiPanda
 
DOCX
Chapter 8 c solution
Azhar Javed
 
PCA-2 Programming and Solving 2nd Sem.pdf
Ashutoshprasad27
 
PCA-2 Programming and Solving 2nd Sem.docx
Ashutoshprasad27
 
Programming fundamentals
Zaibi Gondal
 
In C Programming create a program that converts a number from decimal.docx
tristans3
 
C programs
Vikram Nandini
 
Looping programs that runs based on conditions
Chithra720576
 
SaraPIC
Sara Sahu
 
Best C Programming Solution
yogini sharma
 
Implemintation of looping programs......
anjanasharma77573
 
Itp practical file_1-year
AMIT SINGH
 
Najmul
Najmul Ashik
 
C Language Programming Introduction Lecture
myinstalab
 
Numerical analysis
Vishal Singh
 
Chapter 1 Programming Fundamentals Assignment.docx
Shamshad
 
C faq pdf
DebiPanda
 
Chapter 8 c solution
Azhar Javed
 
Ad

More from Prasadu Peddi (17)

PDF
Pointers
Prasadu Peddi
 
PDF
String notes
Prasadu Peddi
 
DOCX
COMPUTING SEMANTIC SIMILARITY OF CONCEPTS IN KNOWLEDGE GRAPHS
Prasadu Peddi
 
DOCX
Energy-efficient Query Processing in Web Search Engines
Prasadu Peddi
 
DOCX
MINING COMPETITORS FROM LARGE UNSTRUCTURED DATASETS
Prasadu Peddi
 
DOCX
GENERATING QUERY FACETS USING KNOWLEDGE BASES
Prasadu Peddi
 
DOCX
UNDERSTAND SHORTTEXTS BY HARVESTING & ANALYZING SEMANTIKNOWLEDGE
Prasadu Peddi
 
DOCX
SOCIRANK: IDENTIFYING AND RANKING PREVALENT NEWS TOPICS USING SOCIAL MEDIA FA...
Prasadu Peddi
 
DOCX
QUERY EXPANSION WITH ENRICHED USER PROFILES FOR PERSONALIZED SEARCH UTILIZING...
Prasadu Peddi
 
DOCX
COLLABORATIVE FILTERING-BASED RECOMMENDATION OF ONLINE SOCIAL VOTING
Prasadu Peddi
 
DOCX
DYNAMIC FACET ORDERING FOR FACETED PRODUCT SEARCH ENGINES
Prasadu Peddi
 
PPTX
A Cross Tenant Access Control (CTAC) Model for Cloud Computing: Formal Specif...
Prasadu Peddi
 
PPTX
Time and Attribute Factors Combined Access Control on Time-Sensitive Data in ...
Prasadu Peddi
 
PPTX
Attribute Based Storage Supporting Secure Deduplication of Encrypted D...
Prasadu Peddi
 
PPTX
RAAC: Robust and Auditable Access Control with Multiple Attribute Authorities...
Prasadu Peddi
 
PPTX
Provably Secure Key-Aggregate Cryptosystems with Broadcast Aggregate Keys for...
Prasadu Peddi
 
PPTX
Identity-Based Remote Data Integrity Checking With Perfect Data Privacy Prese...
Prasadu Peddi
 
Pointers
Prasadu Peddi
 
String notes
Prasadu Peddi
 
COMPUTING SEMANTIC SIMILARITY OF CONCEPTS IN KNOWLEDGE GRAPHS
Prasadu Peddi
 
Energy-efficient Query Processing in Web Search Engines
Prasadu Peddi
 
MINING COMPETITORS FROM LARGE UNSTRUCTURED DATASETS
Prasadu Peddi
 
GENERATING QUERY FACETS USING KNOWLEDGE BASES
Prasadu Peddi
 
UNDERSTAND SHORTTEXTS BY HARVESTING & ANALYZING SEMANTIKNOWLEDGE
Prasadu Peddi
 
SOCIRANK: IDENTIFYING AND RANKING PREVALENT NEWS TOPICS USING SOCIAL MEDIA FA...
Prasadu Peddi
 
QUERY EXPANSION WITH ENRICHED USER PROFILES FOR PERSONALIZED SEARCH UTILIZING...
Prasadu Peddi
 
COLLABORATIVE FILTERING-BASED RECOMMENDATION OF ONLINE SOCIAL VOTING
Prasadu Peddi
 
DYNAMIC FACET ORDERING FOR FACETED PRODUCT SEARCH ENGINES
Prasadu Peddi
 
A Cross Tenant Access Control (CTAC) Model for Cloud Computing: Formal Specif...
Prasadu Peddi
 
Time and Attribute Factors Combined Access Control on Time-Sensitive Data in ...
Prasadu Peddi
 
Attribute Based Storage Supporting Secure Deduplication of Encrypted D...
Prasadu Peddi
 
RAAC: Robust and Auditable Access Control with Multiple Attribute Authorities...
Prasadu Peddi
 
Provably Secure Key-Aggregate Cryptosystems with Broadcast Aggregate Keys for...
Prasadu Peddi
 
Identity-Based Remote Data Integrity Checking With Perfect Data Privacy Prese...
Prasadu Peddi
 
Ad

Recently uploaded (20)

PPTX
How to Customize Quotation Layouts in Odoo 18
Celine George
 
PDF
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
PPTX
How to use search fetch method in Odoo 18
Celine George
 
PPTX
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
PDF
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
PDF
Hurricane Helene Application Documents Checklists
Mebane Rash
 
PPTX
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
PPTX
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
PDF
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
PPTX
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
PPTX
Peer Teaching Observations During School Internship
AjayaMohanty7
 
PPTX
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
PDF
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
PPTX
How payment terms are configured in Odoo 18
Celine George
 
PPTX
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
PPTX
Photo chemistry Power Point Presentation
mprpgcwa2024
 
PDF
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
PPTX
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
PPTX
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
PDF
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 
How to Customize Quotation Layouts in Odoo 18
Celine George
 
Romanticism in Love and Sacrifice An Analysis of Oscar Wilde’s The Nightingal...
KaryanaTantri21
 
How to use search fetch method in Odoo 18
Celine George
 
F-BLOCK ELEMENTS POWER POINT PRESENTATIONS
mprpgcwa2024
 
HistoPathology Ppt. Arshita Gupta for Diploma
arshitagupta674
 
Hurricane Helene Application Documents Checklists
Mebane Rash
 
INDUCTIVE EFFECT slide for first prof pharamacy students
SHABNAM FAIZ
 
2025 Completing the Pre-SET Plan Form.pptx
mansk2
 
LDMMIA Yoga S10 Free Workshop Grad Level
LDM & Mia eStudios
 
CRYPTO TRADING COURSE BY FINANCEWORLD.IO
AndrewBorisenko3
 
Peer Teaching Observations During School Internship
AjayaMohanty7
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 6-14-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Public Health For The 21st Century 1st Edition Judy Orme Jane Powell
trjnesjnqg7801
 
How payment terms are configured in Odoo 18
Celine George
 
How to Manage Different Customer Addresses in Odoo 18 Accounting
Celine George
 
Photo chemistry Power Point Presentation
mprpgcwa2024
 
ECONOMICS, DISASTER MANAGEMENT, ROAD SAFETY - STUDY MATERIAL [10TH]
SHERAZ AHMAD LONE
 
OBSESSIVE COMPULSIVE DISORDER.pptx IN 5TH SEMESTER B.SC NURSING, 2ND YEAR GNM...
parmarjuli1412
 
NSUMD_M1 Library Orientation_June 11, 2025.pptx
Julie Sarpy
 
THE PSYCHOANALYTIC OF THE BLACK CAT BY EDGAR ALLAN POE (1).pdf
nabilahk908
 

B.Com 1year Lab programs

  • 1. 1. Write a C Program to Add Two Integers #include <stdio.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; } OUTPUT Enter two integers: 12 11 12 + 11 = 23
  • 2. 2. Write a C Program to Print an Integer (Entered by the User) #include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); printf("You entered: %d", number); return 0; } OUTPUT Enter an integer: 25 You entered: 25
  • 3. 3. Write a C Program to Multiply Two Floating-Point Numbers #include <stdio.h> int main() { float num1, num2, product; printf("Enter first Number: "); scanf("%f", &num1); printf("Enter second Number: "); scanf("%f", &num2); product = num1 * num2; printf ("Product of entered numbers is: %.3f", product); return 0; } OUTPUT Enter first Number: 12.761 Enter second Number: 89.23 Product of entered numbers is:1138.664
  • 4. 4. Write a 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; } OUTPUT Enter a character: G ASCII value of G = 71
  • 5. 5. Write a C Program to Compute Quotient and Remainder. #include <stdio.h> int main() { int dividend, divisor; int quotient, remainder; printf("Enter dividend: "); scanf("%d",&dividend); printf("Enter divisor: "); scanf("%d",&divisor); // Computes quotient quotient= dividend/divisor; // Computes remainder remainder= dividend%divisor; printf("quotient: %d, remainder: %dn",quotient,remainder); return 0; } OUTPUT Enter dividend: 25 Enter divisor: 4 Quotient = 6 Remainder = 1
  • 6. 6. Write a 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; printf("Size of int: %ld bytesn", sizeof(intType)); printf("Size of float: %ld bytesn", sizeof(floatType)); printf("Size of double: %ld bytesn", sizeof(doubleType)); printf("Size of char: %ld byten", sizeof(charType)); return 0; } OUTPUT Size of int: 4 bytes Size of float: 4 bytes Size of double: 8 bytes Size of char: 1 byte
  • 7. 7. Write a C Program to Swap Two Numbers Using Temporary Variable. #include<stdio.h> int main() { double first, second, temp; printf("Enter first number: "); scanf("%lf", &first); printf("Enter second number: "); scanf("%lf", &second); temp = first; first = second; second = temp; printf("nAfter swapping, firstNumber = %.2lfn", first); printf("After swapping, secondNumber = %.2lf", second); return 0; } OUTPUT Enter first number: 1.20 Enter second number: 2.45 After swapping, firstNumber = 2.45 After swapping, secondNumber = 1.20
  • 8. 8. Write a 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; } OUTPUT Enter an integer: 7 7 is odd.
  • 9. 9. Write a C Program to Check Odd or Even Using the Ternary Operator. #include <stdio.h> int main() { int num; printf("Enter an integer: "); scanf("%d", &num); (num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.", num); return 0; } OUTPUT Enter an integer: 33 33 is odd.
  • 10. 10. Write a C Program to Check Whether a Character is a Vowel or Consonant. #include <stdio.h> int main() { char c; int lowercase, uppercase; printf("Enter an alphabet: "); scanf("%c", &c); lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); if (lowercase || uppercase) printf("%c is a vowel.", c); else printf("%c is a consonant.", c); return 0; } OUTPUT Enter an alphabet: G G is a consonant.
  • 11. 11. Write a 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; } OUTPUT Enter three numbers: -4.5 3.9 5.6 5.60 is the largest number.
  • 12. 12. Write a C Program to Check Leap Year. #include <stdio.h> int main() { int year; printf("Enter a year: "); scanf("%d", &year); if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) printf("%d is a leap year.", year); else printf("%d is not a leap year.", year); } else printf("%d is a leap year.", year); } else printf("%d is not a leap year.", year); return 0; } OUTPUT Enter a year: 2012 2012 is a leap year.
  • 13. 13. Write a 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; } OUTPUT Enter a character: * * is not an alphabet
  • 14. 14. Write a C Program to Calculate the Sum of first ‘N’ Natural Numbers. #include <stdio.h> int main() { int n, i, sum = 0; printf("Enter a positive integer: "); scanf("%d", &n); i = 1; while (i <= n) { sum += i; ++i; } printf("Sum = %d", sum); return 0; } OUTPUT Enter a positive integer: 100 Sum = 5050
  • 15. 15. Write a 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); // shows error if the user enters a negative integer 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; } OUTPUT Enter an integer: 10 Factorial of 10 = 3628800
  • 16. 16. Write a C Program to Generate Multiplication Table of a given number. #include <stdio.h> int main() { int n, i; printf("Enter an integer: "); scanf("%d", &n); for (i = 1; i <= 10; ++i) { printf("%d * %d = %d n", n, i, n * i); } return 0; } OUTPUT Enter an integer: 9 9 * 1 = 9 9 * 2 = 18 9 * 3 = 27 9 * 4 = 36 9 * 5 = 45 9 * 6 = 54 9 * 7 = 63 9 * 8 = 72 9 * 9 = 81 9 * 10 = 90
  • 17. 17. Write a C Program to Display Fibonacci Sequence up to ‘n’ numbers. #include <stdio.h> int main() { int i, n, t1 = 0, t2 = 1, nextTerm; printf("Enter the number of terms: "); scanf("%d", &n); printf("Fibonacci Series: "); for (i = 1; i <= n; ++i) { printf("%d, ", t1); nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; } return 0; } OUTPUT Enter the number of terms: 10 Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
  • 18. 18. Write a 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); while (n != 0) { n /= 10; // n = n/10 ++count; } printf("Number of digits: %d", count); } OUTPUT Enter an integer: 3452 Number of digits: 4
  • 19. 19. Write a C Program to Reverse a Number. #include <stdio.h> int main() { int n, rev = 0, remainder; printf("Enter an integer: "); scanf("%d", &n); while (n != 0) { remainder = n % 10; rev = rev * 10 + remainder; n /= 10; } printf("Reversed number = %d", rev); return 0; } OUTPUT Enter an integer: 2345 Reversed number = 5432
  • 20. 20. Write a C Program to Check Whether a Number is Palindrome or Not. #include <stdio.h> int main() { int n, reversedN = 0, remainder, originalN; printf("Enter an integer: "); scanf("%d", &n); originalN = n; // reversed integer is stored in reversedN while (n != 0) { remainder = n % 10; reversedN = reversedN * 10 + remainder; n /= 10; } // palindrome if orignalN and reversedN are equal if (originalN == reversedN) printf("%d is a palindrome.", originalN); else printf("%d is not a palindrome.", originalN); return 0; } OUTPUT Enter an integer: 1001 1001 is a palindrome.
  • 21. 21. Write a C Program to Check Whether a Number is Prime or Not. #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) { // condition for non-prime if (n % i == 0) { flag = 1; break; } } if (n == 1) { printf("1 is neither prime nor composite."); } else { if (flag == 0) printf("%d is a prime number.", n); else printf("%d is not a prime number.", n); } return 0; }
  • 22. OUTPUT Enter a positive integer: 29 29 is a prime number.
  • 23. 22. Write a C Program to Check whether the given number is an Armstrong Number or not. #include <stdio.h> int main() { int num, originalNum, remainder, result = 0; printf("Enter a three-digit integer: "); scanf("%d", &num); originalNum = num; while (originalNum != 0) { // remainder contains the last digit remainder = originalNum % 10; result += remainder * remainder * remainder; // removing last digit from the orignal number originalNum /= 10; } if (result == num) printf("%d is an Armstrong number.", num); else printf("%d is not an Armstrong number.", num); return 0; }
  • 24. OUTPUT Enter a three-digit integer: 371 371 is an Armstrong number.
  • 25. 23. Write a C Program to Make a Simple Calculator Using switch...case. #include <stdio.h> int main() { char operator; double first, second; printf("Enter an operator (+, -, *,): "); scanf("%c", &operator); printf("Enter two operands: "); scanf("%lf %lf", &first, &second); switch (operator) { case '+': printf("%lf + %lf = %lf", first, second, first + second); break; case '-': printf("%lf - %lf = %lf", first, second, first - second); break; case '*': printf("%lf * %lf = %lf", first, second, first * second); break; case '/': printf("%lf / %lf = %lf", first, second, first / second); break; // operator doesn't match any case constant default: printf("Error! operator is not correct"); }
  • 26. return 0; } OUTPUT Enter an operator (+, -, *,): * Enter two operands: 1.5 4.5 1.5 * 4.5 = 6.8
  • 27. 24. Write a C Programming Code To Create Pyramid and Pattern. #include<stdio.h> int main() { int i, space, rows, k=0; printf("Enter number of rows: "); scanf("%d", &rows); for (i=1; i<=rows; ++i,k=0) { for (space=1; space<=rows-i; ++space) { printf(" "); } while (k!=2*i-1) { printf("* "); ++k; } printf("n"); } return 0; } OUTPUT * * * * * * * * * * * * * * * * * * * * * * * * *
  • 28. 25. Write a C program to reverse a Sentence Using Recursion. #include <stdio.h> void reverseSentence(); int main() { printf("Enter a sentence: "); reverseSentence(); return 0; } void reverseSentence() { char c; scanf("%c", &c); if (c != 'n') { reverseSentence(); printf("%c", c); } } OUTPUT Enter a sentence: margorp emosewa awesome program
  • 29. 26. Write a C Program to Display Prime Numbers Between Intervals Using Function. #include <stdio.h> int checkPrimeNumber(int n); int main() { int n1, n2, i, flag; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); printf("Prime numbers between %d and %d are: ", n1, n2); for (i = n1 + 1; i < n2; ++i) { flag = checkPrimeNumber(i); if (flag == 1) printf("%d ", i); } return 0; } int checkPrimeNumber(int n) { int j, flag = 1; for (j = 2; j <= n / 2; ++j) { if (n % j == 0) { flag = 0; break;
  • 30. } } return flag; } OUTPUT Enter two positive integers: 12 30 Prime numbers between 12 and 30 are: 13 17 19 23 29
  • 31. 27. Write a C Program to Convert Binary Number to Decimal and vice-versa. a) Program to convert binary to decimal #include <math.h> #include <stdio.h> int convert(long long n); int main() { long long n; printf("Enter a binary number: "); scanf("%lld", &n); printf("%lld in binary = %d in decimal", n, convert(n)); return 0; } int convert(long long n) { int dec = 0, i = 0, rem; while (n != 0) { rem = n % 10; n /= 10; dec += rem * pow(2, i); ++i; } return dec; } OUTPUT Enter a binary number: 110110111 110110111 in binary = 439
  • 32. b) Program to convert decimal to binary #include <math.h> #include <stdio.h> long long convert(int n); int main() { int n; printf("Enter a decimal number: "); scanf("%d", &n); printf("%d in decimal = %lld in binary", n, convert(n)); return 0; } long long convert(int n) { long long bin = 0; int rem, i = 1, step = 1; while (n != 0) { rem = n % 2; printf("Step %d: %d/2, Remainder = %d, Quotient = %dn", step++, n, rem, n / 2); n /= 2; bin += rem * i; i *= 10; } return bin; }
  • 33. OUTPUT Enter a decimal number: 19 Step 1: 19/2, Remainder = 1, Quotient = 9 Step 2: 9/2, Remainder = 1, Quotient = 4 Step 3: 4/2, Remainder = 0, Quotient = 2 Step 4: 2/2, Remainder = 0, Quotient = 1 Step 5: 1/2, Remainder = 1, Quotient = 0 19 in decimal = 10011 in binary
  • 34. 28. Write a C Program to Check Prime or Armstrong Number Using User-defined Function. #include <math.h> #include <stdio.h> int checkPrimeNumber(int n); int checkArmstrongNumber(int n); int main() { int n, flag; printf("Enter a positive integer: "); scanf("%d", &n); flag = checkPrimeNumber(n); if (flag == 1) printf("%d is a prime number.n", n); else printf("%d is not a prime number.n", n); flag = checkArmstrongNumber(n); if (flag == 1) printf("%d is an Armstrong number.", n); else printf("%d is not an Armstrong number.", n); return 0; } int checkPrimeNumber(int n) { int i, flag = 1; for (i = 2; i <= n / 2; ++i) {
  • 35. if (n % i == 0) { flag = 0; break; } } return flag; } int checkArmstrongNumber(int num) { int original, rem, n = 0, flag; double result = 0.0; original = num; while (original != 0) { original /= 10; ++n; } original = num; while (original != 0) { rem = original % 10; result += pow(rem, n); original /= 10; } if (round(result) == num) flag = 1; else flag = 0; return flag;
  • 36. } OUTPUT Enter a positive integer: 407 407 is not a prime number. 407 is an Armstrong number.
  • 37. 29. Write a C program to calculate the power using recursion. #include <stdio.h> int power(int n1, int n2); int main() { int base, a, result; printf("Enter base number: "); scanf("%d", &base); printf("Enter power number(positive integer): "); scanf("%d", &a); result = power(base, a); printf("%d^%d = %d", base, a, result); return 0; } int power(int base, int a) { if (a != 0) return (base * power(base, a - 1)); else return 1; } OUTPUT Enter base number: 3 Enter power number(positive integer): 4 3^4 = 81
  • 38. 30. Write a C Program to Find G.C.D Using Recursion. #include <stdio.h> int hcf(int n1, int n2); int main() { int n1, n2; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2)); return 0; } int hcf(int n1, int n2) { if (n2 != 0) return hcf(n2, n1 % n2); else return n1; } OUTPUT Enter two positive integers: 366 60 G.C.D of 366 and 60 is 6.
  • 39. 31. Write a C Program to Calculate Average Using Arrays. #include <stdio.h> int main() { int n, i; float num[100], sum = 0.0, avg; printf("Enter the numbers of elements: "); scanf("%d", &n); while (n > 100 || n < 1) { printf("Error! number should in range of (1 to 100).n"); printf("Enter the number again: "); scanf("%d", &n); } for (i = 0; i < n; ++i) { printf("%d. Enter number: ", i + 1); scanf("%f", &num[i]); sum += num[i]; } avg = sum / n; printf("Average = %.2f", avg); return 0; }
  • 40. OUTPUT Enter the numbers of elements: 6 1. Enter number: 45.3 2. Enter number: 67.5 3. Enter number: -45.6 4. Enter number: 20.34 5. Enter number: 33 6. Enter number: 45.6 Average = 27.69
  • 41. 32. Write a C Program to Find Largest Element in an Array #include <stdio.h> int main() { int i, n; float arr[100]; printf("Enter the number of elements (1 to 100): "); scanf("%d", &n); for (i = 0; i < n; ++i) { printf("Enter number%d: ", i + 1); scanf("%f", &arr[i]); } // storing the largest number to arr[0] for (i = 1; i < n; ++i) { if (arr[0] < arr[i]) arr[0] = arr[i]; } printf("Largest element = %.2f", arr[0]); return 0; }
  • 42. OUTPUT Enter the number of elements (1 to 100): 5 Enter number1: 34.5 Enter number2: 2.4 Enter number3: -35.5 Enter number4: 38.7 Enter number5: 24.5 Largest element = 38.70
  • 43. 33. Write a C Program to Add Two Matrices Using Multi-dimensional Arrays. #include <stdio.h> int main() { int r, c, a[100][100], b[100][100], sum[100][100], i, j; printf("Enter the number of rows (between 1 and 100): "); scanf("%d", &r); printf("Enter the number of columns (between 1 and 100): "); scanf("%d", &c); printf("nEnter elements of 1st matrix:n"); for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { printf("Enter element a%d%d: ", i + 1, j + 1); scanf("%d", &a[i][j]); } printf("Enter elements of 2nd matrix:n"); for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { printf("Enter element a%d%d: ", i + 1, j + 1); scanf("%d", &b[i][j]); } for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { sum[i][j] = a[i][j] + b[i][j]; }
  • 44. printf("nSum of two matrices: n"); for (i = 0; i < r; ++i) for (j = 0; j < c; ++j) { printf("%d ", sum[i][j]); if (j == c - 1) { printf("nn"); } } return 0; } OUTPUT Enter the number of rows (between 1 and 100): 2 Enter the number of columns (between 1 and 100): 3 Enter elements of 1st matrix: Enter element a11: 2 Enter element a12: 3 Enter element a13: 4 Enter element a21: 5 Enter element a22: 2 Enter element a23: 3 Enter elements of 2nd matrix: Enter element a11: -4 Enter element a12: 5 Enter element a13: 3 Enter element a21: 5 Enter element a22: 6 Enter element a23: 3
  • 45. Sum of two matrices: -2 8 7 10 8 6
  • 46. 34. Write a C Program to Find the Length of a String. #include <stdio.h> int main() { char s[20]; printf (“Enter any string”); scanf(“%s”, s); for (int i = 0; s[i] != '0'; ++i); { printf("Length of the string: %d", i); } return 0; } OUTPUT Enter any String RG Kedia College Length of the string: 16
  • 47. 35. Write a C Program to Concatenate Two Strings. #include <stdio.h> int main() { char s1[100] = "RG KEDIA", s2[] = "COLLEGE OF COMMERECE"; int i, j; // length of s1 is stored in i for (i = 0; s1[i] != '0'; ++i) { printf("i = %dn", i); } // concatenating each character of s2 to s1 for (j = 0; s2[j] != '0'; ++j, ++i) { s1[i] = s2[j]; } s1[i] = '0'; printf("After concatenation: "); puts(s1); return 0; } OUTPUT After concatenation: RG KEDIA COLLEGE OF COMMERECE
  • 48. 36. Write a C Program to Copy String Without Using strcpy(). #include <stdio.h> int main() { char s1[100], s2[100], i; printf("Enter string s1: "); fgets(s1, sizeof(s1), stdin); for (i = 0; s1[i] != '0'; ++i) { s2[i] = s1[i]; } s2[i] = '0'; printf("String s2: %s", s2); return 0; } OUTPUT Enter string s1: Hey fellow programmer. String s2: Hey fellow programmer.
  • 49. 37. Write a C Program to Count the Number of Vowels, Consonants and so on. #include <stdio.h> int main() { char line[150]; int vowels, consonant, digit, space; vowels = consonant = digit = space = 0; printf("Enter a line of string: "); fgets(line, sizeof(line), stdin); for (int i = 0; line[i] != '0'; ++i) { if (line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' || line[i] == 'u' || line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O' || line[i] == 'U') { ++vowels; } else if ((line[i] >= 'a' && line[i] <= 'z') || (line[i] >= 'A' && line[i] <= 'Z')) { ++consonant; } else if (line[i] >= '0' && line[i] <= '9') { ++digit; } else if (line[i] == ' ') { ++space; } } printf("Vowels: %d", vowels); printf("nConsonants: %d", consonant); printf("nDigits: %d", digit); printf("nWhite spaces: %d", space);
  • 50. return 0; } OUTPUT Enter a line of string: adfslkj34 34lkj343 34lk Vowels: 1 Consonants: 11 Digits: 9 White spaces: 2
  • 51. 38. Write a C Program to Find the Frequency of Characters in a String. #include <stdio.h> int main() { char str[1000], ch; int count = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin); printf("Enter a character to find its frequency: "); scanf("%c", &ch); for (int i = 0; str[i] != '0'; ++i) { if (ch == str[i]) ++count; } printf("Frequency of %c = %d", ch, count); return 0; } OUTPUT Enter a string: This website is awesome. Enter a character to find its frequency: e Frequency of e = 4
  • 52. 39. Write a C Program to Access Array Elements Using Pointers. #include <stdio.h> int main() { int data[5]; printf("Enter elements: "); for (int i = 0; i < 5; ++i) scanf("%d", data + i); printf("You entered: n"); for (int i = 0; i < 5; ++i) printf("%dn", *(data + i)); return 0; } OUTPUT Enter elements: 1 2 3 5 4 You entered: 1 2 3 5 4
  • 53. 40. Write a C program to create, initialize, assign and access a pointer variable. #include <stdio.h> int main() { int num; int *pNum; pNum=& num; num=100; printf("Using variable num:n"); printf("value of num: %dnaddress of num: %un",num,&num); printf("Using pointer variable:n"); printf("value of num: %dnaddress of num: %un",*pNum,pNum); return 0; } OUTPUT Using variable num: value of num: 100 address of num: 2764564284 Using pointer variable: value of num: 100 address of num: 2764564284
  • 54. 41. Write a C program to swap two numbers using pointers #include <stdio.h> int main() { int x, y, *a, *b, temp; printf("Enter the value of x and yn"); scanf("%d%d", &x, &y); printf("Before Swappingnx = %dny = %dn", x, y); a = &x; b = &y; temp = *b; *b = *a; *a = temp; printf("After Swappingnx = %dny = %dn", x, y); return 0; } OUTPUT: Enter the value of x and y 20 30 Before Swapping x = 20 y = 30 After Swapping x = 30 y = 20
  • 55. 42. Write a C program to count vowels and consonants in a string using pointers. #include <stdio.h> int main() { char str[100]; char *p; int vCount=0,cCount=0; printf("Enter any string: "); fgets(str, 100, stdin); p=str; while(*p!='0') { if(*p=='A' ||*p=='E' ||*p=='I' ||*p=='O' ||*p=='U' ||*p=='a' ||*p=='e' ||*p=='i' ||*p=='o' ||*p=='u') vCount++; else cCount++; p++; } printf("Number of Vowels in String: %dn",vCount); printf("Number of Consonants in String: %d",cCount); return 0; }
  • 56. OUTPUT Enter any string: RGKEDIACOLLEGE Number of Vowels in String: 5 Number of Consonants in String:9
  • 57. 43. Write a C Program to Store Information of a Student Using Structure. #include <stdio.h> struct student { char name[50]; int roll; float marks; } s; int main() { printf("Enter information:n"); printf("Enter name: "); fgets(s.name, sizeof(s.name), stdin); printf("Enter roll number: "); scanf("%d", &s.roll); printf("Enter marks: "); scanf("%f", &s.marks); printf("Displaying Information:n"); printf("Name: "); printf("%s", s.name); printf("Roll number: %dn", s.roll); printf("Marks: %.1fn", s.marks); return 0; }
  • 58. OUTPUT Enter information: Enter name: Jack Enter roll number: 23 Enter marks: 34.5 Displaying Information: Name: Jack Roll number: 23 Marks: 34.5
  • 59. 44. Write a C Program to Add Two Distances (in inch-feet system) using Structures. #include<stdio.h> struct Distance { int feet; float inch; } d1, d2, sumOfDistances; int main() { printf("Enter information for 1st distancen"); printf("Enter feet: "); scanf("%d", &d1.feet); printf("Enter inch: "); scanf("%f", &d1.inch); printf("nEnter information for 2nd distancen"); printf("Enter feet: "); scanf("%d", &d2.feet); printf("Enter inch: "); scanf("%f", &d2.inch); sumOfDistances.feet=d1.feet+d2.feet; sumOfDistances.inch=d1.inch+d2.inch; if (sumOfDistances.inch>12.0) { sumOfDistances.inch = sumOfDistances.inch-12.0; ++sumOfDistances.feet; } printf("nSum of distances = %d'-%.1f"", sumOfDistances.feet, sumOfDistances.inch);
  • 60. return 0; } OUTPUT Enter information for 1st distance Enter feet: 23 Enter inch: 8.6 Enter information for 2nd distance Enter feet: 34 Enter inch: 2.4 Sum of distances = 57'-11.0"
  • 61. 45. Write a C Program to Store Information of Students Using Structure. #include <stdio.h> struct student { char firstName[50]; int roll; float marks; } s[10]; int main() { int i; printf("Enter information of students:n"); for (i = 0; i < 5; ++i) { s[i].roll = i + 1; printf("nFor roll number%d,n", s[i].roll); printf("Enter first name: "); scanf("%s", s[i].firstName); printf("Enter marks: "); scanf("%f", &s[i].marks); } printf("Displaying Information:nn"); for (i = 0; i < 5; ++i) { printf("nRoll number: %dn", i + 1); printf("First name: "); puts(s[i].firstName); printf("Marks: %.1f", s[i].marks); printf("n"); }
  • 62. return 0; } OUTPUT Enter information of students: For roll number1, Enter name: Tom Enter marks: 98 For roll number2, Enter name: Jerry Enter marks: 89 . . . Displaying Information: Roll number: 1 Name: Tom Marks: 98 . . .
  • 63. 46. Write a C program to declare, initialize an union. #include <stdio.h> union pack { char a; int b; double c; }; int main() { pack p; printf("nOccupied size by union pack: %d",sizeof(pack)); p.a='A'; printf("nValue of a:%c",p.a); p.b=10; printf("nValue of b:%d",p.b); p.c=12345.6790; printf("nValue of c:%f",p.c); p.a='A'; p.b=10; p.c=12345.6790; printf("nValue of a:%c, b:%d, c:%f",p.a,p.b,p.c); return 0; }
  • 64. OUTPUT Occupied size by union pack: 8 Value of a:A Value of b:10 Value of c:12345.679000 Value of a:�, b:-377957122, c:12345.679000
  • 65. 47. Write a C++ program to implement function overloading. #include <iostream> void display(int); void display(float); void display(int, float); int main() { int a = 5; float b = 5.5; display(a); display(b); display(a, b); return 0; } void display(int var) { cout << "Integer number: " << var << endl; } void display(float var) { cout << "Float number: " << var << endl; }
  • 66. void display(int var1, float var2) { cout << "Integer number: " << var1; cout << " and float number:" << var2; } OUTPUT Integer number: 5 Float number: 5.5 Integer number: 5 and float number: 5.5
  • 67. 48. Write a C++ program to calculate an area of rectangle using encapsulation. #include <iostream> int main() { int width, lngth, area, peri; cout << "nn Find the Area and Perimeter of a Rectangle :n"; cout << "-------------------------------------------------n"; cout<<" Input the length of the rectangle : "; cin>>lngth; cout<<" Input the width of the rectangle : "; cin>>width; area=(lngth*width); peri=2*(lngth+width); cout<<" The area of the rectangle is : "<< area << endl; cout<<" The perimeter of the rectangle is : "<< peri << endl; cout << endl; return 0; } OUTPUT Find the Area and Perimeter of a Rectangle : ------------------------------------------------- Input the length of the rectangle : 10 Input the width of the rectangle : 15 The area of the rectangle is : 150 The perimeter of the rectangle is : 50
  • 68. 49. Write a C++ program to add two numbers using data abstraction. #include <iostream> class Sum { private: int x, y, z; // private variables public: void add() { cout<<"Enter two numbers: "; cin>>x>>y; z= x+y; cout<<"Sum of two number is: "<<z<<endl; } }; int main() { Sum sm; sm.add(); return 0; } OUTPUT Enter two numbers: 3 6 Sum of two number is: 9
  • 69. 50. Write a C++ program to overload binary operators. #include <iostream> using namespace std; class Test { private: int count; public: Test(): count(5){} void operator ++() { count = count+1; } void Display() { cout<<"Count: "<<count; } }; int main() { Test t; // this calls "function void operator ++()" function ++t; t.Display(); return 0; }