Monday, October 29, 2018

Finding Length of string using user defined functions

#include<iostream>
using namespace std;
int u_strlen(char s[])
{int i;
 for(i=0; s[i]!='\0'; i++);  // starts from first character till last
    return i; // return strlen
}
int main()
{
    // declare the string
    char name[40];
    //accept the string
   // cin>>name; // ignore space in between
    cin.getline(name,40); // accept space also
            cout<<name <<endl; // display string
    int len=u_strlen(name);
cout<<"Length of String is "<<len<<endl;
//display string in reverse order
for(int k=len-1;k>=0;k--)
cout<<name[k]<<endl;
    system("pause");
    return 0;
}
   
   
   
   
   
   
    

String Manipulation (output Answer)

#include<iostream>
using namespace std;
void u_strmanip(char s[])
{int i;
 for(i=0; s[i]!='\0'; i++)  // starts from first character till last
 {
  if(isupper(s[i]))
   s[i]=s[i]+1;
  else
  if(i%2!=0)
  s[i]=s[i+1];
  else
  if(!isalnum(s[i]))
  s[i]='*';
 
}
}
int main()
{
    // declare the string
    char name[40];
    //accept the string
   // cin>>name; // ignore space in between
    cin.getline(name,40); // accept space also
          //  cout<<name <<endl; // display string
   u_strmanip(name);
//display string in reverse order
cout<<name;
    system("pause");
    return 0;
}
   
    Give output for
CoMpuTEr SciENce 2019

Answer
DMNuuUF *TcEFOc *0099

   
   
   
   
    

Thursday, September 27, 2018

Concatenating two strings - strcat() - string.h

#include<string.h>
#include<iostream>
using namespace std;
// Program to convert a string into lower case

main()
{
char str[100],str_small[30];
cout<<"Enter a String 1 ";
cin.getline(str,100);

cout<<"Enter a String 2 ";
cin.getline(str_small,100);

cout<<"\n Entered String 1 = " <<str;
cout<<"\n Entered String 2 = " <<str_small;
strcat(str,str_small);
cout<<"\n Changed String = " <<str;
}

changing string into lowercase - strlwr() - string.h

#include<string.h>
#include<iostream>
using namespace std;
// Program to convert a string into lower case

main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);

cout<<"\n Original String = " <<str;
strlwr(str);
cout<<"\n Changed String = " <<str;
}

converting string into uppercase - strupr() - string.h

#include<string.h>
#include<iostream>
using namespace std;
// Program to convert a string into upper case

main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);

cout<<"\n Original String = " <<str;
strupr(str);
cout<<"\n Changed String = " <<str;
}

Comparing two strings - case senstive

#include<string.h>
#include<iostream>
using namespace std;
// Program to compare a string

main()
{
char str1[100], str2[100];
cout<<"Enter a String ";
cin.getline(str1,100);
cout<<"\n"<<str1;
cout<<"Enter a String ";
cin.getline(str2,100);
cout<<"\n "<<str2;

if(strcmpi(str1,str2)==0)
cout<<"\n Strings are equal";
else
cout<<"\n Strings are unequal";
}

Comparing two strings - case sensitive - string.h

#include<string.h>
#include<iostream>
using namespace std;
// Program to compare a string

main()
{
char str1[100], str2[100];
cout<<"Enter a String ";
cin.getline(str1,100);
cout<<str1;
cout<<"Enter a String ";
cin.getline(str2,100);
cout<<str2;

if(strcmp(str1,str2)==0)
cout<<"strings are equal";
else
if(strcmp(str1,str2)>0)
cout<<"\n "<<str1 <<" is greater than " <<str2;
else
if(strcmp(str1,str2)<0)
cout<<"\n "<<str1 <<" is smaller than " <<str2 ;

}

string.h - Copy String to another string

#include<string.h>
#include<iostream>
using namespace std;
// Program to copy a string

main()
{
char str[100], str_copy[100];
cout<<"Enter a String ";
cin.getline(str,100);

cout<<str;
strcpy(str_copy,str);
cout<<"\n Original String = " <<str;
cout<<"\n Copied String = " <<str_copy;
}

string.h - Finding out string length

#include<string.h>
#include<iostream>
using namespace std;
// Program to find out length of a string

main()
{
char str[100];
cout<<"Enter a String ";
cin.getline(str,100);

cout<<str;
int i=strlen(str);

cout<<"\n Total Number of characters in the string = " <<i;
}

Changing case into reverse case (Toggle case)

#include<ctype.h>
#include<iostream>
using namespace std;

main()
{

char ch;
cout<<" Enter a character ";
cin>>ch;

cout<<"Enter Character = "<<ch;
// Convert the characetr into Toggle Case
if(islower(ch))
ch=toupper(ch);

else
if(isupper(ch))
ch=tolower(ch);

cout<<"Changed case character  = "<<ch;

}

