PRACTICAL RECORD OF PROGRAMMING WITH C & C++
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);
// calculating sum
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>
void main()
{
int number;
printf("Enter an integer: ");
// reads and stores input
scanf("%d", &number);
// displays output
printf("You entered: %d", number);
}
Output
Enter an integer: 25
You entered: 25
3. write a c program to multiply two floating point numbers
#include <stdio.h>
void main()
{
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
// Calculating product
product = a * b;
// Result up to 2 decimal point is displayed using %.2lf
printf("Product = %.2lf", product);
Output
Enter two numbers: 2.4
1.12
Product = 2.69
4. write a c program to find ascii value of a character
#include <stdio.h>
void main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
}
Output
Enter a character: G
ASCII value of G = 71
5. write a c program to compute quotient and remainder
#include <stdio.h>
void main()
{
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", 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>
void main()
{
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf("Size of int: %ld bytes\n", sizeof(intType));
printf("Size of float: %ld bytes\n", sizeof(floatType));
printf("Size of double: %ld bytes\n", sizeof(doubleType));
printf("Size of char: %ld byte\n", sizeof(charType));
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>
Void main()
{
double first, second, temp;
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// Value of first is assigned to temp
temp = first;
first = second;
second = temp;
printf("\nAfter swapping, firstNumber = %.2lf\n", first);
printf("After swapping, secondNumber = %.2lf", second);
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 the given number is odd or even
#include <stdio.h>
void 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);
}
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 >
void main()
{
int n;
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
printf("Enter an integer number\n");
scanf("%d", &n);
(n % 2 == 0) ?
(printf("%d is Even number\n", n)) :
(printf("%d is Odd number\n", n));
Output
Enter an integer number
2
2 is even number
10. write a c program to check whether a character is a vowel or consonant
#include <stdio.h>
void main()
{
char c;
int lowercase, uppercase;
printf("Enter an alphabet: ");
scanf("%c", &c);
// evaluates to 1 if variable c is lowercase
lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 if variable c is uppercase
uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// evaluates to 1 if c is either lowercase or uppercase
if (lowercase || uppercase)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
Output
Enter an alphabet: G
G is a consonant.
11. write a c program to find largest among three numbers
#include <stdio.h>
void main()
{
double n1, n2, n3;
printf("Enter three different numbers: ");
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
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);
}
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>
void main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 4 == 0) {
if (year % 100 == 0) {
// the year is a leap year if it is divisible by 400.
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);
Output 1
Enter a year: 1900
1900 is not a leap year.
13. write a c program to check whether a character is an alphabet or not
#include <stdio.h>
int main()
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
{
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);
for (i = 1; i <= n; ++i) {
sum += 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);
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
// 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 the 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()
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
{
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 given number
#include <stdio.h>
int main()
{
int n, rev = 0, remainder;
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
print f("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
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
#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 a 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 = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}
if (result == num)
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
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("%.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;
// 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
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
24. write a c programming code to create pyramid and pattern
#include<stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d", &rows);
for (i=1; i<=rows; ++i) {
for (j=1; j<=i; ++j)
{ printf("* "); }
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
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
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 will be equal to 1 if i is prime
flag = checkPrimeNumber(i);
if (flag == 1)
printf("%d ", i);
}
return 0;
}
// user-defined function to check prime number
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 number vice-versa
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: ");
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
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
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 = %d\n", 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
15 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
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);
// check prime number
flag = checkPrimeNumber(n);
if (flag == 1)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
// check Armstrong number
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) {
// condition for non-prime number
if (n % i == 0) {
flag = 0;
break;
}
}
return flag;
}
int checkArmstrongNumber(int num) {
int original, rem, result = 0, n = 0, flag;
original = num;
while (original != 0) {
original /= 10;
16 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
++n;
}
original = num;
while (original != 0) {
rem = original % 10 ;
result += pow(rem, n);
original /= 10;
}
// condition for Armstrong number
if (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. c program to find power of a number using recursive function
#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. c program to find GCD using recursion
17 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
#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;
}
18 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
Output
Enter the numbers of elements: 6
Enter number: 45.3
Enter number: 67.5
Enter number: -45.6
Enter number: 20.34
Enter number: 33
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>
19 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
int main()
{
int m, n, c, d, first[10][10], second[10][10], sum[10][10];
printf("Enter the number of rows and columns of matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for (c = 0; c < m; c++)
for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrix\n");
for (c = 0; c < m; c++)
for (d = 0 ; d < n; d++)
scanf("%d", &second[c][d]);
printf("Sum of entered matrices:-\n");
for (c = 0; c < m; c++) {
for (d = 0 ; d < n; d++) {
sum[c][d] = first[c][d] + second[c][d];
printf("%d\t", sum[c][d]);
}
printf("\n");
}
return 0;
}
Output
20 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
34. write a c program to find the length of a string
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
printf("Enter a string to calculate its length\n");
gets(a);
length = strlen(a);
printf("Length of the string = %d\n", length);
return 0;
}
Output
35. write a c program to concatenate two strings
21 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
#include <stdio.h>
#include <string.h>
int main()
{
char a[1000], b[1000];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a, b);
printf("String obtained on concatenation: %s\n", a);
return 0;
}
Output
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;
}
22 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
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>
23 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
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, freq);
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("%d\n", *(data + i));
return 0;
}
Output
Enter elements: 1
2
3
5
4
You entered:
1
2
24 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
3
5
4
40. write a c program to create initialize assign and access a pointer variable
#include <stdio.h>
int main()
{
int num; /*declaration of integer variable*/
int *pNum; /*declaration of integer pointer*/
pNum=& num; /*assigning address of num*/
num=100; /*assigning 100 to variable num*/
//access value and address using variable num
printf("Using variable num:\n");
printf("value of num: %d\naddress of num: %u\n",num,&num);
//access value and address using pointer variable num
printf("Using pointer variable:\n");
printf("value of num: %d\naddress of num: %u\n",*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
25 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
#include<stdio.h>
void swap(int *num1, int *num2) {
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
}
int main() {
int num1, num2;
printf("\nEnter the first number : ");
scanf("%d", &num1);
printf("\nEnter the Second number : ");
scanf("%d", &num2);
swap(&num1, &num2);
printf("\nFirst number : %d", num1);
printf("\nSecond number : %d", num2);
return (0);
}
Output
Enter the first number : 12
Enter the Second number : 22
First number : 22
Second number : 12
42. write a c program to count vowels and consonants in a string using pointer
#include <stdio.h>
int main()
{
char str[100];
char *ptr;
26 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
int cntV,cntC;
printf("Enter a string: ");
gets(str);
//assign address of str to ptr
ptr=str;
cntV=cntC=0;
while(*ptr!='\0')
{
if(*ptr=='A' ||*ptr=='E' ||*ptr=='I' ||*ptr=='O' ||*ptr=='U' ||*ptr=='a' ||*ptr=='e'
||*ptr=='i' ||*ptr=='o' ||*ptr=='u')
cntV++;
else
cntC++;
//increase the pointer, to point next character
ptr++;
}
printf("Total number of VOWELS: %d, CONSONANT: %d\n",cntV,cntC);
return 0;
}
Output
Enter a string: This is a test string
Total number of VOWELS: 5, CONSONANT: 16
43. write a c program to store information of a student 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");
// storing information
for (i = 0; i < 5; ++i) {
27 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
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:\n\n");
// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", 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
.
.
.
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()
28 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
{
printf("Enter information for 1st distance\n");
printf("Enter feet: ");
scanf("%d", &d1.feet);
printf("Enter inch: ");
scanf("%f", &d1.inch);
printf("\nEnter information for 2nd distance\n");
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 inch is greater than 12, changing it to feet.
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 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);
29 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
printf("Enter marks: ");
scanf("%f", &s.marks);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", s.name);
printf("Roll number: %d\n", s.roll);
printf("Marks: %.1f\n", 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
46. write a c program to declare intialize an union
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
union Data data;
printf( "Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
Output
Memory size occupied by data : 20
47. write a c++ program to implement function overloading
30 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
#include <iostream>
using namespace std;
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 area of rectangle using encapsulation
#include <iostream.h>
#include<conio.h>
class Rectangle
{
int x, y;
public:
31 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
void set_values (int,int);
int area (void)
{
return (x*y);
}
};
void Rectangle::set_values (int a, int b)
{
x=a;
y= b;
}
void main ()
{
clrscr();
Rectangle rect;
rect.set_values (3,4);
cout << "Area is : " << rect.area();
getch();
}
Output :
Area is : 12
49. write a c++ program to add two numbers using data abstraction
#include <iostream>
using namespace std;
class Add
{
public:
int num1, num2;
void ask(){
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
}
int sum(int n1, int n2){
return n1+n2;
}
//This function displays the addition result
void show(){
cout<<sum(num1, num2);
}
32 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
};
int main(){
//Creating object of class Add
Add obj;
obj.ask();
//Displaying the output
obj.show();
return 0;
}
Output:
Enter first number: 21
Enter second number: 19
40
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
33 | 33