Learning C++: 2011-08-21

Pages

Wednesday, August 24, 2011

Finding length of string in C ? (Not for C++ Readers)

In below program, I have find out string length and print each character of string in new line..

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
 char string[10];
 int i=0,lengh=0;
 clrscr();

 printf("Enter the string: ");
 scanf("%s",&string);
 lengh=strlen(string);

 for(i=0;i<lengh;i++)
 {
 printf("%c",string[i]);
 printf("\n");
 }
 getch();
}

Monday, August 22, 2011

What are the basic arithmatic operations in C++ ?

Addition, subtraction, multiplication and division are basic operations of any programming language.
In C++, basic arithmetic operations are
1) Addition (+)
2) Subtraction (-)
3) Multiplication (*)
4) Division (/)
5) Increment by 1 (++)
6) Decrement by 1 (--)
Here I am demonstrating example of addition in C++, I want you guys to try other operations by your self. If you will find any problem, comment me.

Program Definition: Take two number from user and add them and display result.

How to solve: (Logic)
1) Take number 1 and save in no1 Variable.
2) Take number 2 and save in no2 variable.
3) Take no3 variable to save result of addition.
4) Add no1 and no2 and assign result in no3.
5) Display variable no3.

#include<stdio.h>
#include<iostream.h>
void main()
{
     int no1,no2,no3;
     //Clears output screen
     clrscr();
     cout<<"Enter first number: ";
     cin>>no1; 
     cout<<"Enter second number: ";
     cin>>no2;
     //Assign result of Addition of no1 and no2 to no3
     no3=no1+no2;
     cout<<"Addition of both numbers is: "<<no3;
     getch();
}

Try it, and try with all arithmetic operations and share your experiences.
Enjoy Coding :)