Changing lowercase character into uppercase

#include<ctype.h>
#include<iostream>
using namespace std;

main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;

cout<<"Enter Character = "<<ch;
// Check whether entered characetr is an lowercase letter and convert into Upper case
if(islower(ch))
ch=toupper(ch);

cout<<"Changed case character  = "<<ch;

}

Changing an uppercase character into lower case

#include<ctype.h>
#include<iostream>
using namespace std;

main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;

cout<<"Enter Character = "<<ch;
// Check whether entered characetr is an uppercase letter and convert into Lower case
if(isupper(ch))
ch=tolower(ch);

cout<<"Changed case character  = "<<ch;

}

Functions of ctype.h - Demo Program 1

// Program to demonstrate the usage of ctype.h - built in functions
#include<ctype.h>
#include<iostream>
using namespace std;

main()
{
char ch;
cout<<" Enter a character ";
cin>>ch;

// 1. Check whether entered characetr is an Alphanumeric Character or not
if(isalnum(ch))
cout<<" Entered character "<<ch <<" is alphanumeric character";

// 2.Check whether entered characetr is an Alphabet or not
if(isalpha(ch))
cout<<"\n" <<ch<< " Character entered and is an alphabet".

// 3. Check whether entered characetr is a Digit or not
if(isdigit(ch))
cout<<" Entered character "<<ch <<" is a digit.";

// 4. Check whether entered characetr is an uppercase letter
if(isupper(ch))
cout<<" Entered character "<<ch <<" is an uppercase letter.";

// 5. Check whether entered characetr is an lowercase letter
if(islower(ch))
cout<<" Entered character "<<ch <<" is an lowercase letter.";

}



Tuesday, September 11, 2018

Sum of digits of a number

#include <iostream>
#include<conio.h>
using namespace std;

main()
{
  int num,i, digit, sum=0;
  cout<<"Enter Number "; cin>>num;
  i=num;
          while(num!=0)
        {
            digit = num % 10;
            sum = sum + digit ;
            num/=10;
        }

       
            cout << "Sum of digits of number " <<i
<<" is "<<sum << endl;
    getch();
 }

Display Prime Numbers Between two Intervals

//Display Prime Numbers Between two Intervals
#include <iostream>
using namespace std;

int main()
{
    int start, num, end, i, flag;

    cout << "Enter two numbers(intervals): ";
    cin >> start >> end;

    cout << "Prime numbers between " << start << " and "
<< end << " are: ";

    while (start < end)
    {
        flag = 0;
num=start;
        for(i = 2; i <= num/2; ++i)
        {
            if(num % i == 0)
            {
                flag = 1;
                break;
            }
        }

        if (flag == 0)
            cout << num << " is a prime number\n ";

        ++start;
    }

    return 0;
}

Monday, September 10, 2018

Display Floyd’s Triangle in C++ | Learn Floyd’s Triangle in 5 minutes

Code Guide - Chandni Agarwal

Floyd's Triangle

// Program to print Pattern - Triangle with numbers
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num,print_val,r,c; // To input number of lines
cout<<"Enter Number "; cin>>num;
for(r=1,print_val=1; r<=num; r++) {
for(c=1 ; c<=r; c++,print_val++)
{
cout<<print_val<<" "; // will print col num
//in a single line
}
cout<<"\n"; // At end of inner loop next
//line will come in a new line
}
getch();
}

Print Number Pyramid

