[go: up one dir, main page]

0% found this document useful (0 votes)
36 views3 pages

All 30 C Programs

The document contains a series of C programming examples demonstrating basic algorithms such as checking if a number is even or odd, counting digits, converting strings to uppercase, checking for palindromes, counting character frequencies, summing digits, reversing numbers, and printing Pascal's Triangle. Each example includes a brief description and the corresponding C code. These examples serve as practical exercises for understanding fundamental programming concepts.

Uploaded by

gy589973
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)
36 views3 pages

All 30 C Programs

The document contains a series of C programming examples demonstrating basic algorithms such as checking if a number is even or odd, counting digits, converting strings to uppercase, checking for palindromes, counting character frequencies, summing digits, reversing numbers, and printing Pascal's Triangle. Each example includes a brief description and the corresponding C code. These examples serve as practical exercises for understanding fundamental programming concepts.

Uploaded by

gy589973
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
You are on page 1/ 3

1.

Check Whether a Number is Even or Odd

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

2. Count Number of Digits in an Integer

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

3. Convert Lowercase to Uppercase (String)

#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
for (int i = 0; str[i] != '\0'; i++) {
str[i] = toupper(str[i]);
}
printf("Uppercase string: %s", str);
return 0;
}

4. Check Whether a String is Palindrome

#include <stdio.h>
#include <string.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
int len = strlen(str), flag = 1;
for (int i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
flag = 0;
break;
}
}
if (flag)
printf("Palindrome");
else
printf("Not a palindrome");
return 0;
}

5. Count Frequency of Characters in a String

#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int freq[256] = {0};
printf("Enter a string: ");
gets(str);
for (int i = 0; str[i] != '\0'; i++) {
freq[(int)str[i]]++;
}
printf("Character frequencies:\n");
for (int i = 0; i < 256; i++) {
if (freq[i] > 0)
printf("%c = %d\n", i, freq[i]);
}
return 0;
}

6. Sum of Digits of a Number

#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;
}
7. Reverse a Number

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

8. Program to Print Pascal's Triangle

#include <stdio.h>
int fact(int n) {
int f = 1;
for (int i = 1; i <= n; i++)
f *= i;
return f;
}
int main() {
int n, i, j;
printf("Enter number of rows: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j <= i; j++)
printf("%d ", fact(i) / (fact(j) * fact(i - j)));
printf("\n");
}
return 0;
}

You might also like