Function Return in C Language

Bhanu Priya
Updated on 08-Mar-2021 05:56:30

6K+ Views

A function is a self-contained block that carries out a specific task.The advantages of function in C language are as follows −ReusabilityThe length program can be reduced.It is easy to locate and isolate a wrong function.It facilitates top-down modular programming.ExampleFollowing is the C program for functions −#include /*Function prototypes*/ myfunc(); main(){    myfunc(); } /*Function Defination*/ myfunc(){    printf("Hello "); }Here, In calculations, we generally expect a function to return a value. But, it may or may not accept the arguments.This return value has a type int, float, char or anything else.Default type of function is integer.ExampleAnother program for the ... Read More

Print One Month Calendar Using For Loop in C

Mandalika
Updated on 06-Mar-2021 18:36:13

6K+ Views

The logic to print a one-month calendar is as follows −for(i=1;i

Calculate Sum of Random Numbers in C Programming

Bhanu Priya
Updated on 06-Mar-2021 16:35:57

2K+ Views

In this program, we are adding random numbers that are generated in between 0 and 100.After every runtime, the result of sum of random numbers is different, i.e., we get a different result for every execution.The logic we use to calculate the sum of random numbers in between 0 to 100 is −for(i = 0; i

Write a C Program Using time.h Library Function

Bhanu Priya
Updated on 06-Mar-2021 16:33:29

388 Views

ProblemHow to display the current date and time in ISO standard format using C Programming language?SolutionThe current date and time of the input will be taken and we are trying to print the system time and date in ISO format.For example, Monday, Dec 15, 2020 10.50p.The built-in functions that we used in this program are −Time() − returns current time.Strftime() − converts the time to string form, this function include in time.h.Example Live Demo#include #include int main(){    time_t current = time(NULL);    char datetime[20];    strftime(datetime, sizeof(datetime), "%a, %d%b%y %H:%M", localtime(¤t));    puts(datetime);    return 0; }OutputThu, 31 Dec 20 ... Read More

Calculate Interest Amount Using Formula in C Language

Bhanu Priya
Updated on 06-Mar-2021 16:17:24

932 Views

ProblemWrite a C program to calculate the deposited amount incremented after some years with interestSolutionThe formula for calculating interest is −M=((r/100) * t); A=P*exp(M);Where r= rate of interest            t=no. of years            P=amount to be deposited            M=temporary variable            A= Final amount after interestAlgorithmSTART Step 1: declare double variables Step 2: read amount to be deposited Step 3: read rate of interest Step 4: read years you want to deposit Step 5: Calculate final amount with interest          I. ... Read More

Extract First Sub-Element from a List in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 14:15:37

14K+ Views

To extract only first element from a list, we can use sapply function and access the first element with double square brackets. For example, if we have a list called LIST that contains 5 elements each containing 20 elements then the first sub-element can be extracted by using the command sapply(LIST,"[[",1).Example1Consider the below data frame − Live DemoList1

Find Monthly Average from Different Date Data in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 14:15:10

2K+ Views

To find the monthly average from different date data, we first need to extract the months and year from date column and then find the average using aggregate function. For example, if we have a data frame called df which has three columns say x, month, and year then the monthly average can be found by using the command −aggregate(x~Month+Year,df,mean)Example1Consider the below data frame − Live DemoDate1

Change Code 'Yes' to 1 in R Data Frame Column

Nizamuddin Siddiqui
Updated on 06-Mar-2021 14:07:30

16K+ Views

To change the code “Yes” to 1, we can use ifelse function and set the Yes to 1 and others to 0. For example, if we have a data frame called df that contains a character column x which has Yes and No values then we can convert those values to 1 and 0 using the command ifelse(df$x=="Yes",1,0).Example1Consider the below data frame − Live DemoAgree

Count Particular Character in String Vector in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 14:02:00

194 Views

To find the count of a particular character in a string vector we can use nchar function along with gsub. For example, if we have a vector called x that contains string such India, Russia, Indonesia then we can find the number of times character i occurred then we can use the command nchar(gsub("[^i]","",x)) and the output will be 1 1 1 because first I’s in India and Indonesia will not be considered as they are in uppercase.Example1 Live Demox1

Find Mean Squared Error for Linear Model in R

Nizamuddin Siddiqui
Updated on 06-Mar-2021 14:01:33

4K+ Views

To find the mean squared error for linear model, we can use predicted values of the model and find the error from dependent variable then take its square and the mean of the whole output. For example, if we have a linear model called M for a data frame df then we can find the mean squared error using the command mean((df$y-predict(M))^2).Example1Consider the below data frame − Live Demox1

Advertisements