/* Number pyramid
            1
           121
  12321
*/ 
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num,r,c; // To input number of lines
cout<<"Enter Number "; cin>>num;
for( r=1; r<=num; r++) {
for(int sp=1;sp<=num-r;sp++) // SPACES BEFORE NUMBER
  cout<<" ";
for( c=1 ; c<=r; c++)//inner loop 2
{
cout<<c; // will print NUMBER in a single line
}
for(int k=c-2;k>=1;k--)
{
cout<<k;
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

Print pyramid of stars

/* Program to print Pattern - Triangle of stars
*
   * * *
* * * * *
*/
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;
int r,c;
for(r=1; r<=num; r++) {
for(int sp=1;sp<=num-r;sp++) cout<<"  ";
for(c=1 ; c<=2*r-1; c++)
{
cout<<"* "; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

Inverted Pyramid of stars

/* Program to print Pattern - Reverse Triangle of stars
*****
***
  *
*/
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;
int r,c;
for(r=num; r>=1; r--) {
for(int sp=1;sp<=num-r;sp++) cout<<" ";
for(c=1 ; c<=2*r-1; c++)
{
cout<<"*"; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

Print Diamond Pattern

// Program to print Diamond Pattern
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;
int r,c,sp;
for(r=1; r<=num; r++) {
for(sp=1;sp<=num-r;sp++) cout<<"  ";
for( c=1 ; c<=2*r-1; c++)
{
cout<<"@ "; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
for(r=num-1; r>=1; r--) {
for(sp=1;sp<=num-r;sp++) cout<<"  ";
for(c=1 ; c<=2*r-1; c++)
{
cout<<"@ "; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

Display pyramid and inverted pyramid using * and digits in C | Displa...

Thursday, September 6, 2018

pattern 7 - printing numbers - part 3 video

#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;

for(int r=num; r>=1; r--) {
for(int sp=1;sp<=num-r;sp++)
cout<<" ";
for(int c=r ; c>=1; c--)
{
cout<<c; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

star pattern - part 3 video

#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;
for(int r=1; r<=num; r++) {
for(int sp=1;sp<=num-r;sp++)//spaces before star
// num 5- r 1 = 5 1 to 4
  cout<<" ";
for(int c=1 ; c<=r; c++)
{
cout<<"*"; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

pattern 6 - part 3 video

#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;
for(int r=1; r<=num; r++) {
for(int sp=1;sp<=num-r;sp++) // SPACES BEFORE NUMBER
  cout<<" ";
for(int c=1 ; c<=r; c++)
{
cout<<c; // will print NUMBER in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

pattern 5 - numbers in reverse order - Right aligned - part3 video

#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;

for(int r=num; r>=1; r--) {
for(int sp=1;sp<=num-r;sp++)
cout<<" ";
for(int c=r ; c>=1; c--)
{
cout<<c; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

Pattern -4 Part3 video

#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;
int r,c; char ch;
for(r=1; r<=num; r++) {
for(int sp=1;sp<r;sp++)
cout<<" ";
for( ch='A',c=1 ; c<=num-r+1;ch++, c++)
{
cout<<ch; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

New Star Pattern in C++ | part -3 patterns in C++ | patterns usig Nested...

Tuesday, September 4, 2018

Triangle Patterns with Nested Loop part -2 | Nested Loops in C++ | Draw ...

Printing Number pattern 2

// Program to print Pattern - Triangle with numbers
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;
for(int r=1; r<=num; r++) {
for(int c=1 ; c<=r; c++)
{
cout<<r<<" "; // will print col num
//in a single line
}
cout<<"\n"; // At end of inner loop next
//line will come in a new line
}
getch();
}

printing alphabets in a grid pattern (1 to r) pattern program

// Program to print Pattern
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;
char ch; int c;
for(int r=1; r<=num; r++) {
for(c=1,ch='A' ; c<=r; c++,ch++)
{
cout<<(char)ch<<" "; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

Pattern printing number patterns part -2 Program 3

// Program to print Pattern 2
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;
for(int r=1; r<=num; r++) {
for(int c=1; c<=num-r+1; c++) // Inner Loop
// will run from 1 to num - r +1
//(value from outer loop)
// so with every line star will reduce
{
cout<<r<<" "; // will print col num in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

Printing Star pattern -2 Program

// Program to print Pattern - Triangle of stars
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines
cout<<"Enter Number "; cin>>num;
for(int r=1; r<=num; r++) {
for(int c=1; c<=num-r+1; c++) // Inner Loop
// will run from 1 to num - r+1
//(value from outer loop)
// so with every line star will reduce
{
cout<<"*"; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

Monday, September 3, 2018

Patterns using Nested Loop part -1 | Nested Loops in C++ | Draw star patt...

NESTED LOOP PATTERN PART -1 - ALL STARS

// Program to print Pattern - square of Stars
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines to print
cout<<"Enter Number "; cin>>num;
char ch; cout<<"Enter Character "; cin>>ch;
int r,c;
for( r=1; r<=num; r++)// Outer Loop to print lines - Vertical Count
{
for( c=1 ; c<=num; c++)// Inner Loop for Horizontal Count
{
cout<<ch; // will print * in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

Pattern Code - Part 1 - printing all numbers

// Program to print Pattern - Number Pattern in Lines
#include<iostream>
#include<conio.h>
using namespace std;
main()
{
int num; // To input number of lines to print
cout<<"Enter Number "; cin>>num;
int r,c;
for( r=1; r<=num; r++)// Outer Loop to print lines - Vertical Count
{
for( c=1 ; c<=num; c++)// Inner Loop for Horizontal Count
{
cout<<c<<" "; // will print Number in a single line
}
cout<<"\n"; // At end of inner loop next line will
// come in a new line
}
getch();
}

Tuesday, January 30, 2018

VERY VERY HELPFUL XI CS VIVA QUESTIONS

Viva General by chandni1972 on Scribd