Practice Programs
- Doing Programs gives you the great understanding about what you have learned.
- Doing Programs gives you the Confidence.
- So let's Start by doing some programming.
- Q:Define function 'Reverse_number' which will take integer number as an argument and it should perform operation of reversing the number and return that reverse number.
Solution:
#include<stdio.h> int Reverse_number(int n) //declaration. int Reverse_number(int n)//definition. { int i,j=0; while(n!=0) { i = n%10; j = (j*10) + i; n = n/10; } return j; }
Using above function:
void main() { int result; //calling function 'Reverse_number'. result = Reverse_number(564);//formal parameters printf("\nReverse number is %d",result); }
Output:
Reverse number is 465.Exercise:
Define function 'is_palindrome' which will take integer number as an argument and it should perform operation of checking whether number is palindrome or not,And return 1 if palindrome otherwise return 0. - Q:Define function 'is_prime' which will take integer number as an argument and it should perform operation of checking that number is prime or not , return 1 if prime otherwise return 0.
Solution:
int is_prime(int n) { int i,flag; for(i=2 ;i<n ; i++) { if(n%i == 0) { break; } } if(i == n) flag = 1; else flag = 0; return flag; }
Using above function:
void main() { int result; //calling function 'is_prime'. result = is_prime(1); if(result == 1) printf("\nNumber is a prime number."); else printf("Number is not an prime number."); }
Output:
Number is not an prime number.Exercise:
Define function 'is_even' which will take integer number as an argument and it should perform operation of checking whether number is even or odd,And return 1 if even otherwise return 0. - Q:Define function 'sort_asc' which will take integer array and number of element in an array as an argument and it should perform operation of sorting the array in ascending order.
Solution:
#include<stdio.h> void sort_Asc(int arr[],int n) ;//decleration void sort_Asc(int arr[],int n) //definition { int i,j,temp; for(i=0;i<n;i++) { for(j=i+1; j<n; j++) { if(arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } }
Using above function:
void main() { int i; int a[10] = {10,9,8,7,6,5,4,3,2,1}; printf("\nUnsorted array is :\n"); for(i=0;i<10;i++) { printf("%d\t",a[i]); } //calling function 'sort_Asc' sort_Asc(a,10); printf("\nArray in ascending array is :\n"); for(i=0;i<10;i++) { printf("%d\t",a[i]); } }
Output:
Unsorted array is :
10 9 8 7 6 5 4 3 2 1
Array in ascending array is :
1 2 3 4 5 6 7 8 9 10Exercise:Define function 'sort_desc' which will take integer array and number of element in an array as an argument and it should perform operation of sorting the array in descending order.
- Q:Define function 'dict_ord' which will take character array as an argument and it should perform operation of sorting the array in dictionary order.
Solution:
void dict_ord(char s[]) { int i,j; char ch; for(i=0; s[i]!='\0'; i++) { for(j=i+1; s[j]!='\0'; j++) { if(s[i] > s[j]) { ch = s[i]; s[i] = s[j]; s[j] = ch; } } } }
Using above function:
void main() { int i; char a[10] = "zaheer"; printf("\nUnsorted letters :\n"); for(i=0;a[i]!='\0';i++) { printf("%c\t",a[i]); } //calling function 'dict_ord' dict_ord(a); printf("\nletters in Dictionary order are:\n"); for(i=0;a[i]!='\0';i++) { printf("%c\t",a[i]); } }
Output:
Unsorted letters :
z a h e e r
letters in Dictionary order are:
a e e h rExercise: Define function 'non_dict_ord' which will take character array as an argument and it should perform operation of sorting the array in non-dictionary order.
- Q:Define function 'power' which will take two integer number 'a' and 'b'as an argument and it should perform operation of calculating 'a' raised to power of b by 'Recursion'and return the result.
Solution:
#include<stdio.h> int power(int a ,int n);//decleration int power(int a ,int n) //definition { if(n == 1) return a; else return (a*(power(a,n-1))); }
Using above function:
void main() { int result; //calling function 'power' result = power(2,5); printf("\n2 raised to 5 is %d",result); }
Output:
2 raised to 5 is 32.Exercise:
Define function 'product' which will take integer number 'a' as an argument and it should perform operation of calculating product of n numbers by 'Recursion' and return the result.
Further Topics:
- What are Pointers?
- how to declare pointers?
- how to initialize a pointers?
- How to access the pointer variables?
- Function of '*' and '&' operators
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- How to declare Structure Pointer?
- Working with structure Pointer
- How to Create Structure Variable Dynamically ?
- Creating structure variable through malloc() and calloc()
People Also Searched:
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() functions
0 Comments
Post a Comment