0% found this document useful (0 votes)
12 views42 pages

C Program 2

Uploaded by

Chandan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
12 views42 pages

C Program 2

Uploaded by

Chandan Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 42

C

Programming
NAME: ADITYA VERMA
ROLL NO.: 24BEC008
BRANCH: ECE
SUBMITTED TO:
DR. AMIT KANT PANDIT
INDEX
1. C "Hello, World!" Program
2. Program to Print an Integer
3. Program to Add Two Integers
4. Program to Multiply Two Numbers
5. Program to Print ASCII Value

7. Program to Find the Size of Variables

8. Program Using the long keyword

9. Swap Numbers Using Temporary Variable

10 Program to Check Even or Odd

11. Program to Check Vowel or consonant

12. Program to Find the Largest Number


Among Three Numbers
Using if Statement
Using if...else Ladder
Using Nested if...else
1. C "Hello, World!" Program
#include <stdio.h>
intmain()
{
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}

OUTPUT
Hello, World!
2. Program to Print an Integer

#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
// reads and stores input
scanf("%d", &number);
// displays output
printf("You entered: %d", number);
return 0;
}

OUTPUT
Enter an integer: 25
You entered: 25
3. Program to Add Two Integers

#include <stdio.h>
int main()
{
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// calculate the sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}

Output
Enter two integers: 12
11
12 + 11 = 23

4. Program to Multiply Two Numbers


#include <stdio.h>
int main()
{
double a, b, product;
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
// Calculating product
product = a * b;
// %.2lf displays number up to 2 decimal point
printf("Product = %.2lf", product);
return 0;
}
Output
Enter two numbers: 2.4
1.12
Product = 2.69

5. Program to PrintASCII Value


#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
// %d displays the integer value of a character
// %c displays the actual character
printf("ASCII value of %c = %d", c, c);
return 0;
}

Output
Enter a character: G
ASCII value of G = 71
6. Program to Compute Quotient and
Remainder
#include <stdio.h>
int main()
{
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", &dividend);
printf("Enter divisor: ");
scanf("%d", &divisor);
// Computes quotient
quotient = dividend / divisor;
// Computes remainder
remainder = dividend % divisor;
printf("Quotient = %d\n", quotient);
printf("Remainder = %d", remainder);
return 0;
}
Output
Enter dividend: 25
Enter divisor: 4
Quotient = 6
Remainder = 1
7. Program to Find the Size of
Variables
#include<stdio.h>
int main()
{
int intType;
float floatType;
double doubleType;
char charType;
// sizeof evaluates the size of a variable
printf("Size of int: %zu bytes\n", sizeof(intType));
printf("Size of float: %zu bytes\n", sizeof(floatType));
printf("Size of double: %zu bytes\n",
sizeof(doubleType));
printf("Size of char: %zu 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

8. Program Using the long keyword


#include <stdio.h>
int main()
{
int a;
long b; // equivalent to long int b;
long long c; // equivalent to long long int c;
double e;
long double f;
printf("Size of int= %zu bytes \n", sizeof(a));
printf("Size of long int= %zu bytes\n",
sizeof(b));
printf("Size of long long int= %zu bytes\n",
sizeof(c));
printf("Size of double = %zu bytes\n",
sizeof(e));
printf("Size of long double = %zu bytes\n",
sizeof(f));
return 0;
}

Output
Size of int= 4 bytes
Size of long int= 8 bytes
Size of long long int= 8 bytes
Size of double = 8 bytes
Size of long double = 16 bytes
9. Swap Numbers Using Temporary
Variable
#include<stdio.h>
intmain()
{
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);
// value of first is assigned to temp
temp = first;
// value of second is assigned to first
first = second;
// value of temp (initial value of first) is assigned to
second
second = temp;
// %.2lf displays number up to 2 decimal points
printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf",
second);
return 0;
}
Output
Enter first number: 1.20
Enter second number: 2.45

After swapping, first number = 2.45


After swapping, second number = 1.20
10. Program to Check Even or Odd
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer: ");
scanf("%d", &num);

// true if num is perfectly divisible by 2


if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}

Output
Enter an integer: -7
-7 is odd.

11. Program to Check Vowel or


consonant
#include <stdio.h>
int main()
{
char c;
intlowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);

// evaluates to 1 if variable c is a lowercase vowel


lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o'
|| c == 'u');

// evaluates to 1 if variable c is a uppercase vowel


uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c ==
'O' || c == 'U');

// evaluates to 1 (true) if c is a vowel


if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}

Output
Enter an alphabet: G
G is a consonant.
12. Program to Find the Largest
Number Among Three Numbers
I. Using if Statement
#include <stdio.h>
int main()
{
double n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2f is the largest number.", n1);

// if n2 is greater than both n1 and n3, n2 is the largest


if (n2 >= n1 && n2 >= n3)
printf("%.2f is the largest number.", n2);

// if n3 is greater than both n1 and n2, n3 is the largest


if (n3 >= n1 && n3 >= n2)
printf("%.2f is the largest number.", n3);

return 0;
}

OUTPUT:
Enter three different numbers: 25 44 68
68.00 is the largest number.
II. Using if...else Ladder
#include <stdio.h>
int main ()
{
double n1, n2, n3;
printf("Enter three numbers: ");
scanf("%lf %lf %lf", &n1, &n2, &n3);
// if n1 is greater than both n2 and n3, n1 is the largest
if (n1 >= n2 && n1 >= n3)
printf("%.2lf is the largest number.", n1);
// if n2 is greater than both n1 and n3, n2 is the largest
else if (n2 >= n1 && n2 >= n3)
printf("%.2lf is the largest number.", n2);

// if both above conditions are false, n3 is the largest


else
printf("%.2lf is the largest number.", n3);

return 0;
}

