Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

C++ Programming Code Examples

C++ > Miscellaneous Code Examples

At input side you simply to give year and then no. of months

/* At input side you simply to give year and then no. of months for finding calendar of that particular month */ #include<stdio.h> #include<conio.h> #include<dos.h> char month[12][10]={"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT" ,"NOV","DEC"}; char week[7][10]={"MON","TUE","WED","THU","FRI","SAT","SUN"}; long int days=0,year,mon,lp,i,dd,tab,n; void main() { int leap(int); clrscr(); printf("Enter a year : "); scanf("%d",&year); printf("Enter a month (1-12) : "); scanf("%d",&mon); if(year>1900) { for(i=1900;i<year;i++) { lp=leap(i); if(lp==1) days+=366; else days+=365; } } else if(year<1900) { for(i=1900;i>=year;i--) { lp=leap(i); if(lp==1) days+=366; else days+=365; } } i=1; while(i<mon) { if(i==1 || i==3 || i==5 || i==7 || i==8 || i==10 || i==12) { dd=31; if(year>=1900) days+=dd; else days-=dd; } else if(i==4 || i==6 || i==9 || i==11) { dd=30; if(year>=1900) days+=dd; else days-=dd; } else { lp=leap(year); if(lp==1) { dd=29; if(year>=1900) days+=dd; else days-=dd; } else { dd=28; if(year>=1900) days+=dd; else days-=dd; } } i++; } clrscr(); for(i=1;i<70;i++) printf("_"); for(i=1;i<5;i++) printf(" "); for(i=1;i<5;i++) printf(" "); printf("%d ",year); for(i=1;i<3;i++) printf(" "); for(i=1;i<5;i++) printf(" "); printf("%s ",month[mon-1]); for(i=1;i<70;i++) printf("_"); printf(" "); for(i=0;i<7;i++) printf(" %s",week[i]); printf(" "); tab=days%7; if(mon==1 || mon==3 || mon==5 || mon==7 || mon==8 || mon==10 || mon==12) { dd=31; } else if(mon==4 || mon==6 || mon==9 || mon==11) { dd=30; } else { lp=leap(year); if(lp==1) { dd=29; } else { dd=28; } } n=1; for(i=1;i<=7;i++) { if(i<=tab) { printf(" "); } else { printf("%2d ",n); n++; } } printf(" "); while(n<=dd) { i=1; while(i<=7) { if(n<=dd) { printf("%2d ",n); n++;i++; } else break; } printf(" "); } printf(" "); for(i=1;i<70;i++) printf("_"); getch(); } int leap(int y) { if(y%4==0) { if(y%100==0) { if(y%400==0) return 1; else return 0; } return 1; } else return 0; }

In computer programming, we use the if statement to run a block code only when a certain condition is met. An if statement can be followed by an optional else statement, which executes when the boolean expression is false. There are three forms of if...else statements in C++: • if statement, • if...else statement, • if...else if...else statement, The if statement evaluates the condition inside the parentheses ( ). If the condition evaluates to true, the code inside the body of if is executed. If the condition evaluates to false, the code inside the body of if is skipped.

C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of statements inside another loop. Any number of loops can be defined inside another loop, i.e., there is no restriction for defining any number of loops. The nesting level can be defined at n times. You can define any type of loop inside another loop; for example, you can define 'while' loop inside a 'for' loop. A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

As the name already suggests, these operators help in assigning values to variables. These operators help us in allocating a particular value to the operands. The main simple assignment operator is '='. We have to be sure that both the left and right sides of the operator must have the same data type. We have different levels of operators. Assignment operators are used to assign the value, variable and function to another variable. Assignment operators in C are some of the C Programming Operator, which are useful to assign the values to the declared variables. Let's discuss the various types of the assignment operators such as =, +=, -=, /=, *= and %=. The following table lists the assignment operators supported by the C language:

The getch() is a predefined non-standard function that is defined in conio.h header file. It is mostly used by the Dev C/C++, MS- DOS's compilers like Turbo C to hold the screen until the user passes a single value to exit from the console screen. It can also be used to read a single byte character or string from the keyboard and then print. It does not hold any parameters. It has no buffer area to store the input character in a program. The getch() function does not accept any parameter from the user. It returns the ASCII value of the key pressed by the user as an input.

In while loop, condition is evaluated first and if it returns true then the statements inside while loop execute, this happens repeatedly until the condition returns false. When condition returns false, the control comes out of loop and jumps to the next statement in the program after while loop. The important point to note when using while loop is that we need to use increment or decrement statement inside while loop so that the loop variable gets changed on each iteration, and at some point condition returns false. This way we can end the execution of while loop otherwise the loop would execute indefinitely. A while loop that never stops is said to be the infinite while loop, when we give the condition in such a way so that it never returns false, then the loops becomes infinite and repeats itself indefinitely.

Break statement in C++ is a loop control statement defined using the break keyword. It is used to stop the current execution and proceed with the next one. When a compiler calls the break statement, it immediately stops the execution of the loop and transfers the control outside the loop and executes the other statements. In the case of a nested loop, break the statement stops the execution of the inner loop and proceeds with the outer loop. The statement itself says it breaks the loop. When the break statement is called in the program, it immediately terminates the loop and transfers the flow control to the statement mentioned outside the loop.

In computer programming, loops are used to repeat a block of code. For example, when you are displaying number from 1 to 100 you may want set the value of a variable to 1 and display it 100 times, increasing its value by 1 on each loop iteration. When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop. A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.

It is a predefined function in "conio.h" (console input output header file) used to clear the console screen. It is a predefined function, by using this function we can clear the data from console (Monitor). Using of clrscr() is always optional but it should be place after variable or function declaration only. It is often used at the beginning of the program (mostly after variable declaration but not necessarily) so that the console is clear for our output.

Logical Operators are used to compare and connect two or more expressions or variables, such that the value of the expression is completely dependent on the original expression or value or variable. We use logical operators to check whether an expression is true or false. If the expression is true, it returns 1 whereas if the expression is false, it returns 0. Assume variable A holds 1 and variable B holds 0:

An array is a collection of data items, all of the same type, accessed using a common name. A one-dimensional array is like a list; A two dimensional array is like a table; The C++ language places no limits on the number of dimensions in an array, though specific implementations may. Some texts refer to one-dimensional arrays as vectors, two-dimensional arrays as matrices, and use the general term arrays when the number of dimensions is unspecified or unimportant. (2D) array in C++ programming is also known as matrix. A matrix can be represented as a table of rows and columns. In C/C++, we can define multi dimensional arrays in simple words as array of arrays. Data in multi dimensional arrays are stored in tabular form (in row major order).

A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.

The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities. The if...else ladder allows you to check between multiple test expressions and execute different statements. In C/C++ if-else-if ladder helps user decide from among multiple options. The C/C++ if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.

To calculate "grade of a student" on the basis of his total marks in C++ Programming, ask to the user to enter marks obtained in some subjects (5 subjects here). To calculate grade

If there exists "Multiple Strongly Connected" component, graph is not strongly connected, it is otherwise. Implementation of Kosaraju's Algorithm to "Print all SCCs". Fills Stack with

Program should display every prime number between 'range' and at the end total number of "prime numbers" found in range. Separate function which receives "two parameters" for

In C++, "Constructor" is automatically called when object ("instance of class") create. It is 'special member function' of the class. It has same name of class, must be public member