Found 1401 Articles for C

Write a C program to reverse array

Bhanu Priya
Updated on 15-Mar-2021 15:04:15

3K+ Views

An array is a group of related items that store with a common name.SyntaxThe syntax is as follows for declaring an array −datatype array_name [size];InitializationAn array can also be initialized at the time of declaration −int a[5] = { 10, 20, 30, 40, 50};Reversing array in CWe can reverse array by using swapping technique.For example, if 'P' is an array of integers with four elements −P[0] = 1, P[1] = 2, P[2] = 3 and P[3]=4Then, after reversing −P[0] = 4, P[1] = 3, P[2] = 2 and P[3]=1ExampleFollowing is the C program to reverse an array −#include int ... Read More

What are the predefined functions in C language?

Bhanu Priya
Updated on 15-Mar-2021 15:01:55

8K+ Views

Functions are broadly classified into two types, which are as follows −Predefined functionsUser defined functionsPredefined (or) library functionsThese functions are already defined in the system libraries.Programmer will reuse the already present code in the system libraries to write error free code.But to use the library functions, user must be aware of syntax of the function.Example −sqrt() function is available in math.h library and its usage is −y= sqrt (x) x number must be positive eg: y = sqrt (25) then ‘y’ = 5printf ( ) present in stdio.h library.clrscr ( ) present in conio.h library.ExampleGiven below is the C program ... Read More

Explain the concept of Arithmetic operators in C language

Bhanu Priya
Updated on 15-Mar-2021 10:26:02

2K+ Views

It is used to perform the arithmetic operations like addition, subtraction etc.OperatorDescriptionExamplea=20, b=10Output+Additiona+b20+1030-subtractiona-b20-1010*multiplicationa*b20*10200/Divisiona/b20/102(quotient)%Modular Divisiona%b20%100 (remainder)AlgorithmFollow the algorithm mentioned below −START Step 1: Declare integer variables. Step 2: Read all variables at runtime. Step 3: Perform arithmetic operations.    i. a+b    ii. a-b    iii. a*b    iv. b/a    v. a%b Step 4: Print all computed values.ProgramFollowing is the C program to compute arithmetic operators −#include main (){    int a, b;    printf("enter a, b:");    scanf("%d%d", &a, &b);    printf("a+b=%d", a+b);    printf("a-b=%d", a-b);    printf("a*b=%d", a*b);    printf("b/a=%d", b/a);    printf("a%b=%d", a%b); }OutputYou will see the ... Read More

C Program to reverse a given number using Recursive function

Bhanu Priya
Updated on 15-Mar-2021 10:21:05

11K+ Views

"Recursive function" is something which calls itself again in the body of the function.For example, A function fact ( ), which computes the factorial of an integer ‘N’, which is the product of all whole numbers from 1 to N.fact ( ) with an argument of 1 (or) 0, the function returns 1. otherwise, it returns n*fact (n-1), this happens until ‘n’ equals 1.Fact (5) =5* fact (4)    =5*4*3* fact (3)    =5*4*3*2* fact (2)    =5*4*3*2*1 fact (1)    =5*4*3*2*1    = 120.ExampleFollowing is the C program for use of recursive function to reverse a number −#include main ... Read More

What are different pointer operations and problems with pointers in C language?

Bhanu Priya
Updated on 15-Mar-2021 10:13:21

2K+ Views

A pointer is a variable whose value is the address of an another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.Consider the following statement −int qty = 179;The representation of the variable in memory is as follows −You can declare a pointer as follows −Int *p;It means ‘p’ is a pointer variable that holds the address of another integer variable.Address operator (&) is used to initialize a pointer variable.For Example −int qty = 175; int *p; p= &qty;To access the value of ... Read More

State the difference between memcmp and memicmp functions in C language

Bhanu Priya
Updated on 15-Mar-2021 09:58:55

283 Views

Memcmp() and memicmp() compares first n bytes of two blocks of memory.memcmp() performs comparison as unsigned characters.memicmp() performs comparison as characters but, ignore upper case or lower case letters.Both functions return an integer value.Two memory buffers are equal (returns 0).First buffer is greater than second (returns >0).First buffer is less than second(returns0)       printf("buffer st1 is bigger than buffer st2");    if(x

How to swap two numbers without using the third or a temporary variable using C Programming?

Bhanu Priya
Updated on 15-Mar-2021 09:38:38

3K+ Views

With the help of addition and subtraction operations, we can swap two numbers from one memory location to another memory location.AlgorithmThe algorithm is explained below −STARTStep 1: Declare 2 variables x and y. Step 2: Read two numbers from keyboard. Step 3: Swap numbers. //Apply addition and subtraction operations to swap the numbers.    i. x=x+y    ii. y=x-y    iii. x=x-y Step 4: Print x and y values.ProgramFollowing is the C program which explains swapping of two numbers without using third variable or a temporary variable −#include int main(){    int x, y;    printf("enter x and y values:"); ... Read More

How to print the range of numbers using C language?

Bhanu Priya
Updated on 13-Mar-2021 11:41:01

1K+ Views

ProblemFor given number try to find the range in which that number exists.SolutionHere, we are learning how to find the ranges of a number.The logic that we apply to find range is −lower= (n/10) * 10; /*the arithmetic operators work from left to right*/ upper = lower+10;ExplanationLet number n=45Lower=(42/10)*10 // division returns quotient       =4*10 =40Upper=40+10=50Range − lower-upper − 40-50ExampleFollowing is the C program for printing the range of numbers −#include main(){    int n, lower, upper;    printf("Enter a number:");    scanf("%d", &n);    lower= (n/10) * 10; /*the arithmetic operators work from left to right*/    upper ... Read More

Finding sum of first and last digit using divide and modulo operator in C language

Bhanu Priya
Updated on 13-Mar-2021 11:40:17

396 Views

ProblemWhat is the C program to obtain the sum of the first and last digit of the number, if a four-digit number is input through the keyboard?SolutionIn this program, we are taking a four-digit number at run time and trying to find the sum of first and last digit of that four-digit number by using the logic −a=n%10; b=n/1000; result = a + b;Let’s apply this logic to find sum of first and last digit of four-digit number −ExampleFollowing is the C program for finding sum of first and last digit by using divide and modulo operator −#include main(){   ... Read More

What are different operators and expressions used in C language?

Bhanu Priya
Updated on 11-Mar-2021 11:14:07

11K+ Views

  Operator performs an operation on data. They are classified into following −Arithmetic operators.Relational operators.Logical operators.Assignment operators.Increment and decrement operators.Bitwise operators.Conditional operators.Special operators.Arithmetic operatorThese operators are used for numerical calculations (or) to perform arithmetic operations like addition, subtraction etc.OperatorDescriptionExamplea=20, b=10Output+Additiona+b20+1030-subtractiona-b20-1010*multiplicationa*b20*10200/Divisiona/b20/102(quotient)%Modular Divisiona%b20%100 (remainder)ProgramFollowing is the C program for arithmetic operator −#include main ( ){    int a= 20, b = 10;    printf (" %d", a+b);    printf (" %d", a-b);    printf (" %d", a*b);    printf (" %d", a/b);    printf (" %d", a%b); }OutputWhen the above program is executed, it produces the following result −30 10 200 20Relational ... Read More

Advertisements