Program for practice
Program for practice
write a program that inputs string from the user and print how many
digits are in the string:
#include <iostream>
int main(){
return 0;}
C++ program for printing no from 1 to10:
#include <iostream>
int main(){
int i;
for(i=0;i<=10;i++){
cout<<i<<"";
cout<<"/n";
return 0;}
int main(){
int age[5]={10,20,40,69,69};
int sum = 0;
float avg;
for(int i=0;i<5;i++){
sum+=age[i];
}cout<<sum<<endl;
int length(sizeof(age)/sizeof(age[0]));
cout<<length<<endl;
avg=sum/length;
cout<<"avg="<<avg<<endl;
return 0;
}
Calculate lowest age among different ages:
#include <iostream>
int main(){
int age,lowestAge;
int ages[9]={3,6,46,78,9,6,5,4};
lowestAge =ages[0];
for(int i=0;i<9;i++)
lowestAge =ages[0];
}
cout<<"lowest age"<<lowestAge<<endl;
return 0;
Below is a C++ program that reads the temperature value and the unit (C for Celsius or F for
Fahrenheit), and then prints whether water is in a solid, liquid, or gas state at the given
temperature.
In this program:
#include <iostream>
using namespace std;
int main() {
float temp;
char unit;
return 0;
#include<iostream>
int main(){
int n=5;
for(int i=0;i<n;i++){
for(int j=1;j<=2*i+1;j++){
cout<<"*";
cout<<endl;
}
return 0;
1. i:
o This represents the current row index.
o In programming, loops often start counting from 0. So, for the first row, i = 0,
the second row i = 1, and so on.
2. 2 * i:
o This part ensures that the number of asterisks increases by 2 as you move from
one row to the next. This is because in an odd-number triangle, the difference
between the number of asterisks in consecutive rows is always 2 (e.g., 1, 3, 5,
7, ...).
3. + 1:
o Since we want the number of asterisks to start at 1 for the first row (not 0), we add
1 to the result of 2 * i.
Odd numbers are evenly spaced, with a difference of 2 between consecutive odd
numbers.
The general formula for the nth odd number (starting from 1) is 2 * i + 1, where i
starts from 0.
#include<iostream>
int main(){
int a,b;
cin>>a>>b;
for(int num=a;num<=b;num++){
int i;
for(i=2;i<num;i++){
if(num%i ==0){
break;
}
} if(i ==num){
cout<<num<<endl;}
return 0;
}
Prime numbers using function:
#include<iostream>
for(int i=2;i<num;i++){
if(num%i==0){
return false;
return true;
int main(){
int a,b;
cin>>a>>b;
for(int i=a;i<=b;i++){
if (isPrime(i)){
cout<<i<<endl;
return 0;
}
Fibioncii sequence using function:
#include<iostream>
int t1=0;
int t2=1;
int nextTerm;
for(int i=0;i<=n;i++){
cout<<t1<<endl;
nextTerm=t1+t2;
t1=t2;
t2=nextTerm;
return;
int main(){
int n;
cin>>n;
fib(n);
return 0;
int factorial=1;
for(int i=1;i<n;i++){
factorial*=i;
return factorial;
int main(){
int n;
cin>>n;
int ans=fact(n);
cout<<ans<<endl;
return 0;
}
Calculate nCr using function:
#include<iostream>
int factorial=1;
for(int i=1;i<=n;i++){
factorial*=i;
return factorial;
int main(){
int n,r;
cin>>n>>r;
int ans=fact(n)/(fact(r)*fact(n-r));
cout<<ans<<endl;
return 0;
}
#include<iostream>
int factorial=1;
for(int i=1;i<=n;i++){
factorial*=i;
return factorial;
int main(){
int n;
cin>>n;
for(int i=0;i<=n;i++){
for(int j=0;j<=i;j++){
cout<<fact(i)/(fact(j)*fact(i-j))<<" ";}
cout<<endl;}
return 0;
for(int i=0;i<n;i++){
if(arr[i]== key){
return i;
return -1;
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
int key;
cin>>key;
cout<<linearSearch(arr,n,key);
return 0;
int e=n;
while(s<=e){
if(arr[mid]=key){
return mid;
e=mid-1;
else e=mid+1;
return -1;
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>arr[i];
int key;
cin>>key;
cout<<binarySearch(arr,n,key);
return 0;
}
This question requires the implementation of a C++ function to merge
two sorted arrays A and B into a third array C in ascending order.
#include <iostream>
#include<algorithm>
for(int i=0;i<m;i++)c[i]=a[i];
for(int i=0;i<n;i++)c[m+i]=b[i];//
sort(c,c+m+n);
int main(){
int n,m;
cin>>m;
cin>>n;
int a[m],b[n],c[m+n];
for(int i=0;i<m;i++){
cin>>a[i];
for(int i=0;i<n;i++) {
cin>>b[i];
}
mergearray(a,m,b,n,c);
for(int i=0;i<m+n;i++){
cout<<c[i]<<" ";
cout<<endl;
return 0;
Transeverse of 2 D array:
#include <iostream>
int main() {
A[i][j] = A[j][i];
A[j][i] = temp;
return 0;
}
The outer loop iterates through rows (i), while the inner loop handles the columns (j), starting
at i + 1 to ensure only the upper triangle of the matrix is processed.
Iterations:
1. i = 0:
o j = 1: Swap A[0][1] (2) with A[1][0] (4).
o j = 2: Swap A[0][2] (3) with A[2][0] (7).
2. i = 1:
o j = 2: Swap A[1][2] (6) with A[2][1] (8).
3. i = 2:
o The loop doesn't execute because j = i + 1 = 3 exceeds the matrix size.
Why Not j = 0?
1. Redo swaps that have already been performed (e.g., swap A[1][0] with A[0][1] again).
2. Waste time processing diagonal elements that don't need to be swapped.
int main() {
int m1[n1][n2];
int m2[n2][n3];
// Matrix multiplication
cout << ans[i][j] << " "; // Add space between numbers
return 0;
Matrix Search:
#include <iostream>
int main() {
int n,m;
cin>>n>>m;
int target;cin>>target;
int mat[n][m];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++)
cin>> mat[i][j];
bool found=false;
found =true;
if(mat[r][c]>target){
c--;
else{
r++;
if (found)
cout<<"element found"<<endl;
else{
cout<<"element no exist";
return 0;
}
Running sum of an array using vector library like leetcode:
#include <iostream>
class Solution {
public:
return nums;
};
int main() {
Solution sol;
return 0;}
#include <iostream>
result[i] = result[i - 1] + nums[i]; // Add the previous sum to the current element
int main() {
int n;
cin >> n;
return 0;
}
Reverse of an array using 2 pointer approach:
#include <iostream>
int start=0,end=n-1;
while(start<=end){
swap(arr[start],arr[end]);
start++;
end--;
int main(){
int arr[]={1,6,7,8,9,4};
int n=6;
arrayReverse(arr,n);
for(int i=0;i<n;i++){
cout<<arr[i]<<endl;
return 0;
}
Swap the largest and smallest number of an array:
#include <iostream>
#include <climits> // For INT_MIN and INT_MAX
using namespace std;
int main() {
int a[] = {1, 6, 7, 8, 9, 4}; // Array of elements
int n = 6; // Size of the array
for(int i=0;i<n;i++){
int count=0;
for(int j=0;j<n;j++){
if(a[i]==a[j])
count++;
if(count==1){
cout<<a[i];
cout<<endl;
int a[]={1,2,2,3,5,6};
int n=6;
uniqueArray( a,n);
return 0;
#include <iostream>
for(int i=0;i<n;i++){
int count=0;
for(int j=0;j<m;j++){
if(a1[i]==a2[i]){
cout<<a1[i]<<" ";
break;
cout<<endl;
int a1[]={1,2,2,3,5,6};
int n=6;
int a2[]={1,4,2,3,8,9};
int m=6;
return 0;