# C# Programming Exercises

As we all know, C is a general-purpose, imperative computer programming language that supports recursion, structured programming, and lexical variable scope. **We will be looking at some easy and functional exercises most used in C programming.**

## Return a 'struct' from a function in C

You can return a structure from a function in C very smoothly. A structure is very similar to a built-in type for parameters passing, assignment, and return values and can quickly be given as functions. The drawback with struct b=a you don’t provide a complete type, struct MyObj b= a will work just fine. Let us look at an example,

#include <stdio.h>#include <stdlib.h>

enum {DAY = 9, MONTH = 2};

typedef struct {int day;int month;} MyStruct;

MyStruct clearMyStruct(MyStruct *st){st->day = 0;st->month = 0;

return *st;}

int setMyStruct(MyStruct *st, int d, int m){if (!st)return -1;st->day = d;st->month = m;return 0;}

int main() {MyStruct tmp;

if (setMyStruct(&tmp, DAY, MONTH) == -1 )exit(EXIT_FAILURE);

printf("day: %d\nmonth: %d\n", tmp.day, tmp.month);clearMyStruct(&tmp);printf("day: %d\nmonth: %d\n", tmp.day, tmp.month);

exit(EXIT_SUCCESS);}

## C program to add two numbers

This is an essential exercise to find the sum of two numbers using C. The basic knowledge required is data types, input/output operations, and arithmetic operations. Users input two numbers in the program and obtain the sum as a result, for example,

/*** C program to find the sum of two numbers*/

#include <stdio.h>

int main(){int num1, num2, sum;

/** Read two numbers from the user*/printf("Enter first number: ");scanf("%d", &num1);printf("Enter second number:");scanf("%d", &num2);

/* Adding both number is simple and fundamental */sum = num1 + num2;

/* Prints the sum of two numbers */printf("Sum of %d and %d = %d", num1, num2, sum);

return 0;}

## C program to find the power of a number using the power function

This exercise helps us find the power of a number and enables the user to understand how to use the power function in C. The required knowledge is data types, arithmetic operations, and primary input/output. Here is a simple example,

/*** C program to find the power of any number*/

#include <stdio.h>#include <math.h> // Used for pow() function

int main(){double base, expo, power;

/* Input two numbers from user */printf("Enter base: ");scanf("%lf", &base);printf("Enter exponent: ");scanf("%lf", &expo);

/* Calculates base^expo */power = pow(base, expo);

printf("%.2lf ^ %.2lf = %.2lf", base, expo, power);

return 0;}

## C program to find the square root of a number

This is an exercise to find the square root of a given number using the sqrt function. We will learn how to use a predefined sqrt function to find the square root of a number here. The knowledge required here is data types and essential input/output functions. Here is an example,

/*** C program to find the square root of a number*/

#include <stdio.h>#include <math.h>

int main(){double num, root;

/* Input a number from user */printf("Enter any number to find square root: ");scanf("%lf", &num);

/* Calculate square root of num */root = sqrt(num);

/* Print the resultant value */printf("Square root of %.2lf = %.2lf", num, root);

return 0;}

## C program to find the area of an equilateral triangle

This is a simple exercise to find out the area of an equilateral triangle using just one side given. As it is an equilateral triangle used, there is only one side needed to find the area of the triangle. The basic knowledge required is arithmetic operators, basic input/output, and variables and expressions.The C equivalent we are using here is,(sqrt(3) / 4) * (side * side)

/*** C program to find the area of an equilateral triangle*/

#include <stdio.h>#include <math.h> // Used for sqrt() function

int main(){float side, area;

/* Input side of equilateral triangle */printf("Enter side of an equilateral triangle: ");scanf("%f", &side);

/* Calculate area of equilateral triangle */area = (sqrt(3) / 4) * (side * side);

/* Print resultant area */printf("Area of equilateral triangle = %.2f sq. units", area);

return 0;}

## C program to find out the simple interest

