0% found this document useful (0 votes)
15 views12 pages

Essential C Programming Exercises

The document contains a collection of C programming examples covering various fundamental concepts such as basic input/output, arithmetic operations, control structures, and functions. Each example includes code snippets demonstrating specific functionalities like calculating sums, checking for prime numbers, and implementing a simple calculator. It serves as a practical guide for beginners to understand and practice programming in C.

Uploaded by

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

Essential C Programming Exercises

The document contains a collection of C programming examples covering various fundamental concepts such as basic input/output, arithmetic operations, control structures, and functions. Each example includes code snippets demonstrating specific functionalities like calculating sums, checking for prime numbers, and implementing a simple calculator. It serves as a practical guide for beginners to understand and practice programming in C.

Uploaded by

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

Computer Programming Practice

By S.K Sahu IIIT, Nagpur

1. Hello World Program


#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}

2. Sum of Two Numbers


#include <stdio.h>
int main()
{
int a = 5, b = 7, sum;
sum = a + b;
printf("Sum = %d", sum);
return 0;
}

3. Find Largest of Two Numbers


#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
if(a > b)
printf("%d is largest", a);
else
printf("%d is largest", b);
return 0;
}

4. Check Even or Odd


#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if(num % 2 == 0)
printf("Even");
else
printf("Odd");
return 0;
}

5. Factorial of a Number
#include <stdio.h>
int main()
{
int n, i, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
fact *= i;
printf("Factorial = %d", fact);
return 0;
}

6. Fibonacci Series
#include <stdio.h>
int main()
{
int n, i, t1 = 0, t2 = 1, next;
printf("Enter number of terms: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
printf("%d ", t1);
next = t1 + t2;
t1 = t2;
t2 = next;
}
return 0;
}

7. Reverse a Number
#include <stdio.h>
int main()
{
int n, rev = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
printf("Reversed Number = %d", rev);
return 0;
}
8. Palindrome Number
#include <stdio.h>
int main()
{
int n, temp, rem, rev = 0;
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while(n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
if(temp == rev)
printf("Palindrome");
else
printf("Not Palindrome");
return 0;
}

🔹 9. Armstrong Number
#include <stdio.h>
#include <math.h>
int main()
{
int n, sum = 0, temp, digits = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
temp = n;
while(temp != 0)
{
temp /= 10;
digits++;
}
temp = n;
while(temp != 0)
{
rem = temp % 10;
sum += pow(rem, digits);
temp /= 10;
}
if(sum == n)
printf("Armstrong");
else
printf("Not Armstrong");
return 0;
}
10. Prime Number Check
#include <stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 2; i <= n/2; i++)
{
if(n % i == 0)
{
flag = 1;
break;
}
}
if(flag == 0 && n > 1)
printf("Prime");
else
printf("Not Prime");
return 0;
}

11. Swap Two Numbers


#include <stdio.h>
int main()
{
int a = 5, b = 10, temp;
temp = a;
a = b;
b = temp;
printf("a = %d, b = %d", a, b);
return 0;
}

12. Sum of Digits


#include <stdio.h>
int main()
{
int n, sum = 0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0)
{
rem = n % 10;
sum += rem;
n /= 10;
}
printf("Sum of Digits = %d", sum);
return 0;
}
3. Greatest of Three Numbers
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if(a > b && a > c)
printf("%d is greatest", a);
else if(b > c)
printf("%d is greatest", b);
else
printf("%d is greatest", c);
return 0;
}

14. Multiplication Table


#include <stdio.h>
int main()
{
int n, i;
printf("Enter a number: ");
scanf("%d", &n);
for(i = 1; i <= 10; i++)
printf("%d x %d = %d\n", n, i, n*i);
return 0;
}

15. Check Leap Year


#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
printf("Leap Year");
else
printf("Not Leap Year");
return 0;
}
16. ASCII Value of Character
#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c", &c);
printf("ASCII value = %d", c);
return 0;
}

17. Find Power of Number


#include <stdio.h>
#include <math.h>
int main()
{
int base, exp, result;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exp);
result = pow(base, exp);
printf("Result = %d", result);
return 0;
}

18. Count Digits in Number


#include <stdio.h>
int main()
{
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while(n != 0)
{
count++;
n /= 10;
}
printf("Number of digits = %d", count);
return 0;
}
19. Celsius to Fahrenheit
#include <stdio.h>
int main()
{
float c, f;
printf("Enter Celsius: ");
scanf("%f", &c);
f = (c * 9/5) + 32;
printf("Fahrenheit = %.2f", f);
return 0;
}

20. Simple Calculator


