Skip to main content

Posts

Showing posts from December, 2011

C Model Question Papers for BCA 1st year Kannur Bharathiar MG Madras Bangalore University

C Model Question Papers for BCA 1st year Kannur Bharathiar MG Madras Bangalore University : Fist Year BCA Model Question Paper for C Programming Language :- Model Question Paper B.C.A. : I Year: Programming using C: Time: 3 Hrs. Max. Marks: 100 SECTION – A 1) Answer the following Questions : 10 x 2 = 20 M a) Define Algorithm. b) Define Identifier. c) What is the Purpose of sizeof() operator. d) Compare while and do-while loops. Justify your answer. e) Define Recursion. f) How can we initialize an array. g) What is the difference between structure and union ? h) Define Pointer variable. i) Define Macro. j) What is the purpose of rewind() function. Section – B Answer the following questions. 4 x 20 = 80 M 2. a) Explain the structure of C program. b) Explain features of C language. (or) c) Describe in brief the operators in C language. d) Write a C program to evaluate the following expression. (a+(b*c)/d) 3. a) Explain control structures with examples. (or) b) Explain different parameter ...

Reverse Digits of a Number in C Program, Number Reverse using Recursion or Array in C Program

Reverse Digits of a Number in C Program, Number Reverse using Recursion or Array in C Program : Example : Write a C program to reverse the digits of a Number. #include<stdio.h> #include<conio.h> void main() { int n,rem,reverse_number=0; clrscr(); printf("Enter an integer number to be reversed :"); scanf("%d",&n); while ( n != 0 ) { rem = n % 10 ; n = n / 10 ; reverse_number = ( reverse_number * 10 ) + rem ; } printf("Reverse Number is %d\n",reverse_number); getch(); } Example : c program to reverse the digits of a number using recursion. #include<stdio.h> #include<conio.h> void reverse_Number(int); void main() { int num ; clrscr(); printf("Enter number : "); scanf("%d", & num); printf("\nNumber Reverse = "); reverse_Number(num); getch(); } void reverse_Number(int num) ...

C Code Shortening Techniques or My Logic for Reduced Source Code Writing in C Program Language