This is a beneficial C exercise where we input P as a principle, R as a rate, and T is the time to find out simple interest for any given situation. The required knowledge is variables and expressions, data types, arithmetic operators, and basic input/output operations. Let us look at an example now,

/*** C program to calculate simple interest*/

#include <stdio.h>int main(){float principle, time, rate, SI;

/* Input principle, rate and time */printf("Enter principle (amount): ");scanf("%f", &principle);

printf("Enter time: ");scanf("%f", &time);

printf("Enter rate: ");scanf("%f", &rate);

/* Calculate simple interest */SI = (principle * time * rate) / 100;

/* Print the resultant value of SI */printf("Simple Interest = %f", SI);

return 0;}

## C program to convert temperature from Celsius to Fahrenheit.

This is a simple exercise to convert a temperature from degree centigrade to Fahrenheit. The required knowledge is arithmetic variables, data types, expressions, and actual input/output.

/*** C program to convert temperature from degrees Celsius to Fahrenheit*/

#include <stdio.h>

int main(){float Celsius, Fahrenheit;

/* Input temperature in celsius */printf("Enter temperature in Celsius: ");scanf("%f", &celsius);

/* celsius to fahrenheit conversion formula */fahrenheit = (celsius * 9 / 5) + 32;

printf("%.2f Celsius = %.2f Fahrenheit", celsius, fahrenheit);

return 0;}

## C program total, percentage, and an average of any five given subjects

In this exercise, we learn to use C logic to find the five subjects’ percentage, average, and total. The knowledge required is basic input/output operations, variables and expressions, data types, and arithmetic operators. An example is given below,

/*** C program to calculate the total, average, and percentage of five subjects*/

#include <stdio.h>

int main(){float eng, phy, chem, math, comp;float total, average, percentage;

/* Input marks of all five subjects */printf("Enter marks of five subjects: \n");scanf("%f%f%f%f%f", &eng, &phy, &chem, &math, &comp);

/* Calculate total, average and percentage */total = eng + phy + chem + math + comp;average = total / 5.0;percentage = (total / 500.0) * 100;

/* Print all results */printf("Total marks = %.2f\n", total);printf("Average marks = %.2f\n", average);printf("Percentage = %.2f", percentage);

return 0;}

## C program to convert centimeter to meter and kilometer

This is an exercise used to easily convert any given value in centimeters to meters and kilometers. The knowledge used in this length conversion exercise is basic input/output operations, arithmetic operators, data types variables, and expressions. Let us take a look at an example now,

/*** C program to convert centimeter into meter and kilometer*/

#include <stdio.h>

int main(){float cm, meter, km;

/* Input length in centimeter from user */printf("Enter length in centimeter: ");scanf("%f", &cm);

/* Convert centimeter into meter and kilometer */meter = cm / 100.0;km = cm / 100000.0;

printf("Length in Meter = %.2f m \n", meter);printf("Length in Kilometer = %.2f km", km);

return 0;}

**Other useful articles:**

- TOP-10 Basic C# Interview Questions and Answers
- TOP-10 C# Questions for Advanced Programmers
- 10 Important C# Interview Questions and Answers
- Most Asked C# Interview Q & A
- C# Interview for Beginners
- TOP-10 Advanced C# Interview Questions and Answers
- Introduction to Functions in C#
- C# Jagged Arrays
- C Sharp Interview Questions for 5 Years Experience
- C Sharp Interview Questions for Testers
- C Sharp Questions for 10 Years Experience
- Iteration Statements in C#
- Exception Handling in C#
- C Sharp Methods
- C# Data Types
- C# Vs. C++
- Regular Expressions in C Sharp
- Defining Nested Methods in C Sharp
- C Sharp Dynamic Data Type
- C Custom Exceptions
- How to Generate Random Numbers with C Sharp
- Pass by Value vs Pass by Reference in C Sharp
- C Sharp Inheritance
- C Sharp LINQ
- C Sharp Static Methods
- Polymorphism in C++
- C# Programming Online Courses
- C Sharp to VB. Net
- C# Programming Exercises