Showing posts with label c plus plus programming. Show all posts
Showing posts with label c plus plus programming. Show all posts

## 17.9.15

### How to find the smallest string in C++

Write a function  min that has three C string parameters and returns the smallest.

Solution
char* min(char* a, char* b, char* c)
{
return strcmp(a,b) < 0 ? strcmp(a,c) < 0 ? a : c : strcmp(b,c) < 0 ? b : c;
}

### switch statement in c++

Question
In the Mapple Leaf School System, children are classified by age as follows:
less than 2, ineligible
2, toddler
3-5, early childhood
8-10, elementary
11 and 12, middle
13, impossible
14-16, high school
17-18, scholar
greater than 18, ineligible

Given an int variable  age , write a switch statement that prints out the appropriate label from the above list based on  age.

Solution
switch (age)
{
case 0:
case 1:
cout << "ineligible";
break;
case 2:
cout << "toddler";
break;
case 3:
case 4:
case 5:
cout << "early childhood";
break;
case 6:
case 7:
break;
case 8:
case 9:
case 10:
cout << "elementary";
break;
case 11:
case 12:
cout << "middle";
break;
case 13:
cout << "impossible";
break;
case 14:
case 15:
case 16:
cout << "high school";
break;
case 17:
case 18:
cout << "scholar";
break;
default:
cout << "ineligible";
break;
}

### how to compare char in c++

Assume that c is a char variable that has been declared and already given a value. Write an expression whose value is true if and only if c is NOT what is called a white space character (that is a space or a tab or a newline-- none of which result in ink being printed on paper).

Solution
(c==' ' || c=='\t' || c=='\n') ? false : true

### calculate total= 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 using c++

Given an  int variable  n that has been initialized to a positive value and, in addition,  int variables  k and  total that have already been declared, use a  do...while loop to compute the sum of the cubes of the first  n whole numbers, and store this value in  total . Thus if  n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into  total . Use no variables other than  n ,  k , and  total .

Solution
total=0;
k=0;
do
{
total+=k*k*k;
k++;
}
while (k<=n);

### letter with loop c++

Write a loop that displays all possible combinations of two letters where the letters are 'a', or 'b', or 'c', or 'd', or 'e'. The combinations should be displayed in ascending alphabetical order:

aa
ab
ac
ae
ba
bb
...
ee

Solution:

for (char outerChar='a'; outerChar<='e'; outerChar++)
for (char innerChar='a'; innerChar<='e'; innerChar++)
cout << outerChar << innerChar << "\n";

## 15.9.15

### print all integer which are divisible by 3 using c++

/* Series of Integer which is divide by 3 */

#include<iostream>
#include<conio.h>
using namespace std;
int series(int start,int last)
{
cout<<"The series which is divide by 3 is:"<<endl;
for(int i=start;i<=last;i++)
{
if(i%3==0)
{
cout<<i<<"\t";
}
}
}
int main()
{
int start,last;
cout<<"Enter start value:"<<endl;
cin>>start;
cout<<"Enter Last value:"<<endl;
cin>>last;
series(start,last);
getch();
return 0;
}

### print strange number between 1 and 100000

/* print strange number 1-100000 */

#include<iostream>
#include<conio.h>
using namespace std;
int strange()
{
int i,j,c,temp,rem,k;
for(i=1;i<=100000;i++)
{
c=1;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
c=0;
break;
}
}
if(c==1)
{
temp=i;
while(temp>0)
{
rem=temp%10;
k=1;
for(j=2;j<=rem/2;j++)
{
if(rem%j==0)
{
k=0;
break;
}
}
if((k==0)||(rem==0))
{
k=0;
break;
}
rem/=10;
}
if(k==1)
{
cout<<i;
}
}
}
return 0;
}
int main()
{
cout<<"The Strange number between 1 to 100000 is:"<<endl;
strange();
return 0;
}

### find prime and check with the sum of all primes using c++

/* Print Prime List of the given range and do the sum then check weather the number is equal to the given number */

#include<iostream>
#include<conio.h>
using namespace std;
int prime_equal(int n)
{
int c,sum=0;
for(int i=1;i<=n;i++)
{
c=1;
for(int j=2;j<=i/2;j++)
{
if(i%j==0)
{
c=0;
break;
}
}
if(c==1)
{
cout<<i<<"\t";
sum+=i;
}
}
cout<<endl;
cout<<"The sum is:"<<sum<<endl;
if(sum==n)
{
cout<<"The sum is equal to "<<n<<endl;
}
else
{
cout<<"The sum is not equal to "<<n<<endl;
}
return 0;
}
int main()
{
int n;
cout<<"Enter Range:"<<endl;
cin>>n;
cout<<"prime List:"<<endl;
prime_equal(n);
return 0;
}

