Shift Operations in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:32:55

1K+ Views

ProblemWhat is the simple program to show the left, right shifts, and complement of a number by using C language?SolutionLeft ShiftIf the value of a variable is left-shifted one time, then its value gets doubled.For example, a = 10, then a1 = 5ExampleFollowing is the C program for the shift operations − Live Demo#include main (){    int a=9;    printf("Rightshift of a = %d",a>>1);//4//    printf("Leftshift of a = %d",a2);//2//    printf("Leftshift by 2 of a = %d",a

Delete Vowels from a Given String using C Language

Bhanu Priya
Updated on 08-Mar-2021 10:31:28

5K+ Views

The logic we use to implement to delete the vowels from the given string is as follows −for(i=0; i

Use Pre-defined Mathematical Functions in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:29:13

326 Views

ProblemHow to find the cube root of any given number by using the C programming language?SolutionAlgorithmStep 1: Enter any number at run time Step 2: Read from console Step 3: Compute result         Result:pow(number, 1.0/3.0) Step 4: Increment result Step 5: Print resultExampleFollowing is the C program to find the cube root of any given number −//finding cube root of given number// #include #include #include void main(){    int number, result;    printf("Enter any number: ");    scanf("%d", &number);    result=pow(number, 1.0/3.0);    result++;    printf("\Cube of %d is: %d", number, result);    getch(); }OutputWhen the above ... Read More

Input and Output for Strings in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:27:24

5K+ Views

An array of characters (or) collection of characters is called a string.Input and output for stringsExampleFollowing is the C program for input and output for strings −#include main ( ){    char a[30];    printf("enter your name");    scanf ( "%s", a);    printf ("your name is %s", a);    getch ( ); }OutputWhen the above program is executed, it produces the following result −1. Enter your name : Lucky 2. Enter your name : Lucky Lol Your name is Lucky Your name is LuckyNote −‘&’ is not used for accepting strings because name of the string itself specifies the ... Read More

What is a String: Declare and Initialize Strings in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:26:10

7K+ Views

An array of characters (or) collection of characters is called a string.DeclarationRefer to the declaration given below −char stringname [size];For example - char a[50]; a string of length 50 characters.InitializationThe initialization is as follows −Using single character constant −char string[20] = { ‘H’, ‘i’, ‘l’, ‘l’, ‘s’ ,‘\0’}Using string constants −char string[20] = "Hello":;‘\0’ is called a null character. It marks the end of the string.‘\0’ is automatically placed by the compiler, if a string is given as input. The user has to take care of placing ‘\0’ at the end if a single character is given.Accessing − There is ... Read More

C Program to Find Fibonacci Series for a Given Number

Bhanu Priya
Updated on 08-Mar-2021 10:23:36

3K+ Views

Fibonacci Series is a sequence of numbers obtained by adding the two previous numbers.Fibonacci series starts from two numbers f0 & f1.The initial values of fo & f1 can be taken 0, 1 or 1, 1 Fibonacci series satisfies the following conditions −fn = fn-1 + fn-2AlgorithmRefer to the algorithm for the Fibonacci series.START Step 1: Read integer variable a, b, c at run time Step 2: Initialize a=0 and b=0 Step 3: Compute c=a+b Step 4: Print c Step 5: Set a=b, b=c Step 6: Repeat 3 to 5 for n times STOPExampleFollowing is the C program for the ... Read More

Count Vowels and Consonants in a String in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:19:18

3K+ Views

ProblemHow to write a C program to count numbers of vowels and consonants in a given string?SolutionThe logic that we will write to implement the code to find vowels and consonants is −if(str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U'||str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' )If this condition is satisfied, we try to increment the vowels. Or else, we increment the consonants.ExampleFollowing is the C program to count the number of vowels and consonants in a ... Read More

Structure Declaration in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:15:03

13K+ Views

The structure is a collection of different datatype variables, grouped together under a single name. It is a heterogeneous collection of data items that share a common name.Features of structureIt is possible to copy the contents of all structural elements of different data types to another structure variable of its type by using an assignment operator.To handle complex datatypes, it is possible to create a structure within another structure, which is called nested structures.It is possible to pass an entire structure, individual elements of structure, and address of structure to a function.It is possible to create structure pointers.The general form ... Read More

Find a Leap Year Using C Language

Bhanu Priya
Updated on 08-Mar-2021 10:09:07

410 Views

Leap year is a year that consists of 366 days. For every four years, we will experience a leap year.The logic we will implement to find whether the given year by the user through the console is a leap or not −if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0)))If this condition is satisfied, then the given year is a leap year. Otherwise, it is not.ExampleFollowing is the C program to check leap year with the help of If condition − Live Demo#include int main(){    int year;    printf("Enter any year you wish ... Read More

Display Complete Text One Word Per Line in C Language

Bhanu Priya
Updated on 08-Mar-2021 10:06:44

1K+ Views

First, open the file in write mode. Later on, enter the text until it reaches the End Of File (EOF) i.e. press ctrlZ to close the file.Again, open in reading mode. Then, read words from the file and print each word in a separate line and close the file.The logic we implement to print one word per line is as follows −while ((ch=getc(fp))!=EOF){    if(fp){       char word[100];       while(fscanf(fp, "%s", word)!=EOF) // read words from file{          printf("%s", word); // print each word on separate lines.       }     ... Read More

Advertisements