0% found this document useful (0 votes)
32 views4 pages

C Programs

This document contains examples of using conditional statements like if-else, switch statements, and for loops in C programming. It shows how to check conditions, compare values, and iterate through loops. Multiple examples are provided to demonstrate evaluating input values, determining the largest or smallest number, checking for vowels, toggling case of characters, and building a basic calculator program.

Uploaded by

Ganesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

C Programs

This document contains examples of using conditional statements like if-else, switch statements, and for loops in C programming. It shows how to check conditions, compare values, and iterate through loops. Multiple examples are provided to demonstrate evaluating input values, determining the largest or smallest number, checking for vowels, toggling case of characters, and building a basic calculator program.

Uploaded by

Ganesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

-3 -2 -1 0 1 2 3

Negative Zero Positive

#include <stdio.h>

int main() {
int num;

printf("Enter a number: \n");


scanf("%d", &num);
if (num > 0)
printf("%d is a positive number \n", num);
else if (num < 0)
printf("%d is a negative number \n", num);
else
printf("0 is neither positive nor negative");
return 0;
}
--------------------------------------------------------------------------------
Multiple Conditional Statements
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#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");

printf("\n\n\t\t\tCoding is Fun !\n\n\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();

printf("\n\nReverse case of %c is : ",alphabet);

if(islower(alphabet))
putchar(toupper(alphabet));

else
// must be an uppercase character
printf("%c",tolower(alphabet)) ;

printf("\n\n\t\t\tCoding is Fun !\n\n\n");


return 0;
}
--------------------------------------------------------------------------------
#include<stdio.h>

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

for(i = 0; i < 10; i++)


{
printf("i = %d\n", i);

/*
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\ The value of i after exiting the loop is %d\n\n", i);

printf("\n Remember that the loop condition checks the conditional statement
before it loops again.\n\n");

printf("Consequently, when i equals 10, the loop breaks.\n\n");

printf("i is updated before the condition is checked- hence the value of i


after exiting the loop is 10 .\n\n");

printf("\n\n\t\t\t Coding is Fun !\n\n\n");


return 0;
}
-----------------------------------------------------------------------------------
// C Program to Make a Simple Calculator
// Using switch case
#include <stdio.h>
#include <stdlib.h>

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");
}
}
}

You might also like