0% found this document useful (0 votes)
4 views

Exercise Programme

The document contains a series of C programming examples covering basic concepts such as printing messages, arithmetic operations, control structures, and functions. Each example demonstrates a specific functionality, including calculating sums, checking for prime numbers, and determining leap years. The code snippets are designed for educational purposes, illustrating fundamental programming techniques in C.

Uploaded by

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

Exercise Programme

The document contains a series of C programming examples covering basic concepts such as printing messages, arithmetic operations, control structures, and functions. Each example demonstrates a specific functionality, including calculating sums, checking for prime numbers, and determining leap years. The code snippets are designed for educational purposes, illustrating fundamental programming techniques in C.

Uploaded by

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

1.

Hello World

#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;
int sum = a + b;
printf("Sum = %d", sum);
return 0;
}

3. 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;
}

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

#include <stdio.h>
int main() {
int n, i, isPrime = 1;
printf("Enter a number: ");
scanf("%d", &n);
for (i = 2; i <= n / 2; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
if (isPrime && n > 1)
printf("Prime");
else
printf("Not Prime");
return 0;
}

6. Print 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;
}

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

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

9. Swap Two Numbers (using temp)


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

10. Find Largest Among 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("Largest = %d", a);
else if (b >= a && b >= c)
printf("Largest = %d", b);
else
printf("Largest = %d", c);
return 0;
}

✅ 11. Count Digits in a Number

#include <stdio.h>
int main() {
int n, count = 0;
printf("Enter a number: ");
scanf("%d", &n);
while (n != 0) {
n /= 10;
count++;
}
printf("Number of digits = %d", count);
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;
}

13. Simple Calculator (switch case)

#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("%.2lf", a + b); break;
case '-': printf("%.2lf", a - b); break;
case '*': printf("%.2lf", a * b); break;
case '/': printf("%.2lf", a / b); break;
default: printf("Invalid operator");
}
return 0;
}

14. Palindrome Number


#include <stdio.h>
int main() {
int num, rev = 0, rem, temp;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while (temp != 0) {
rem = temp % 10;
rev = rev * 10 + rem;
temp /= 10;
}
if (rev == num)
printf("Palindrome");
else
printf("Not Palindrome");
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 a leap year");
return 0;
}

You might also like