26 Programs:
1. Age program
#include <iostream>
using namespace std;
int main() {
int age;
cout<<"Please enter your age.\n";
cin>>age;
if(age < 16 && age > 12){
cout<<"Congratulations! you are eligible for selection.";
}else{
cout<<"Sorry you are not eligible for this selection.";
return 0;
2. Array pointer
#include <iostream>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
cout<<arr[0]<<endl;
cout<<arr<<endl;
Page | 1
cout<<&arr[0]<<endl;
int * ptr = arr;
cout<<ptr<<endl;
ptr++; // now it points towrads 20
cout<<ptr<<endl;
cout<<*ptr<<endl;
ptr++; // now it points towards 30
cout<<ptr<<endl;
cout<<*ptr<<endl;
return 0;
3. Array
#include <iostream>
using namespace std;
int main() {
// int i1, i2, i3, i4, i5, i6, i7,i8, i9, i10;
// cout<<"Please enter number\n";
// cin>>i1;
// cout<<"Please enter number\n";
// cin>>i2;
// cout<<"Please enter number\n";
// cin>>i3;
// cout<<"Please enter number\n";
// cin>>i4;
Page | 2
// cout<<"Please enter number\n";
// cin>>i5;
// cout<<"Please enter number\n";
// cin>>i6;
// cout<<"Please enter number\n";
// cin>>i7;
// cout<<"Please enter number\n";
// cin>>i8;
// cout<<"Please enter number\n";
// cin>>i9;
// cout<<"Please enter number\n";
// cin>>i10;
// int length = 5; // Static Array Size
int length;
cout<<"Please enter array size\n";
cin>>length;
int numbers[length];
for(int index = 0; index < length; index++){
cout<<"Please enter number\n";
cin>>numbers[index];
// Array Printing.
for(int index = 0; index < length; index++){
cout<<numbers[index]<<"\n";
Page | 3
4. Array average
#include <iostream>
using namespace std;
int main() {
int total_students;
cout<<"Please enter number of students\n";
cin>>total_students;
int marks[total_students];
for(int i = 0; i < total_students; i++){
cout<<"Please enter student marks\n";
cin>>marks[i];
int sum = 0;
for(int i = 0; i < total_students; i++){
sum = sum + marks[i];
cout<<"Sum of All Marks = "<<sum<<endl;
int average = sum/total_students;
cout<<"Average = "<<average;
return 0;
Page | 4
5. c_lang_in_outoput.cpp
#include <stdio.h>
int main() {
int i;
printf("Please enter a value\n");
scanf("%d", i);
printf("You have entered %d\n", i);
return 0;
6. Calculator
#include <iostream>
using namespace std;
int main() {
int num1, num2;
int sum, difference, product, division, remainder, average;
cout<<"Please enter first number.\n";
cin>>num1;
cout<<"Please enter second number.\n";
cin>>num2;
// Sum of two numbers
Page | 5
sum = num1 + num2;
cout<<"The Jama of "<<num1<<" and "<<num2<<" is "<<sum<<endl;
//Difference of two numbers
difference = num2 - num1;
cout<<"The Tafreeq of "<<num1<<" and "<<num2<<" is "<<difference<<endl;
//Product of two numbers
product = num2 * num1;
cout<<"The Zarab of "<<num1<<" and "<<num2<<" is "<<product<<endl;
//Divisio of two numbers
division = num2 / num1;
cout<<"The Taqseem of "<<num1<<" and "<<num2<<" is "<<division<<endl;
// Remainder of two numbers
//Divisio of two numbers
remainder = num2 % num1;
cout<<"The Remainder of "<<num1<<" and "<<num2<<" is "<<remainder<<endl;
return 0;
7. char_pointer_string_example.cpp
#include <iostream>
using namespace std;
int main() {
Page | 6
int age;
int r_no;
char * name;
name = "hello world";
cout<<name;
cin>>name;
cout<<name;
// cout<<"Please enter your name.\n";
// cin>>name;
// cout<<"You have entered. "<<name;
return 0;
8. Comparison
#include <iostream>
using namespace std;
int main() {
// Comparison Operators
// > < >= <= == !=
int num1, num2;
cout<<"Please enter first number\n";
cin>>num1;
cout<<"Please enter second number\n";
cin>>num2;
Page | 7
if(num1 < num2){
cout<<"First Number is smaller than Second Number";
}else{
if(num1 == num2){
cout<<"Both numbers are equal";
}else{
cout<<"Second number is smaller than First Number";
return 0;
9. Factorial
#include <iostream>
using namespace std;
int main() {
int num;
int factorial = 1 ;
cout<<"Please enter a number.\n";
cin>>num;
// Method:1 1x2x3x4x5 = 120
// for(int index=1; index <= num; index++){
Page | 8
// factorial = factorial * index;
// }
// Method:2 5x4x3x2x1 = 120
for(int index = num; index >= 1; index--){
factorial = factorial * index;
cout<<"Factorial of "<<num<<" is "<<factorial;
return 0;
10. Factors
#include <iostream>
using namespace std;
// Find all factors of a given number.
int main() {
int num;
cout<<"Please enter a number.\n";
cin>>num;
for(int index = num; index >= 1; index--){
if(num % index == 0){
cout<<index<<endl;
Page | 9
return 0;
11. Functions
#include <iostream>
using namespace std;
void PrintAddition(int n1, int n2){
int result = n1 + n2;
cout<<"The sum of "<<n1<<" and "<<n2<<" is "<<result<<endl;
return ;
int main() {
int num1, num2;
cout<<"Please enter two numbers\n";
cin>>num1>>num2;
PrintAddition(num2, num1);
int num3 = 40;
int num4 = 60;
PrintAddition(num3, num4);
int num5 = 25;
int num6 = 30;
PrintAddition(num5, num6);
return 0;
Page | 10
}
12. If Conditions
#include <iostream>
using namespace std;
int main() {
int input;
int remainder;
cout<<"Please enter a number\n";
cin>> input;
remainder = input % 2;
if(remainder == 0){
cout<<input<<" is an Even number";
if(remainder == 1){
cout<<input<<" is an Odd number";
return 0;
13. If Else
Page | 11
#include <iostream>
using namespace std;
int main() {
// Arthematic Operators
// + - * / %
// Logic Operators
// && ||
// Comparison Operators
// > < >= <= == !=
int input;
int remainder;
cout<<"Please enter a number\n";
cin>> input;
remainder = input % 2;
if(remainder != 0){
cout<<input<<" is an Odd number";
}else{
cout<<input<<" is an Even number";
return 0;
Page | 12
14.Integers
#include <iostream>
using namespace std;
int main() {
int num1 = 10;
int num2 = 20;
int result = num1 + num2;
result = num1 + num2;
num1 = num2;
num1 = num2 + num2 + num1;
num2 = num1 + 10;
num1 = num2 - num1 + result;
num1++; // 41
num1++; // 42
cout<<"Sum of "<<num1<<" and "<<num2<<" is "<<result;
return 0;
15. Loop
Page | 13
#include <iostream>
using namespace std;
int main() {
for(int i = -5; i < 15; i = i+2){
cout<<i<<endl;
i++;
i = i + 1;
return 0;
16. Months
#include <iostream>
using namespace std;
int main() {
int month;
cout<<"Please enter the month\n";
start:
cin>>month;
switch(month){
// 30 days
case 4:
Page | 14
case 6:
case 9:
case 11:
cout<<"This month has 30 days.";
break;
// 31 days
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
cout<<"This month has 31 days.";
break;
case 2:
cout<<"This month has 28 or 29 days.";
break;
default:
cout<<"You have entered incorrect month number.\nPlease enter valid month
number.";
goto start;
return 0;
Page | 15
17. Nested loop
#include <iostream>
using namespace std;
int main() {
for(int index1 = 0; index1 <= 10; index1 = index1+1){
for(int index2 = 1; index2 <= index1; index2++){
cout<<"*";
cout<<endl;
return 0;
18. Palindrome
#include <iostream>
using namespace std;
int main() {
int num;
int actual_number;
int reverse = 0;
cout<<"Please enter a number.\n";
Page | 16
cin>>num;
actual_number = num;
while(num >0){
reverse = reverse * 10 + (num % 10);
num = num / 10;
if(reverse == actual_number){
cout<<actual_number<<" is a Palindrome.";
}else{
cout<<actual_number<<" is not a Palindrome";
return 0;
19. Pointers
#include <iostream>
using namespace std;
int main() {
int num1 = 30;
int num2 = 40;
int * ptr = &num1;
int * ptr2 = &num1;
ptr = &num2;
ptr2 = &num1;
num1 = num1 + num2 + *ptr + *ptr2;
*ptr = 20;
Page | 17
cout<<num2 * *ptr;
return 0;
20. Prime
#include <iostream>
using namespace std;
int main() {
int num;
int temp = 0;
cout<<"Please enter a number\n";
cin>>num;
for(int index = num; index > 1; index--){
if(num % index == 0){
temp++;
if(temp > 1){
cout<<num<<" is not a Prime Number";
}else{
cout<<num<<" is a Prime number.";
return 0;
Page | 18
21. Reverse
#include <iostream>
using namespace std;
int main() {
int num;
int reverse = 0;
cout<<"Please enter a number.\n";
cin>>num;
while(num >0){
reverse = reverse * 10 + (num % 10);
num = num / 10;
cout<<"Reverse: "<<reverse;
return 0;
22. Starts diamond
#include <iostream>
using namespace std;
int main() {
Page | 19
int n;
cout << "Enter the number of rows for the diamond: ";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
cout << endl;
// Lower part of the diamond
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
cout << " ";
for (int k = 1; k <= 2 * i - 1; k++) {
cout << "*";
cout << endl;
Page | 20
return 0;
23. Strings
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int main() {
// Example strings
char str1[20] = "Hello\0World";
// char str2[20] = "HelloStudents";
char str2[20] = "HELLO";
char str3[40] = "Concatenated: ";
char str4[] = "Irfan\0Khan";
cout<<str4<<endl;
char empty_str[] = "";
// 1. strlen()
printf("Length of str1: %zu\n", strlen(str1));
printf("Length of empty_str: %zu\n", strlen(empty_str));
// 2. strcpy()
strcpy(str3, str1);
printf("Copied str1 to str3: %s\n", str3);
Page | 21
// 3. strcat()
strcat(str3, str2);
printf("Concatenated str2 to str3: %s\n", str3);
// 4. strcmp()
printf("Comparison result: %d\n", strcmp(str1, str2));
// 5. strncmp()
printf("Comparison result (first 3 characters): %d\n", strncmp(str1, str2, 3));
// 6. strchr()
char *ptr = strchr(str3, 'W');
if (ptr != NULL) {
printf("Found 'W' at position: %ld\n", ptr - str3);
} else {
printf("'W' not found in str3\n");
// 7. strstr()
char *substr = strstr(str3, "World");
if (substr != NULL) {
printf("Found 'World' in str3 at position: %ld\n", substr - str3);
} else {
printf("'World' not found in str3\n");
// 8. strncpy()
char dest[10];
Page | 22
strncpy(dest, str1, 3);
dest[3] = '\0';
printf("Copied first 3 characters of str1 to dest: %s\n", dest);
// 9. strncat()
strncat(dest, str2, 2);
printf("Concatenated first 2 characters of str2 to dest: %s\n", dest);
// 10. strtok()
char sentence[] = "This is a sample sentence.";
char *token = strtok(sentence, " ");
printf("Tokens: ");
while (token != NULL) {
printf("%s ", token);
token = strtok(NULL, " ");
printf("\n");
return 0;
// 11. sprintf()
char formatted[50];
sprintf(formatted, "The sum of %d and %d is %d.", 5, 7, 5 + 7);
printf("Formatted string: %s\n", formatted);
// 12. snprintf()
char limited[10];
snprintf(limited, sizeof(limited), "Too long string");
printf("Limited string: %s\n", limited);
Page | 23
return 0;
24. Switch
#include <iostream>
using namespace std;
int main() {
int input;
start:
cout<<"Please enter a digit from 0 to 9\n";
cin>>input;
cout<<"You have entered ";
switch(input){
case 1:
cout<<"ONE";
break;
case 2:
cout<<"TWO";
break;
case 3:
cout<<"THREE";
break;
case 4:
cout<<"FOUR";
break;
case 5:
Page | 24
cout<<"FIVE";
break;
case 6:
cout<<"SIX";
break;
case 7:
cout<<"SEVEN";
break;
case 8:
cout<<"EIGHT";
break;
case 9:
cout<<"NINE";
break;
default:
cout<<" Wrong Number.\n";
goto start;
return 0;
25. Table
#include <iostream>
using namespace std;
Page | 25
int main() {
int table;
cout<<"Please enter a number\n";
cin>>table;
for(int index = 1; index <= 16; index++){
cout<< table <<" x "<< index << " = " << table * index << endl;
return 0;
26. Two D array
#include <iostream>
using namespace std;
int main() {
// int arr[2][3] = {{1, 2, 3}, {4, 5, 6}};
//
// cout<<arr[0][0]; // 1
// cout<<arr[1][1]; // 5
//
int marks[5][2];
for(int i = 0; i < 5; i++){
cout<<"Please Enter Roll No.\n";
cin>>marks[i][0];
cout<<"Please Enter Marks.\n";
Page | 26
cin>>marks[i][1];
cout<<"R.No\t\tMarks\n";
for(int i=0; i < 5; i++){
cout<<marks[i][0]<<"\t\t"<<marks[i][1]<<endl;
return 0;
27.
Page | 27