C Programs
C Programs
#include <stdio.h>
int main() {
int num;
#include<stdio.h>
int main()
{
printf("\n\n\t\t Studytonight - Best place to learn \n\n\n");
int number;
printf("Please enter a number:\n");
scanf("%d",&number);
/*
For single statements we can skip the curly brackets
*/
if(number < 100)
printf("Number is less than 100!\n");
else if(number == 100)
printf("Number is 100!\n");
else
printf("Number is greater than 100!\n");
return 0;
}
-----------------------------------------------------------------------------------
Switch Statements( Vowels or Constant)
~~~~~~~~~~~~~~
#include<stdio.h>
int main()
{
printf("\n\n\t\t Studytonight - Best place to learn\n\n\n");
char ch;
printf("Input a Character : ");
scanf("%c", &ch);
switch(ch)
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf("\n\n%c is a vowel.\n\n", ch);
break;
default:
printf("%c is Consonant.\n\n", ch);
}
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
-------------------------------------------------------------------------------
#include<stdio.h>
#include<ctype.h> // to use system defined function islower & toupper
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
char alphabet;
printf("Enter an alphabet : ");
putchar('\n'); // to move to next Line
alphabet=getchar();
if(islower(alphabet))
putchar(toupper(alphabet));
else
// must be an uppercase character
printf("%c",tolower(alphabet)) ;
int a,b;
int main()
{
printf("\n\n\t\t Studytonight - Best place to learn\n\n\n");
printf("\n\n Enter the two values to find the greatest and smallest number: \
n");
scanf("%d%d", &a, &b);
if(a == b)
printf("Both are equal\n");
else if(a < b)
{
printf("\n\n The largest number is %03d\n", b);
printf("\n The smallest number is %03d\n", a);
printf("\n The largest number is %03d\n", b);
}
else //Only possibility remaining
{
printf("The largest number is %03d\n", a);
printf("The smallest number is %03d\n", b);
}
printf("\n\n\t\t\t Coding is Fun !\n\n\n");
return 0;
}
--------------------------------------------------------------------------
#include<stdio.h>
int main()
{
printf("\n\n\t\t Studytonight - Best place to learn\n\n\n");
/*
Always declare the variables before using them
*/
int i = 0; // declaration and initialization at the same time
/*
consequently, when i equals 10, the loop breaks.
i is updated before the condition is checked-
hence the value of i after exiting the loop is 10
*/
}
printf("\n Remember that the loop condition checks the conditional statement
before it loops again.\n\n");
int main()
{
char ch;
double a, b;
while (1) {
printf("Enter an operator (+, -, *, /), if want to exit press x: ");
scanf(" %c", &ch);
// to exit
if (ch == 'x')
exit(0);
printf("Enter two first and second operand: ");
scanf("%lf %lf",&a,&b);
// Using switch case we will differentiate
// operations based on different operator
switch (ch) {
// For Addition
case '+':
printf("%.1lf + %.1lf = %.1lf\n", a, b, a + b);
break;
// For Subtraction
case '-':
printf("%.1lf - %.1lf = %.1lf\n", a, b, a - b);
break;
// For Multiplication
case '*':
printf("%.1lf * %.1lf = %.1lf\n", a, b, a * b);
break;
// For Division
case '/':
printf("%.1lf / %.1lf = %.1lf\n", a, b, a / b);
break;
// If operator doesn't match any case constant
default:
printf("Error! please write a valid operator\n");
}
}
}