C&C++ Record
C&C++ Record
C&C++ Record
#include <stdio.h>
int main()
{
// calculating sum
sum = number1 + number2;
#include <stdio.h>
void main()
{
int number;
// displays output
printf("You entered: %d", number);
}
Output
Enter an integer: 25
You entered: 25
#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;
Output
Enter two numbers: 2.4
1.12
Product = 2.69
#include <stdio.h>
void main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
}
Output
Enter a character: G
ASCII value of G = 71
#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;
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;
Output
#include<stdio.h>
Void main()
{
double first, second, temp;
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
Output
#include <stdio.h>
void main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);
}
Output
Enter an integer: -7
-7 is odd.
Output
Enter an integer number
2
2 is even number
#include <stdio.h>
void main()
{
char c;
int lowercase, uppercase;
printf("Enter an alphabet: ");
scanf("%c", &c);
Output
Enter an alphabet: G
G is a consonant.
#include <stdio.h>
void main()
{
double n1, n2, n3;
printf("Enter three different numbers: ");
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
}
Output
#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
#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
#include <stdio.h>
int main()
{
int n, i, sum = 0;
#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++
return 0;
}
Output
Enter an integer: 10
Factorial of 10 = 3628800
#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
#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: ");
return 0;
}
Output
#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;
}
Output
#include <stdio.h>
int main()
{
int n, rev = 0, remainder;
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
Output
#include <stdio.h>
int main()
{
int n, reversedN = 0, remainder, originalN;
printf("Enter an integer: ");
scanf("%d", &n);
originalN = n;
return 0;
}
Output
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
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
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++
return 0;
}
Output
#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
#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
*
**
***
****
*****
#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
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) {
if (flag == 1)
printf("%d ", i);
}
return 0;
}
scanf("%lld", &n);
printf("%lld in binary = %d in decimal", n, convert(n));
return 0;
}
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);
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;
}
++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
#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;
}
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;
}
#include <stdio.h>
int main()
{
int n, i;
float num[100], sum = 0.0, avg;
avg = sum / n;
printf("Average = %.2f", avg);
return 0;
}
18 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
Output
#include <stdio.h>
int main()
{
int i, n;
float arr[100];
printf("Enter the number of elements (1 to 100): ");
scanf("%d", &n);
return 0;
}
Output
#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];
return 0;
}
Output
20 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
length = strlen(a);
return 0;
}
Output
21 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
#include <stdio.h>
#include <string.h>
int main()
{
char a[1000], b[1000];
strcat(a, b);
return 0;
}
Output
#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
fgets(s1, sizeof(s1), stdin);
s2[i] = '\0';
printf("String s2: %s", s2);
return 0;
}
22 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
Output
#include <stdio.h>
int main()
{
char line[150];
int vowels, consonant, digit, space;
#include <stdio.h>
23 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
int main()
{
char str[1000], ch;
int count = 0;
#include <stdio.h>
int main()
{
int data[5];
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*/
return 0;
}
Output
25 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
#include<stdio.h>
int main() {
int num1, num2;
swap(&num1, &num2);
return (0);
}
Output
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;
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++;
}
Output
#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
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;
#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("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
#include <stdio.h>
#include <string.h>
union Data {
int i;
float f;
char str[20];
};
int main( ) {
return 0;
}
Output
Memory size occupied by data : 20
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;
}
Output
Integer number: 5
Float number: 5.5
Integer number: 5 and float number: 5.5
#include <iostream.h>
#include<conio.h>
class Rectangle
{
int x, y;
public:
31 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
Output :
Area is : 12
49. write a c++ program to add two numbers using data abstraction
#include <iostream>
using namespace std;
class Add
{
public:
void ask(){
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
}
32 | 33
PRACTICAL RECORD OF PROGRAMMING WITH C & C++
};
int main(){
//Creating object of class Add
Add obj;
obj.ask();
#include <iostream>
using namespace std;
class Test
{
private:
int count;
public:
Test(): count(5){}
int main()
{
Test t;
// this calls "function void operator ++()" function
++t;
t.Display();
return 0;
}
Output
Count: 6
33 | 33