1.Program to Find Roots of Quadratic Equation
#include<stdio.h>
#include<math.h> // This is needed to use sqrt() function
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
float a, b, c, determinant, r1, r2, real, imag;
printf("\n\nEnter coefficients a, b and c: \n\n\n");
scanf("%f%f%f", &a, &b, &c);
/*
mathematical formula to know the
nature of the roots
*/
determinant == b*b - 4*a*c;
if(determinant > 0) // both roots are real
{
r1 = (-b + sqrt(determinant))/2*a; // Brackets are important
r2 = (-b - sqrt(determinant))/2*a;
printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
else if(determinant == 0) // both roots are real and equal
{
r1 = r2 = -b/(2*a); // brackets are important
printf("\n\n\nRoots are: %.2f and %.2f ", r1, r2);
}
/*
Determinant < 0 - both roots are imaginary of the
form real + i*imaginary
*/
else
{
real = -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("\n\n\nRoots are %.2f + i%.2f and %.2f - i%.2f ", real, imag, real, imag);
}
printf("\n\n\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
2.Program to Check for a Perfect Square.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
// variable declaration
int i, number;
// take user input
printf("Enter a number: ");
scanf("%d", &number);
// loop to check number is perfect square or not
for(i = 0; i <= number; i++)
{
if(number == i*i)
{
printf("\n\n\n\t\t\t%d is a perfect square\n\n\n", number);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0; // same as using break in this case to end the program
}
}
printf("\n\n\n\t\t\t%d is not a perfect square\n", number);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
3.Program to Calculate Permutation (nPr) and Combination (nCr).
program to calculate nPr and nCr.
Global declaration scope is within the complete program.
Local declarations scope is limited to the function only.
#include<stdio.h>
// function prototype declarations
long factorial(int);
long find_npr(int, int);
long find_ncr(int, int);
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int n, r;
long npr, ncr;
printf("Enter the value of n and r respectively: \n\n");
scanf("%d%d", &n, &r);
// function calls
npr = find_npr(n, r);
ncr = find_ncr(n, r);
printf("\n\n\n\t\t%dC%d = %ld\n", n, r, ncr);
printf("\n\n\t\t%dP%d = %ld\n", n, r, npr);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
/*
function definition for nCr
*/
long find_ncr(int a, int b)
{
return (factorial(a)/(factorial(b)*factorial(a-b)));
}
/*
function definition for nPr
*/
long find_npr(int a, int b)
{
return (factorial(a)/factorial(a-b));
}
/*
recursive function definition for finding
factorial of a number
*/
long factorial(int c)
{
if(c == 1 || c == 0)
return 1;
else
return c*factorial(c-1);
}
4.Program to shutdown Windows/Linux Shutdown Machine
This program turns off i.e shutdown your computer system. System function of stdlib.h is used to run an executable file shutdown.exe which is present in C:\WINDOWS\system32 folder in Windows 7 and XP.
program to shutdown Windows 7.
#include<stdio.h>
#include<stdlib.h> // to use system() method
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char ch;
printf("Do you want to shutdown your pc now (y/n)?");
scanf("%c", &ch);
if(ch == 'y'|| ch == 'Y')
{ /*
/s is used to order the compiler
to shutdown the PC
*/
system("C:\\WINDOWS\\System32\\shutdown /s");
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
You can use various options while executing shutdown.exe, for example you can use /t option to specify number of seconds after which the shutdown occurs.
Syntax: "shutdown /s /t x"; here x is the number of seconds after which shutdown will occur.
Example: By default shutdown occurs after 30 seconds. To shutdown immediately you can write "shutdown /s /t 0"
If you wish to restart your computer then you can use "shutdown /r".
Program to Shutdown Windows XP Machine
#include<stdio.h>
#include<stdlib.h> // to use system() function
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char ch;
printf("Do you want to shutdown the PC- (y/n) ?\n");
scanf("%c", &ch);
if(ch == 'y' || ch == 'Y')
{
system("C:\\WINDOWS\\System32\\shutdown -s");
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
To shutdown immediately use "C:\\WINDOWS\\System32\\shutdown -s -t 0". To restart use "-r" instead of "-s".
For better understanding go through the program for shutting down Windows 7, in which there is a detailed explanation of using t and r instead of s.
Note: A '-' performs the same function in Windows XP as that performed by '/' incase of Windows 7.
Program to Shutdown Linux OS
Program to shutdown Linux operating system.
#include<stdio.h>
#include<stdlib.h> // to use system() function
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char ch;
printf("Do you want to shutdown your pc now(y/n)?");
scanf("%c", &ch);
if(ch == 'y' || ch == 'Y')
system("shutdown -P now");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
You need to be logged in as user for above program to execute otherwise you will get the message shutdown: "Need to be root".
'-P' option specifies you want to power off your machine.
You can specify minutes as: shutdown -P "number of minutes"
For more help or options type at terminal: man shutdown
5.Program without a main() function
In the below program, main() function is there, but hidden using the preprocessors.
As you can see in the second line, #define decode() function is used, which holds a character combination of m,a,i,n and is followed by ##m##a##i##n.
Here ## operator is used to merge the characters in the order mentioned using ##, which is main
In the 3rd line #define go decode(m,a,i,n) as we have specified the characters in same order, the decode function will assign value main for go.
#include<stdio.h>
//Need to include the following statements in same manner
#define decode(m,a,i,n) m##a##i##n
#define go decode(m,a,i,n)
int go()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
printf("You have just executed your first program without making use of main() function!\n");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
We can use different words and combination here, like
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define go decode(a,n,i,m,a,t,e)
Here as per the first line, 4th, 1st, 3rd and 2nd chcracters have to be formed into a word. When the same combination is taken out from the word animate it makes main.
Using macro to define main
#include<stdio.h>
#define go main
int go(void)
{
printf("Welcome to Studytonight");
return 0;
}
This is is the simplest technique, where all we have done is provided our main() function with a different name, which is set the main before the program is executed.
Using Token-Pasting operator
#include<stdio.h>
#define go m##a##i##n
int go(void)
{
printf("Welcome to Studytonight");
return 0;
}
6.Program to create a Menu Driven software using Switch Case
menu driven program using switch case.
unsigned is doubled the size of signed as it only considers positive values.
%lu is the format specifier for unsigned integer.
A number divisible by an integer other than 1 and itself is called a composite number.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
int choice, num, i;
unsigned long int fact;
while(1)
{
printf("1. Factorial \n");
printf("2. Prime\n");
printf("3. Odd\\Even\n");
printf("4. Exit\n\n\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter number:\n");
scanf("%d", &num);
fact = 1;
for(i = 1; i <= num; i++)
{
fact = fact*i;
}
printf("\n\nFactorial value of %d is = %lu\n\n\n",num,fact);
break;
case 2:
printf("Enter number:\n");
scanf("%d", &num);
if(num == 1)
printf("\n1 is neither prime nor composite\n\n");
for(i = 2; i < num; i++)
{
if(num%i == 0)
{
printf("\n%d is not a prime number\n\n", num);
break;
}
}
/*
Not divisible by any number other
than 1 and itself
*/
if(i == num)
{
printf("\n\n%d is a Prime number\n\n", num);
break;
}
case 3:
printf("Enter number:\n");
scanf("%d", &num);
if(num%2 == 0) // 0 is considered to be an even number
printf("\n\n%d is an Even number\n\n",num);
else
printf("\n\n%d is an Odd number\n\n",num);
break;
case 4:
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
exit(0); // terminates the complete program execution
}
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
7.program to Change the Text Background Color
Few important points regarding this program are:
SetConsoleTextAttribute: Sets the attributes of characters written to the console screen buffer by the WriteFile or WriteConsole function, or echoed by the ReadFile or ReadConsole function.
This function affects text written after the function call.
Syntax:
BOOL WINAPI SetConsoleTextAttribute(_In_ HANDLE hConsoleOutput , _In_ WORD wAttributes);
For more Details, you may visit the Official Documentation here :
https://docs.microsoft.com/en-us/windows/console/setconsoletextattribute
Below is the program for changing text background color.
#include<windows.h>
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
//BACKGROUND_RED| BACKGROUND_GREEN| BACKGROUND_BLUE| BACKGROUND_INTENSITY
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE|BACKGROUND_RED|BACKGROUND_INTENSITY);
printf("\n\nStudytonight just showed you how to put colors to your code!!");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_GREEN);
printf("\n\nIsn't this Awesome?");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_INTENSITY|BACKGROUND_RED);
printf("\n\nYou just did something that only 1 out of 10 coders are familiar of :)\n");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_GREEN|BACKGROUND_INTENSITY);
printf("\n\nYou are doing great!!");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_BLUE|BACKGROUND_INTENSITY);
printf("\n\nThe best is yet to come!");
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),BACKGROUND_GREEN|BACKGROUND_INTENSITY);
printf("\n\nWhat are you waiting for?? Just play with it!!");
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
8.Program to Display the current Date and Time
The C library function char *ctime(const time_t *timer) returns a string representing the localtime based on the argument timer. The returned string has the following format: Www Mmm dd hh:mm:ss yyyy. Here Www is the weekday, Mmm the month in letters, dd the day of the month, hh:mm:ss the time and yyyy the year.
Below is the program to display current date and time.
#include<time.h> is used for time and ctime function and time_t datatype.
#include<stdio.h>
#include<time.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
time_t t; // not a primitive datatype
time(&t);
printf("\nThis program has been writeen at (date and time): %s", ctime(&t));
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}