#include <stdio.h>
int main()
{
char op;
double a, b;
printf("Enter operator (+,-,*,/): ");
scanf("%c", &op);
printf("Enter two numbers: ");
scanf("%lf %lf", &a, &b);
switch(op)
{
case '+': printf("Result = %.2lf", a+b); break;
case '-': printf("Result = %.2lf", a-b); break;
case '*': printf("Result = %.2lf", a*b); break;
case '/': printf("Result = %.2lf", a/b); break;
default: printf("Invalid Operator");
}
return 0;
}

🔹 Arithmetic Operators
1. Addition, Subtraction, Multiplication, Division
#include <stdio.h>
int main()
{
int a = 20, b = 10;
printf("Addition = %d\n", a + b);
printf("Subtraction = %d\n", a - b);
printf("Multiplication = %d\n", a * b);
printf("Division = %d\n", a / b);
return 0;
}
2. Modulus Operator
#include <stdio.h>
int main()
{
int a = 29, b = 5;
printf("Remainder = %d", a % b);
return 0;
}

3. Area of Circle using Arithmetic


#include <stdio.h>
int main()
{
float r = 5, area;
area = 3.14 * r * r;
printf("Area = %.2f", area);
return 0;
}

4. Swap Two Numbers using Arithmetic (Without Third Variable)


#include <stdio.h>
int main()
{
int a = 5, b = 10;
a = a + b;
b = a - b;
a = a - b;
printf("a = %d, b = %d", a, b);
return 0;
}

🔹 Relational Operators
5. Compare Two Numbers
#include <stdio.h>
int main()
{
int a = 15, b = 20;
if(a > b)
printf("a is greater");
else
printf("b is greater");
return 0;
}
6. Check Equality
#include <stdio.h>
int main()
{
int x = 25, y = 25;
if(x == y)
printf("Equal");
else
printf("Not Equal");
return 0;
}

7. Check Student Pass/Fail


#include <stdio.h>
int main()
{
int marks = 35;
if(marks >= 33)
printf("Pass");
else
printf("Fail");
return 0;
}

🔹 Logical Operators
8. Check Leap Year using Logical AND/OR
#include <stdio.h>
int main()
{
int year = 2024;
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("Leap Year");
else
printf("Not Leap Year");
return 0;
}

9. Check Vowel or Consonant


#include <stdio.h>
int main()
{
char c = 'e';
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||
c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
printf("Vowel");
else
printf("Consonant");
return 0;
}

10. Check Positive and Even Number


#include <stdio.h>
int main()
{
int n = 12;
if(n > 0 && n % 2 == 0)
printf("Positive Even Number");
else
printf("Not Positive Even");
return 0;
}

🔹 Assignment Operators
11. Compound Assignment Example
#include <stdio.h>
int main()
{
int a = 10;
a += 5; // a = 15
a *= 2; // a = 30
printf("Final value = %d", a);
return 0;
}

12. Sum of Natural Numbers using +=


#include <stdio.h>
int main()
{
int n = 5, i, sum = 0;
for(i = 1; i <= n; i++)
sum += i;
printf("Sum = %d", sum);
return 0;
}
🔹 Increment/Decrement Operators
13. Pre-Increment and Post-Increment
#include <stdio.h>
int main()
{
int a = 5;
printf("Pre-increment = %d\n", ++a);
printf("Post-increment = %d\n", a++);
printf("Final a = %d", a);
return 0;
}

14. Countdown using Decrement


#include <stdio.h>
int main()
{
int i = 5;
while(i > 0)
{
printf("%d ", i--);
}
return 0;
}

🔹 Bitwise Operators
15. Bitwise AND, OR, XOR
#include <stdio.h>
int main()
{
int a = 5, b = 3;
printf("AND = %d\n", a & b);
printf("OR = %d\n", a | b);
printf("XOR = %d\n", a ^ b);
return 0;
}

16. Bitwise NOT


#include <stdio.h>
int main()
{
int a = 5;
printf("NOT = %d", ~a);
return 0;
}
17. Left Shift and Right Shift
#include <stdio.h>
int main()
{
int a = 5;
printf("Left Shift = %d\n", a << 1);
printf("Right Shift = %d", a >> 1);
return 0;
}

🔹 Conditional (Ternary Operator)


18. Largest of Two Numbers
#include <stdio.h>
int main()
{
int a = 15, b = 25;
int max = (a > b) ? a : b;
printf("Largest = %d", max);
return 0;
}

19. Odd or Even using Ternary


#include <stdio.h>
int main()
{
int n = 7;
(n % 2 == 0) ? printf("Even") : printf("Odd");
return 0;
}

20. Positive, Negative or Zero


#include <stdio.h>
int main()
{
int n = -5;
(n > 0) ? printf("Positive") : (n < 0 ? printf("Negative") :
printf("Zero"));
return 0;
}

You might also like