Found 1401 Articles for C

C Program for focal length of a lens

Sunidhi Bansal
Updated on 20-Dec-2019 11:20:43

264 Views

Given two floating values; image distance and object distance from a lens; the task is to print the focal length of the lens.What is focal length?Focal length of an optical system is the distance between the center of lens or curved mirror and its focus.Let’s understand with the help of figure given below −In the above figure i is the object, and F is the image of the object which is formed and f is the focal length of the image.So to find the focal length of the image from the lens the formula is −1F= 1O+1IWhere, F is the ... Read More

Code valid in both C and C++ but produce different output

Arnab Chakraborty
Updated on 17-Dec-2019 13:26:14

62 Views

Here we will see some program that will return different results if they are compiled in C or C++ compilers. We can find many such programs, but here we are discussing about some of them.In C and C++, the character literals are treated as different manner. In C, they are treated as int but in C++, they are treated as characters. So if we check the size using sizeof() operator, it will return 4 in C, and 1 in C++.Example Live Demo#include int main() {    printf("The character: %c, size(%d)", 'a', sizeof('a')); }OutputThe character: a, size(4)Example#include int main() {    printf("The ... Read More

What are the differences between bitwise and logical AND operators in C/C++

Arnab Chakraborty
Updated on 17-Dec-2019 12:37:17

6K+ Views

As we know the bit-wise AND is represented as ‘&’ and the logical operator is represented as ‘&&’. There are some fundamental differences between them. These are as follows −The logical AND operator works on Boolean expressions, and returns Boolean values only. The bitwise AND operator works on integer, short int, long, unsigned int type data, and also returns that type of data.Example Live Demo#include using namespace std; int main() {    int x = 3; //...0011    int y = 7; //...0111    if (y > 1 && y > x)       cout

C Program Coin Change

sudhir sharma
Updated on 22-Nov-2019 09:28:52

3K+ Views

In this problem, we are given a value n, and we want to make change of n rupees, and we have n number of coins each of value ranging from 1 to m. And we have to return the total number of ways in which make the sum.ExampleInput : N = 6 ; coins = {1, 2, 4}. Output : 6 Explanation : The total combination that make the sum of 6 is : {1, 1, 1, 1, 1, 1} ; {1, 1, 1, 1, 2}; {1, 1, 2, 2}; {1, 1, 4}; {2, 2, 2} ; {2, 4}.Example#include ... Read More

C / C++ Program for Dijkstra's shortest path algorithm

sudhir sharma
Updated on 08-Nov-2023 00:13:33

24K+ Views

We are given a graph with a source vertex in the graph. And we have to find the shortest path from the source vertex to all other vertices of the graph. The Dijikstra's algorithm is a greedy algorithm to find the shortest path from the source vertex of the graph to the root node of the graph. Algorithm Step 1 : Create a set shortPath to store vertices that come in the way of the shortest path tree. Step 2 : Initialize all distance values as INFINITE and assign distance values as 0 for source vertex so that it is ... Read More

Binary to Gray code using recursion in C program

sudhir sharma
Updated on 09-Jul-2020 11:49:04

2K+ Views

A Binary number is a number that has only two bits 0 and 1.Gray code is a special type of binary number that has a property that two successive number of the code cannot differ more than one bit. This property of gray code makes it useful more K-maps, error correction, communication, etc.This makes the conversion of binary to gray code necessary. So, let’s see an algorithm to convert binary to gray code using recursion.ExampleLet’s take an example of gray code coInput : 1001 Output : 1101AlgorithmStep 1 : Do with input n :    Step 1.1 : if n ... Read More

Binary Search for Rational Numbers without using floating point arithmetic in C program

sudhir sharma
Updated on 22-Nov-2019 09:16:45

164 Views

In this problem, we are given a sorted array of rational numbers. and we have to search the given element using binary search algorithm for this rational number array without using floating point arithmetic.A Rational number is number represented in the form p/q where both p and q are integers. For example, ⅔, ⅕.Binary search is searching technique that works by finding the middle of the array for finding the element.for finding the element using binary search from a sorted array of rational numbers, where floating point arithmetic are not allowed. We will compare the numerators and denominators to find ... Read More

C program to delete a file

sudhir sharma
Updated on 22-Nov-2019 07:40:35

2K+ Views

In programming, working with files is very important and every programming language has its own set of functions or library that help in manipulation of files.In C Programming Language also there is a function remove which can be used by the programmer to delete a file.remove() function in c programmingThe remove function is used to delete the file whose name is specified. This function is in the stdio header file.Syntaxremove (“file_name”);ParametersThe function accepts one parameter which is the name of the file that is to be deleted.File name can also be the path to the file but only if the ... Read More

C program to find the Roots of Quadratic equation

Ayush Gupta
Updated on 09-Jul-2020 08:55:12

2K+ Views

In this tutorial, we will be discussing a program to find the roots of the Quadratic equation.Given a quadratic equation of the form ax2 + bx + c. Our task is to find the roots x1 and x2 of the given equation.For this, we are using the deterministic method, in thisD = √b2 - 4acthen the roots of the equation will bex1 = (-b + D)/2a ,andx2 = (-b - D)/2aExample#include #include #include //calculating the roots of equation void calc_roots(int a, int b, int c) {    if (a == 0) {       printf("Invalid Equation");       ... Read More

C Program for Hexagonal Pattern

Sunidhi Bansal
Updated on 21-Nov-2019 12:28:19

1K+ Views

We are given with an integer ‘n’ and the task is to generate the hexagonal pattern and display the final output.ExampleInput-: n=5 Output-:Input-: n = 4 Output-:Approach we are using in the given program is as follows −Input the number ‘n’ from userDivide the entire pattern into three parts i.e. upper part, middle part and lower part Start loop i for printing the upper part of the pattern from i to 0 and i to be less than n and keep incrementing the value of i Start loop m for printing the middle part of the pattern from m to ... Read More

Advertisements