Neeraj Mishra

A crazy computer and programming lover. He spend most of his time in programming, blogging and helping other programming geeks.

C++ program to find cube of a number using macros

Also Read: C++ program to swap two numbers using macros Also Read: C++ Program to find cube of a number using function #include<iostream.h> #include<conio.h> #define CUBE(x) (x*x*x) void main() { clrscr(); int n,cube; cout<<“Enter a number:”; cin>>n; cube=CUBE(n); cout<<“Cube=”<<cube; getch(); }

C program to count number of words in a string

#include<stdio.h> #include<conio.h> void main() { int i,words=1; char str[100]; clrscr(); printf(“Enter a string:”); gets(str); for(i=0;str[i]!=’’;++i) if(str[i]==’ ‘) words++; printf(“No. of words are %d”,words); getch(); }

C++ program to swap two numbers using pointers

#include<iostream.h> #include<conio.h> void main() { clrscr(); int *a,*b,*temp; cout<<“Enter value of a and b:”; cin>>*a>>*b; temp=a; a=b; b=temp; cout<<“nAfter swapingna=”<<*a<<“nb=”<<*b; getch(); }

C program to print following triangle:

#include<stdio.h> #include<conio.h> void main() { int i,j,k,n,a,b; clrscr(); printf(“How many lines?”); scanf(“%d”,&n); n=n*2; for(i=1;i<n;i+=2) { for(j=n-1;j>i;j-=2) printf(” “); for(k=1;k<=(i/2)+1;++k) printf(“%d”,k); for(k=i/2;k>=1;–k) printf(“%d”,k); printf(“n”); } getch(); }