14 C Program

14 C Program

1……………………
C “Hello, World!” Program

#include 
int main()
{
// printf() displays the string inside quotation
printf(“Hello, World!”);
return 0;
}
Output:
Hello, World!
2………………………………………………
C Program to Add Two Integers
#include
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
printf(“Enter two integers: “);
// Two integers entered by user is stored using scanf() function
scanf(“%d %d”, &firstNumber, &secondNumber);
// sum of two numbers in stored in variable sumOfTwoNumbers
sumOfTwoNumbers = firstNumber + secondNumber;
// Displays sum
printf(“%d + %d = %d”, firstNumber, secondNumber, sumOfTwoNumbers);
return 0;
}
Output:
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
3…………………………………………………..
C Program to Multiply two Floating Point Numbers
#include
int main()
{
double firstNumber, secondNumber, productOfTwoNumbers;
printf(“Enter two numbers: “);
// Stores two floating point numbers in variable firstNumber and secondNumber respectively
scanf(“%lf %lf”, &firstNumber, &secondNumber);
// Performs multiplication and stores the result in variable productOfTwoNumbers
productOfTwoNumbers = firstNumber * secondNumber;
// Result up to 2 decimal point is displayed using %.2lf
printf(“Product = %.2lf”, productofTwoNumbers);
return 0;
}
Output:
Enter two numbers: 2.4
1.12
Product = 2.69
4…………………………………………………….
C Program to Find the Size of int, float, double and char
#include
int main()
{
int integerType;
float floatType;
double doubleType;
char charType;
// Sizeof operator is used to evaluate the size of a variable
printf(“Size of int: %ld bytes\n”,sizeof(integerType));
printf(“Size of float: %ld bytes\n”,sizeof(floatType));
printf(“Size of double: %ld bytes\n”,sizeof(doubleType));
printf(“Size of char: %ld byte\n”,sizeof(charType));
return 0;
}
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte
5……………………………………………………..
Program to Swap Numbers Using Temporary Variable
#include
int main()
{
double firstNumber, secondNumber, temporaryVariable;
printf(“Enter first number: “);
scanf(“%lf”, &firstNumber);
printf(“Enter second number: “);
scanf(“%lf”,&secondNumber);
// Value of firstNumber is assigned to temporaryVariable
temporaryVariable = firstNumber;
// Value of secondNumber is assigned to firstNumber
firstNumber = secondNumber;
// Value of temporaryVariable (which contains the initial value of firstNumber) is assigned to secondNumber
secondNumber = temporaryVariable;
printf(“\nAfter swapping, firstNumber = %.2lf\n”, firstNumber);
printf(“After swapping, secondNumber = %.2lf”, secondNumber);
return 0;
}
Output:
Enter first number: 1.20
Enter second number: 2.45
After swapping, firstNumber = 2.45
After swapping, secondNumber = 1.20
6…………………………………………………………..
Program to Check Even or Odd
#include
int main()
{
int number;
printf(“Enter an integer: “);
scanf(“%d”, &number);
// True if the number is perfectly divisible by 2
if(number % 2 == 0)
printf(“%d is even.”, number);
else
printf(“%d is odd.”, number);
return 0;
}
Output:
Enter an integer: -7
-7 is odd.
7………………………………………………………..
C Program to Check Whether a Character is Vowel or Consonant
#include
int main()
{
char c
int isLowercaseVowel, isUppercaseVowel;
printf(“Enter an alphabet: “);
scanf(“%c”,&c);
// evaluates to 1 (true) if c is a lowercase vowel
isLowercaseVowel = (c == ‘a’ || c == ‘e’ || c == ‘i’ || c == ‘o’ || c == ‘u’);
// evaluates to 1 (true) if c is an uppercase vowel
isUppercaseVowel = (c == ‘A’ || c == ‘E’ || c == ‘I’ || c == ‘O’ || c == ‘U’);
// evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
if (isLowercaseVowel || isUppercaseVowel)
printf(“%c is a vowel.”, c);
else
printf(“%c is a consonant.”, c);
return 0;
}
Output:
Enter an alphabet: G
G is a consonant.
8……………………………………………………….
Program to Find the Largest Number Among Three Numbers
#include
int main()
{
double n1, n2, n3;
printf(“Enter three numbers: “);
scanf(“%lf %lf %lf”, &n1, &n2, &n3);
if( n1>=n2 && n1>=n3 )
printf(“%.2f is the largest number.”, n1);
if( n2>=n1 && n2>=n3 )
printf(“%.2f is the largest number.”, n2);
if( n3>=n1 && n3>=n2 )
printf(“%.2f is the largest number.”, n3);
return 0;
}
Output:
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
9………………………………………………………..
Program to Find Factorial of a Number
#include
int main()
{
int n, i;
unsigned long long factorial = 1;
printf(“Enter an integer: “);
scanf(“%d”,&n);
// show error if the user enters a negative integer
if (n < 0)
printf(“Error! Factorial of a negative number doesn’t exist.”);
else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf(“Factorial of %d = %llu”, n, factorial);
}
return 0;
}
Output:
Enter an integer: 10
Factorial of 10 = 3628800

10……………………………………………………………
Program to Check Leap Year
#include
int main()
{
int year;
printf(“Enter a year: “);
scanf(“%d”,&year);
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
printf(“%d is a leap year.”, year);
else
printf(“%d is not a leap year.”, year);
}
else
printf(“%d is a leap year.”, year );
}
else
printf(“%d is not a leap year.”, year);
return 0;
}
Output: 1
Enter a year: 1900
1900 is not a leap year.
Output: 2
Enter a year: 2012
2012 is a leap year.
11………………………………………………
Program to Display Fibonacci Sequence
#include
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm = 0;
printf(“Enter the number of terms: “);
scanf(“%d”, &n);
printf(“Fibonacci Series: “);
for (i = 1; i <= n; ++i)
{
// Prints the first two terms.
if(i == 1)
{
printf(“%d, “, t1);
continue;
}
if(i == 2)
{
printf(“%d, “, t2);
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
printf(“%d, “, nextTerm);
}
return 0;
}
Output:
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
12………………………………………….
Program to Display English Alphabets
#include
int main()
{
char c;
for(c = ‘A’; c <= 'Z'; ++c)
printf(“%c “, c);
return 0;
}
Output:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

13………………………………………………
Reverse an Integer
#include
int main()
{
int n, reversedNumber = 0, remainder;
printf(“Enter an integer: “);
scanf(“%d”, &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf(“Reversed Number = %d”, reversedNumber);
return 0;
}
Output:
Enter an integer: 2345
Reversed Number = 5432

14…………………………………………….
Power Using pow() Function
#include
#include
int main()
{
double base, exponent, result;
printf(“Enter a base number: “);
scanf(“%lf”, &base);
printf(“Enter an exponent: “);
scanf(“%lf”, &exponent);
// calculates the power
result = pow(base, exponent);
printf(“%.1lf^%.1lf = %.2lf”, base, exponent, result);
return 0;
}
Output:
Enter a base number: 2.3
15…………………………………………
Enter an exponent: 4.5
2.3^4.5 = 42.44
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s