Factorial in C using Character array

Hey Guys, wondering why I am here once again with this “factorial” thing? You will get to know it in no time..just keep reading! Let’s calculate some of the factorials: 1! = 1 2! = 2 3! = 6 4! = 24 5! = 120 6! = 720 well there is no problem, isn’t it? Continue then… 7! = 5040 8! = 40320 Now let’s look at 21! It is 51090942171709440000...

June 15, 2016 · 5 min · Swati Kesarwani

Factorial in C

Hey! Today i am gonna show you how to calculate factorial of a number. The code below depicts the basic methodology of how to calculate factorial of a number. # include<stdio.h> void factorial(int number){ int i; long long int factorial=1; for ( i = number ; i > 0 ; i-- ) factorial*=i; // Printing the factorial of the number **/ printf("Factorial of %d = %lld",number,factorial); return 0; } int main(){ // Declaring variables **/ int num; // Asking and scanning input **/ printf("Enter a number : "); scanf("%d",&num); // Passing number to the function **/ factorial(num); return 0; } output of above code will be...

June 1, 2016 · 1 min · Swati Kesarwani

Linked List in C

First let’s get familiar with some basic terminologies used in linked list. A linked list is a type of array in which each elements points to the next element. Each element in the list is called a node The very first node of the list is called the root node or head of the list. Linked list is represented only by the root node i.e., we store only the pointer to the root node....

November 23, 2015 · 8 min · Zeeshan Khan

Faster Input Scan Method in C

Sometimes we need to scan input really fast like when we are participating in coding competitions like codechef or SPOJ where the time limit is very strict and inputs are large, we can save some time by adopting faster scanning methods. We can achieve our gaol using getchar_unlocked function. code below scans 100000 integers and has two different methods one is scan_normal() this will scan integers using standard scanf() function and the other one is scan_fast() this scan integers using getchar_unlocked function, we will execute the program twice one for each method mark the time taken for execution to evaluate the performance....

January 26, 2015 · 2 min · Zeeshan Khan

File Handling in C

The following program demonstrate how we to perform read and write operations on files, We can create and open a file w+ (write plus) mode, this will open a file if it exists otherwise create a new file and open it in write mode ...

January 3, 2015 · 1 min · Zeeshan Khan

Dynamic array in C

Dynamic array is a array that has a fixed number of items on it, but the space required for data storage is alloted at runtime on data input. The code below will demonstrate how we can use dynamic array in C # include<stdio.h> # include<stdlib.h> int main() { int *arr[10], i; printf("Enter 10 numbers\n"); for(i = 0; i < 10; i++){ //asking for space to store an int arr[i] = (int*) malloc(sizeof(int)); scanf("%d", arr[i]); } printf("the array you entered is "); for(i = 0; i < 10; i++){ printf("%d ", *arr[i]); //returning memory to system free(arr[i]); } printf("\n"); return 0; } The above code will produce the following output...

January 2, 2015 · 1 min · Zeeshan Khan

Pointers in C

Pointers are variables that stores the reference (address) of a data value, datatype of pointers represent what kind of values they can hold reference of, however, a pointer of type void can hold reference of any datatype, but explicitly type casting is required to use them. The code below will demonstrate how we can use pointers in C # include<stdio.h> int main(){ int *pnum, num; printf("Enter a number\n"); scanf("%d", &num); pnum = &num; // now you can modify the value from pnum as well as num....

January 2, 2015 · 1 min · Zeeshan Khan

Array in C

Arrays in C are collection of values that have same data types. The code below initializes and assigns values to an each position in an array # include<stdio.h> int main(){ int arr[10]; int i; printf("Enter 10 numbers\n"); for(i=0;i<10;i++){ scanf("%d",&arr[i]); } printf("the array you entered is\n"); for(i=0;i<10;i++){ printf("%d ",arr[i]); } printf("\n"); return 0; } Output: Enter 10 numbers 1 2 3 4 5 6 7 8 9 10 the array you entered is 1 2 3 4 5 6 7 8 9 10 '''

January 1, 2015 · 1 min · Zeeshan Khan

Bitwise operations in C

Bitwise operators in C are & AND Operator | OR Operator ~ NOT Operator ^ XOR Operator The code below demonstrate common bitwise operations in c # include<stdio.h> int main(){ int a, b, option, res; printf("Enter a and b\n"); scanf("%d %d", &a, &b); do{ printf("MENU\n 1.AND\n 2.OR\n 3.NOT \n 4.XOR\n 0.Exit\n"); printf("Enter choice: "); scanf("%d", &option); switch(option){ case 1: res = a & b; printf("%d AND %d = %d\n", a, b, res); break; case 2: res = a | b; printf("%d OR %d = %d\n", a, b, res); break; case 3: res = ~a; printf("NOT of %d = %d\n", a, res); res = ~b; printf("NOT of %d = %d\n", b, res); break; case 4: res = a ^ b; printf("%d XOR %d = %d\n", a, b, res); break; default: break; } printf("\n"); }while(option); return 0; } Output:...

January 1, 2015 · 1 min · Zeeshan Khan

Swap Two Numbers in C

Code below swaps two numbers without using a third variable # include<stdio.h> int swap(int* a, int *b){ // swapping values using match trick *a = *a + *b; *b = *a - *b; *a = *a - *b; return 0; } int main(){ int a, b; printf("Enter two numbers"); scanf("%d %d", &a, &b); printf("a=%d, b=%d", a, b); swap(&a, &b); printf("a=%d, b=%d", a, b); return 0; } Output: Enter two numbers 10 34 before swapping a=10, b=34 After swapping a=34, b=10

December 30, 2014 · 1 min · Zeeshan Khan