0% found this document useful (0 votes)
50 views7 pages

Solve The Following Problems

The document contains instructions and code snippets for 5 C++ programs: 1) Print the table of a given number 2) Sort an array of numbers in ascending order 3) Convert a binary number to decimal and vice versa 4) Count the frequency of characters in a string 5) Delete all vowels from a given string

Uploaded by

Fairoz Ashour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views7 pages

Solve The Following Problems

The document contains instructions and code snippets for 5 C++ programs: 1) Print the table of a given number 2) Sort an array of numbers in ascending order 3) Convert a binary number to decimal and vice versa 4) Count the frequency of characters in a string 5) Delete all vowels from a given string

Uploaded by

Fairoz Ashour
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LAB#2

Solve the following problems

1. C++ program to Print Table of any Number

2. Sort an Array Elements in Ascending Order in C++

3. Covert a binary number to decimal and vise verse

4. C++ program to Count Frequency of Characters

5. C++ Program to Delete Vowels from Given String


C++ program to Print Table of any Number

The concept of generating table of any number is multiply particular number from 1 to 10.

num * 1

num * 2

num * 3

num * 4

num * 5

num * 6

num * 7

num * 8

num * 9

num * 10
Example

#include<iostream.h>
#include<conio.h>

void main(){
inti,no,table=1;
clrscr();
cout<<"Enter any num : ";
cin>>no;
cout<<"Table of "<<no;
for(i=1;i< =10;i++){
table=no*i;
cout<<" \n"<<table;
cout<<"\n";
}
getch();
}
Sort an Array Elements in Ascending Order in C++

Sort an array elements means arrange elements of array in Ascending Order and Descending Order. You

can easily sort all elements using bubble sort.


C++ Program to Sort Elements of Array in Ascending Order

#include<iostream.h>
#include<conio.h>
void main(){
Inti,a[10],temp,j;
clrscr();
cout<<"Enter any 10 num in array: \n";
for(i=0;i<=10;i++)
cin>>a[i];
cout<<"\nData before sorting: ";
for(j=0;j<10;j++)
cout<<a[j];
for(i=0;i<=10;i++)
{
for(j=0;j<=10-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"\nData after sorting: ";
for(j=0;j<10;j++)
cout<<a[j];
getch();
}
Program in C++ to Convert Binary to Decimal

In this types of program we takes a binary number as input and converts it into decimal number.

 Take a binary number as input.

 Multiply each digits of the binary number starting from the last with the powers of 2
respectively.

 Add all the multiplied digits.

 Heree total sum gives the decimal number.


Convert Binary to Decimal in C

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
longintbin_number,dec_number=0,i=1, rem;
cout<<"Please Enter any Binary Number: ";
cin>>bin_number;
while(bin_number!=0)
{
rem=bin_number%10;
dec_number=dec_number+rem*i;
i=i*2;
bin_number=bin_number/10;
}
cout<<"Equivalent Decimal value:"<<dec_number;
getch();
}
C++ program to Count Frequency of Characters

Frequency of character in any string means how many times a particular character is present in any

string. For example; suppose we have string hitesh in this word h repeated 2 times, it is the frequency of h

in string hitesh.
Count Frequency of Characters in C++

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

void main()
{
inti,count=0;
charch[20],c;
clrscr();
cout<<"Enter Any String: ";
gets(ch);
cout<<"Enter any Character form string: ";
cin>>c;
for(i=0;ch[i]!='\0';i++)
{
if(ch[i]==c)
count++;
}
if(count==0)
{
cout<<"Given character not found";
}
else
{
cout<<"Repetition of "<<c<<" "<<count<<" times";
}
getch();
}
C++ Program to Delete Vowels from Given String

To delete all the vowels (i.e., a, A, e, E, i, I, o, O, u, U) from the string in C++ program. In case of english

alphabet their are 5 lower case letter and 5 captipal letter, in this programming code we remove all small as

well as capital letter from given string. You can also see this code in C Programming Language, C Program

to Delete Vowels from Given String


C++ Program to Delete Vowels from Given String

#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
charstr[20];
intlen,i, j;
clrscr();
cout<<"Please Enter any String: ";
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||
str[i]=='E'||str[i]=='I'||str[i]=='O'|| str[i]=='U')
{
for(j=i; j<len;j++)
str[j]=str[j+1];
len--;
}
}
cout<<"After Deleting the vowels, the String is:"<<str;
getch();
}

You might also like