You are on page 1of 6

METHODS n FUNCTIONS

The major difference between methods and functions is that methods called by the reference variables called objects where as the functions do not having any reference variables. Functions have independent existence which means they are defined outside the class. Method do not have independent existence that means they are defined inside the class.

Write "hello world program" without using any semicolon in C


main() { if (printf("nHello World")) { } }

Write a c program to print the floyd's triangle given below 1 2 3 4 5 6 7 8 9 10 11 ...........15?


#include <stdio.h> #include <conio.h> main( ) { int i, j, k = 1; clrscr( ); for( i = 1; k <= 15; ++i ) { for( j = 1; j <= i; ++j ) printf( "%d ", k++ ); printf( "\n\n" ); } getch( ); return 0; }

Write a c program to print Pascal triangle


#include<stdio.h> int main(){ int length,i,j,k;

//Accepting length from user printf("Enter the length of pascal's triangle : "); scanf("%d",&length); //Printing the pascal's triangle for(i=1;i<=length;i++) { for(j=1;j<=length-i;j++) printf(" "); for(k=1;k<i;k++) printf("%d",k); for(k=i;k>=1;k--) printf("%d",k); printf("\n"); } return 0; }
Write a Program to print this triangle: * ** * **** * ****** * ******** * ********** use two nested loops.

void main() { int n = 5 ; // take use input here int i, j; for( i =1; i<=n; i++){ printf("\n *"); for(j=0; j<=i; j++){ printf(" *"); } } }

Program to print simple triangle of asterisks


1. #include <stdio.h> 2. 3. int main() 4. { 5. int i, j; 6. 7. for(i = 1; i <= 10; i = i + 1) 8. { 9. </stdio.h> <stdio.h>for(j = 1; j <= i; j = j + 1) 10. </stdio.h> <stdio.h>printf("*"); 11. </stdio.h> <stdio.h>printf("\n"); 12. } 13. return 0; 14. }

#include main() { int i,j; clrscr(); for(i=1;i<=12;i+=2) { for(j=1;j<=1;j++) { printf("*"); } printf("\n"); } getch(); }

Tell one difference which is in C and not in C++. Can we pass arguments in main() Difference between static and constant variable.
one thing in c but not present in c++ ============================= in C we need not to mention the header files for using the built-in functions.

but, in C++ we must mention the header file for same purpose. this is the very basic difference between these two languages and when MAIN() have arguments this means that... when we start program from command promt we can pass starting arguments to the program. these r nothing but the values which the programmer things that must be there when program starts execution. yes we can pass arguments if we pass argument ir would become commandline argument. eventhough static and constant variable are common for alla objects the value of static variable can be increased in time being but it cannot be increased in constant variable C is a procedure oriented language (emphasis on procedure or algorithms) C++ is object oriented (emphasis on data) In C keywords new and delete are not available(memory management),in C++ its otherwise In C we use multiline comments/* */,C++ single line// C does not provide default arguments while C++ does. Static variables change their values with the iteration while the while does not change when constant variables are used.

Yes we can pass argument through main function. For example void main(int arg,char d[5]) // arg count the number of argument (currently no. of argument is 2) { if(arg<1) //2<1 { cout<< "hello"; exit(0); } for(int i=0;i<arg;i++) {cout<<"test-msg";} //output will be test-msg Difference: 1. C is procedural language and C++ is object oriented. 2. C works on top down approach & C++ on bottom up approach.

in C data is on back seat whereas C++ it is an driving seat

1. C has malloc and calloc for dynamic allocation of memory, while C++ uses "new" 2. Yes we can pass.. it is called command-line agument 3. Static can be changed, constant cannot be 1. C is a subest of C++ 2. C provide action/structure/function oriented programming C++ provides object oriented programming with features such as : abstraction, encapsulation, polymorphism, inheritance. 3. C++ is a strong type checking langugae as compared to c. So many programs which would complile without any warning or error in c compiler results in many warning and error in C++ compiler. 4. C++ supports sophisticated error handling using the Exception Handling Mechanism. C++ is more secure than C. C++ is object oriented where as C++ is not. C have got Top to down approach where as C++ has got bottom up approach. Inheritance & polymorphism is available in C++ whereas they are not in C.

In c declaration of variables are allowed only at the Beginning of the method i.e. just after '{' . but in c++ we can declare anywhere. :) In c declaration must be just after opening braces { of method. The is flexibility in c++.

what is incorrect among teh following A recursive functiion a.calls itself b.is equivalent to a loop c.has a termination cond d.does not have a return value at all
The last one statement does not have a return value at all is wrong. Because a recursive function return always a value to itself. It can return a value.

regarding the scope of the varibles;identify the incorrect statement: a.automatic variables are automatically initialised to 0 b.static variables are are automatically initialised to 0 c.the address of a register variable is not accessiable d.static variables cannot be initialised with any expression
the incorrect statement regardint the scope of variables is automatic variables are automatically initialised to zero. Actually when the are declared they will contain some Garbage value.

You might also like