OUTPUT:
Enter three numbers: 43 45 223
223.00 is the largest number.
III. Using Nested if...else
#include <stdio.h>
int main ()
{
double n1, n2, n3;

printf("Enter three numbers: ");


scanf("%lf %lf %lf", &n1, &n2, &n3);

// outer if statement
if (n1 >= n2) {

// inner if...else
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
}

// outer else statement


else {

// inner if...else
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}

OUTPUT:
Enter three numbers: 565 544 899
899.00 is the largest number
13. Program to Find Roots of a Quadratic Equation

#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, discriminant , root1, root2, realPart,
imagPart;
printf("enter coefficient a, b and c:");
scanf("%lf %lf %lf", &a, &b ,&c);

discriminant = (b*b)-(4*a*c);
// condition for real and different roots
if (discriminant > 0){
root1= (-b+ sqrt(discriminant)) / (2*a);
root2= (-b- sqrt(discriminant)) / (2*a);
printf(" Root1 = %0.2lf and Root2 = %0.2lf \n",
root1 , root2);
}
// condition for real and equal roots
else if ( discriminant ==0 ){
root1 = root2 = -b/(2*a);
printf("Root1 = Root2 = %0.2lf \n", root1);
}
// if roots are not real
else{
realPart= -b/(2*a);
imagPart= sqrt(-discriminant)/(2*a);
printf("Root1 = %0.2lf + %0.2lfi and Root2=
%0.2lf - %0.2lfi",realPart , imagPart , realPart ,
imagPart);

return 0;
}

Output
Enter coefficients a, b and c: 2.3
4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i
14. Program to Check Leap Year
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400


if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return 0;
}

Output 1
Enter a year: 1900
1900 is not a leap year.
15. Check Positive or Negative Using Nested
if...else
#include <stdio.h>
int main () {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num <= 0.0) {
if (num == 0.0)
printf("You entered 0.");
else
printf("You entered a negative number.");
}
else
printf("You entered a positive number.");
return 0;
}
Output 1
Enter a number: 12.3
You entered a positive number.
Check Positive or Negative Using if...else
Ladder
#include <stdio.h>
int main() {
double num;
printf("Enter a number: ");
scanf("%lf", &num);
if (num < 0.0)
printf("You entered a negative number.");
else if (num > 0.0)
printf("You entered a positive number.");
else
printf("You entered 0.");
return 0;
}

Output 2
Enter a number: 0
You entered 0.

16. Program to Check Alphabet


#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c
<= 'Z'))
printf("%c is an alphabet.", c);
else
printf("%c is not an alphabet.", c);
return 0;
}

Output
Enter a character: *
* is not an alphabet

17. (a) Sum of Natural Numbers Using for


Loop
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; ++i) {
sum += i;
}
printf("Sum = %d", sum);
return 0;
}

(b) Sum of Natural Numbers Using while


Loop
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
i = 1;
while (i <= n) {
sum += i;
++i;
}
printf("Sum = %d", sum);
return 0;
}

Output
Enter a positive integer: 100
Sum = 5050
18. Factorial of a Number
#include <stdio.h>
int main() {
int n, i;
unsigned long long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
// shows 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) {
fact *= i;
}
printf("Factorial of %d = %llu", n, fact);
}

return 0;
}
Output
Enter an integer: 10
Factorial of 10 = 3628800
19. Multiplication Table Up to 10

#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);

for (int i = 1; i <= 10; ++i) {


printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
Output
Enter an integer: 9
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
20. Fibonacci Series up to n terms
#include <stdio.h>
int main() {

int i, n;

// initialize first and second terms


int t1 = 0, t2 = 1;

// initialize the next term (3rd term)


int nextTerm = t1 + t2;

// get no. of terms from user


printf("Enter the number of terms: ");
scanf("%d", &n);

// print the first two terms t1 and t2


printf("Fibonacci Series: %d, %d, ", t1, t2);

// print 3rd to nth terms


for (i = 3; i <= n; ++i) {
printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

Output
Enter the number of terms: 10
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
21. GCD Using for loop and if Statement
#include <stdio.h>
int main()
{
int n1, n2, i, gcd;

printf("Enter two integers: ");


scanf("%d %d", &n1, &n2);
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}
printf("G.C.D of %d and %d is %d", n1, n2,
gcd);

return 0;
}
#2: GCD Using while loop and if...else
Statement
#include <stdio.h>
int main()
{
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d",&n1,&n2);
while(n1!=n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("GCD = %d",n1);
return 0;
}
Output
Enter two positive integers: 81
153
GCD = 9
Example #3: GCD for both positive and
negative numbers
#include <stdio.h>
int main()
{
int n1, n2;

printf("Enter two integers: ");


scanf("%d %d",&n1,&n2);

// if user enters negative number, sign of the


number is changed to positive
n1 = ( n1 > 0) ? n1 : -n1;
n2 = ( n2 > 0) ? n2 : -n2;

while(n1!=n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
printf("GCD = %d",n1);

return 0;
}
Output
Enter two integers: 81
-153
GCD = 9
22. LCM using while and if
#include <stdio.h>
int main() {
int n1, n2, max, lcm;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
// maximum number between n1 and n2 is
stored in max
max = (n1 > n2) ? n1 : n2;
lcm = max;
while ((lcm % n1 != 0) || (lcm % n2 != 0)) {
lcm += max;
}

printf("The LCM of %d and %d is %d.",


n1, n2, lcm);

return 0;
}
Output
Enter two positive integers: 72
120
The LCM of 72 and 120 is 360.

You might also like