### find prime numbers and make sum of all primes using c++

/* Find all primes and do the sum of those primes between a given range */

#include<iostream>
#include<conio.h>
using namespace std;
void prime_check(int first,int last)
{
int c,j,count=0,sum=0;
cout<<"The Number of Primes:"<<endl;
for(int i=first;i<=last;i++)
{
c=1;
for(j=2;j<=i/2;j++)
{
if(i%j==0)
{
c=0;
break;
}
}
if(c==1)
{
count++;
sum+=i;
}
}
cout<<count<<"\t"<<endl;;
cout<<"The sum of the prime number is:"<<sum<<endl;
}
int main()
{
int first,last;
cout<<"Range Start from:"<<endl;
cin>>first;
cout<<"End of Range:"<<endl;
cin>>last;
prime_check(first,last);
getch();
return 0; }

### sum of all digits from an integer number using c++

/* A Program that reads a six digit integer and prints the sum of its six digit using Arithmatic operators. */

#include<iostream>
using namespace std;
int print_sum_of_digit(int number)
{
int a,i,sum=0,count=0;
if(number!=0)
{
while(number>0)
{
i=number%10;
number=number/10;
sum+=i;
count++;
}
cout<<"Number of Digit:"<<count<<endl;
return sum;
}
}
int main()
{
int number,i;
cout<<"Enter the digit of six integer:"<<endl;
cin>>number;
cout<<"The sum of the digit is:"<<print_sum_of_digit(number)<<endl;
return 0;
}

### conditional operator in c++

/* Use of Conditional Operator */

#include<iostream>
using namespace std;
int Multiple(int a,int b);
int main()
{
int a,b;
cout<<"Enter two integer:"<<endl;
cin>>a>>b;
Multiple(a,b);
}
int Multiple(int a,int b)
{
if(a>b)
{
if(a%b==0)
cout<<"Multiple"<<endl;
else
cout<<"Not Multiple"<<endl;
}
else if(a<b)
{
if(b%a==0)
cout<<"Multiple"<<endl;
else
cout<<"Not Multiple"<<endl;
}
return 0;
}

### comparison operator in c++

/* Use of Comparison Operator */

#include<iostream>
using namespace std;
string comparison(int age)
{
string str;
if(age<18)
str="You are a child";
else if(age>=18 && age<65)
else if(age>=65)
str="You are a senior citizen";
return str;
}
int main()
{
int age;
cout<<"Enter Your Age to know who You are:"<<endl;
cin>>age;
cout<<comparison(age);
}

### array programming in c++

/* Fill up an array,print the array,count number of values,count the number of odd,count the number of even,sum  of odd ,sum of even,average,find largest value,find smallest */

#include<iostream>
using namespace std;
int fill_up(int array[],int size);
int main()
{
int array,size;
cout<<"Enter the size of an array:"<<endl;
cin>>size;
fill_up(array,size);
}
int fill_up(int array[],int size)
{
int count=0,even=0,odd=0,even_sum=0,odd_sum=0,largest,smallest;
float avg=0.0,avg_sum=0.0;
cout<<"Enter the element:"<<endl;
for(int i=0;i<size;i++)
{
cin>>array[i];
if(array[i]%2==0)
{
even++;
even_sum+=array[i];
}
else
{
odd++;
odd_sum+=array[i];
}
count++;
avg_sum+=array[i];
}
avg=avg_sum/size;
largest=array;
smallest=array;
for(int i=0;i<size;i++)
{
if(array[i]<smallest)
{
smallest=array[i];
}
}
for(int i=0;i<size;i++)
{
if(array[i]>largest)
{
largest=array[i];
}
}
cout<<"The value of the array is:"<<endl;
for(int i=0;i<size;i++)
{
cout<<array[i]<<"\t";
}
cout<<endl;
cout<<"The number of value into the array:"<<count<<endl;
cout<<"The number of even Value is:"<<even<<endl;
cout<<"The number of Odd value is:"<<odd<<endl;
cout<<"The sum of the even number is:"<<even_sum<<endl;
cout<<"The sum of the Odd number is:"<<odd_sum<<endl;
cout<<"The Avarage is:"<<avg<<endl;
cout<<"The Largest value is:"<<largest<<endl;
cout<<"The Lowest value is:"<<smallest<<endl;
return 0;
}