C Code Shortening Techniques or My Logic for Reduced Source Code Writing Tips and Tricks in C Program Language :- Role of Semi Colon(;) and Curly Braces({ }) in C Program Perhaps, you might have heard that the for() loop should not end with ";" like if(). But, in fact the ";" is using in C language as a Statement Terminator, so, if we want to terminate for() loop, then we should use ";" at the end of for() loop. [ Header files are to be included as usual, it is not written in the examples ] Question : write a C program to print 1 to 10 numbers using for() loop ? Example 1 : void main() { int i; for ( i = 1 ; i <= 10 ; i++ ) { printf("%d\n",i); } } /* above program, for() loop has one statement outside, so we do not use ";" at the end, because if we put ";" the for() will not take the continuing statement as its own. */ Example 2 : void main() { int i; for ( i = 1 ; i <= 10 ; printf( "%d \n" , i ...

Insertion Sort in C Program with Algorithm and Function Code Reducing Techniques

Insertion Sort in C Program with Algorithm and Function Code Reducing Techniques : Example for Insertion Sorting in C Language Program with Explanations is given below :- Insertion Sorting Algorithm : Every repetition of insertion sort removes an element from the input data, inserting it into the correct position in the already-sorted list, until no input elements remain. The choice of which element to remove from the input is arbitrary, and can be made using almost any choice algorithm. Sorting is typically done in-place. The resulting array after k iterations has the property where the first k + 1 entries are sorted. In each iteration the first remaining entry of the input is removed, inserted into the result at the correct position, thus extending the result: #include<stdio.h> #include<conio.h> void insertion_Sort(int, int []); /* function prototype declaration */ void main() { int arr[100] , a , n ; clrscr(); printf("Enter the number of array elements to be Sor...

Quick Sort in C Code with Algorithm using Recursion and Code Reducing Techniques

Quick Sort in C Code with Algorithm using Recursion and Code Reducing Techniques : Example for Quick Sort Program in C using Recursion with Algorithm is given below :- Algorithm : Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists. Pick an element, called a pivot, from the list. Reorder the list so that all elements with values less than the pivot come before the pivot, while all elements with values greater than the pivot come after it (equal values can go either way). After this partitioning, the pivot is in its final position. This is called the partition operation. Recursively sort the sub-list of lesser elements and the sub-list of greater elements. The base case of the recursion are lists of size zero or one, which never need to be sorted. #include<stdio.h> #include<conio.h> void quick_Sort( int [], int, int ); /* functi...

C Palindrome String using Pointers Example, C Program to find String Palindrome or not.

C Program for Palindrome String using Pointers Example, C Program to find String Palindrome or not? Example : Write a Program to print a String Palindrome or not in C Language is given below:- #include<stdio.h> #include<conio.h> #include<string.h> int string_Palindrome(char* str) { int i,len; len = strlen(str); for( i = 0 ; i < len / 2 ; i++ ) { if( *(str + i ) != *(str + len - i - 1) ) return 0; } return 1; } void main() { char string[100]=""; clrscr(); printf("Enter a string :"); gets(string); if( string_Palindrome(string)) printf("\n%s is a palindrome",string); else printf("\n%s is not a palindrome",string); getch(); }

Binary Search use Recursion in C or C Program for Binary Search using Array Passing UDF

Binary Search using Recursion function in C or C Program for Binary Search using Array Passing User Defined Function :- Example : How to write C Program for Binary Search using Recursive Function and its solutions given below :- #include<stdio.h> #include<conio.h> int binarySearch(int lmt[],int arrayStart,int arrayEnd,int a) /* UDF declar with definition */ { int m,pos; if (arrayStart<=arrayEnd) { m=(arrayStart+arrayEnd)/2; if (lmt[m]==a) return m; else if ( a < lmt[m] ) return binarySearch(lmt,arrayStart,m-1,a); /* function calls itself called Recursive function */ else return binarySearch(lmt,m+1,arrayEnd,a); /* function calls itself called Recursive function */ } return -1; } void readArray(int lmt[],int n) /* UDF declar with definition */ { int i; printf("Enter the array elements :\n"); for( i=0 ; i < n ; i++ ) scanf("%d",&lmt[i]); } /* main() function starts here */ void main()...

C Program for Bubble Sort Example, C Bubble Sort integer Array, Bubble Sort Program in C Language

C Program for Bubble Sort Example, C Bubble Sort integer Array, Bubble Sort Program in C Language :- Example - Bubble Sort Program in C for integer Array with maximum 100 elements and the source is given below :- #include<stdio.h> #include<conio.h> void main( ) { int arr[100], i, j, n, temp ; clrscr(); printf("Enter the number of elements to be sorted :"); scanf("%d",&n); printf("\nEnter the integer elements :\n"); for(i=0 ; i <n ; scanf("%d",&arr[i]), i++); /* here for() loop ended with ; and scanf() can also be put out side for() as we use regular */ n=n-2; for ( i = 0 ; i <= n ; i++ ) { for ( j = 0 ; j <= n - i ; j++ ) { if ( arr[j] > arr[j + 1] ) { temp = arr[j] ; arr[j] = arr[j + 1] ; arr[j + 1] = temp ; } } } printf ( "\nArray after sorting:\n"); n++; for ( i = 0 ; i <= n ; i++ ) printf ( "%d,", arr[i] ); getch(); } /* if we give no of ...

C Program to find Fibonacci Series, Fibonacci Series using Recursion Function in C

C Program to find Fibonacci Series, Fibonacci Series using Recursion Function in C : Example 1 : How to Write Fibonacci Series using Recursion Function in C with easy logic is given below :- #include<stdio.h> #include<conio.h> fibonacciSeries(int); void main() { int n,r,i; clrscr(); printf("Enter the number of elements to be printed in Fibonacci Series :"); scanf("%d",&n); printf("\n"); for(i=0 ; i < n ; i++) { r=fibonacciSeries(i); printf("%d,",r); } getch(); } fibonacciSeries(int n) { int f; if((n==1)||(n==2)) return(1); else if(n==0) return(0); else f=fibonacciSeries(n-2)+fibonacciSeries(n-1); return(f); } /* if enter 7 then output will be 0,1,1,2,3,5,8, */ Example 2 : How to Write Fibonacci Series in C upto a limit is given below :- #include<stdio.h> #include<conio.h> void main() { int n,a=0,b=1,c=0; clrscr(); printf("Enter a limit :"); scanf("%d",...

C Program to Convert Decimal to Binary, Easy Logic to Convert Decimal to Binary in C Language

C Program to Convert Decimal to Binary, Easy Logic to Convert Decimal to Binary in C Language : Example : How to Convert Decimal Number to Binary in C with explanations given below :- #include<stdio.h> #include<conio.h> void main() { int q[50],i,a,n,count=0; clrscr(); printf("\nEnter a Decimal Number :"); scanf("%d",&n); for(i=0 ; n>0 ; i++) { a = n%2; /* storing remainter part, eg a=7%2 then a=1 ie 3. 1 */ q[i] = a; n = n/2; /* storing integer part, eg a=7/2 then a=3 ie 3 .1 */ count++; } printf("\nThe Conversion is given below :\n"); for(i=0 ; i < count ; i++) printf("%d",q[i]); getch(); } /* if we enter a decimal number as 7 the result will be 111, if 5 then 101 */

C Program to find Prime Number using UDF, also find Prime Numbers upto a limit in C Language

C Program to find Prime Number using UDF, also find Prime Numbers upto a limit in C Language: Example 1 : C Program to find prime number using user defined function :- #include<stdio.h> #include<conio.h> int primeNumber(int); /* function declaration or prototype, int arg int returns */ void main() { int num,prime; clrscr(); printf("Enter an integer Number :"); scanf("%d",&num); prime = primeNumber(num); /* function call here with Actual Parameter num, rtns integer value and assigning to the var prime */ if(prime==1) printf("%d is a prime number",num); else printf("%d is not a prime number",num); getch(); } int primeNumber(int n) /* function definition with Formal Parameter n and returns an integer */ { int i; for( i=2 ; i<=n/2 ; i++) { if(n%i==0) return 0; } return 1; } /* prime number 3,7,11,17.... */ Example 2 : C Program to find prime num...

How to create User Defined Functions in C, see example Perfect_Number() function used as UDF in C Program

How to create User Defined Functions in C, see example Perfect_Number() function used as UDF in C Program :- Example : This is a program to find Perfect Numbers upto a limit using User Defined Function(UDF) in C Program Language :- #include<stdio.h> #include<conio.h> int Perfect_Number(int); /* UDF function prototype declaration : int argument and int returns */ main() { int lmt,num; clrscr(); printf("Enter a Limit :"); scanf("%d",&lmt); for(num=5 ; num<=lmt ; num++) { /* function calls here with Actual Parameter num and returns either 1 or 0 and checks */ if(Perfect_Number(num)==1) { printf("\n%d",num); } } getch(); } /* Either the function should be defined out side the main() or it should be in one of the header files included. function definition with a Formal Parameter as n */ int Perfect_Number(int n) { int a,sm; sm=0; for(a=1 ; a<n ; a++) { if(n%a==0) { sm=sm+a; } } if(sm==n) return 1; else ...

How to Compile and Run CGI Scripts in C Examples in Linux Apache Server

How to Compile and Run CGI Scripts in C Examples in Linux Apache Server :- first let us create an HTML file(testcgi.html) : it can be made using keditor, geditor or vi editor Enter First Number : Enter Second Number : use form action http://localhost/cgi-bin/sum.cgi then we have to make a C Program (sum.c): #include<stdio.h> #include<stdlib.h> int main(void) { char *data; long m,n; printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10); printf(" Result \n"); printf(" HTML Form values are added... \n"); data = getenv("QUERY_STRING"); if(data == NULL) printf("Error! Error in passing data from form to script."); else if(sscanf(data,"m=%ld&n=%ld",&m,&n)!=2) printf("Error! Invalid data. Data must be numeric."); else printf("The Sum of %ld and %ld is %ld.",m,n,m+n); return 0; } To Compile: in linux login root user and go to command line terminal root# cc sum.c...

How to create a Header File in C Language or In C How to make User Defined Header Files

How to create a Header File in C Language or In C How to make User Defined Header Files :- First we have to install or copy Turbo C++ in our computer. If we copy the TC folder, which should be copied into the same Drive as the Source Computer Drive, otherwise an Error will occur see more . Any way let us guess the C Program in "C:\TC\BIN\TC.EXE" then usually the Header files residing in "C:\TC\INCLUDE\" directory. our working directory is "C:\TC\BIN\" Let us try How to create a "Hello World!" Program with clrscr() SDF :- #include<stdio.h> #include<conio.h> void main() { clrscr(); printf("Hello World!"); } in the above program, we used clrscr() as a System Defined Function , because it is defined in conio.h and it is included. Let us try How to create a "Hello World!" Program with clrscr() as UDF and User Defined Header File :- Step 1: fist let us make a header file named "my_header_file.h" and i...

C Program for Matrix Multiplication or Matrix Multiplication in C Example Source Code

C Program for Matrix Multiplication or Matrix Multiplication in C Example Source Code given below: Matrix Multiplication Example 1 :- #include<stdio.h> #include <conio.h> #include <stdlib.h> main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; clrscr(); // first clear the screen printf("Enter the number of rows and columns of first matrix\n"); scanf("%d%d",&m,&n); printf("Enter the number of rows and columns of second matrix\n"); scanf("%d%d",&p,&q); if ( n != p ) { printf("Sorry! Matrix Multiplication can't be done, study row col combination of Matrices...\n"); getch(); // uses wait comand and in conio.h exit(0); // to exit program and in stdlib.h } printf("Enter the elements of first matrix\n"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) ...

How to Reverse a String in C without using C Reverse String function and using either Pointers or Recursive Function.

C Program to Reverse a String without using C Reverse String function (strrev() in string.h) and using either Pointers or Recursive Function is given below:- 1) C Example program to Reverse a String using pointers : #include<stdio.h> int strLength(char*); // user defined function declaration or function prototype void strReverse(char*); // UDF declr or fun proto main() { char str[100]; printf("Enter a String to be Reversed :"); gets(str); // using gets() instead of scanf() because scanf() does not take space char strReverse(str); // function with argument as string variable str calls here printf("Reverse of given String is \"%s\".\n", str); return 0; } void strReverse(char *str) // strReverse() function defines here { int length, c; char *begin, *end, temp; length = strLength(str); // function strLength() calls here and which returns integer value to be stored in "length" varia...