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

OOPs ASSIGNMENT

The document contains a series of C programming tasks that include basic number operations such as prime number checking, reversing a number, and calculating factorials. It also covers character-based tasks like finding ASCII values, converting uppercase to lowercase, and counting vowels and consonants in a string. Additionally, it includes array-based tasks for finding maximum and minimum values, reversing an array, and sorting an array using bubble sort.
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)
11 views12 pages

OOPs ASSIGNMENT

The document contains a series of C programming tasks that include basic number operations such as prime number checking, reversing a number, and calculating factorials. It also covers character-based tasks like finding ASCII values, converting uppercase to lowercase, and counting vowels and consonants in a string. Additionally, it includes array-based tasks for finding maximum and minimum values, reversing an array, and sorting an array using bubble sort.
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/ 12

Basic Number Tasks

1. Prime Number Check – Determine if a number is prime.

#include<stdio.h>

int main()
{
int n,c = 0;
printf("Enter a numbe to find prime : ");
scanf("%d",&n);
for(int i = 2 ; i<n ; i++){
if (n%i == 0){
c++;
break;
}
}

if (c > 0){
printf("It is not a Prime Number");
}
else {
printf("It is a Prime Number");
}

return 0;
}
2. Reverse a Number – Reverse the digits of an integer.

#include<stdio.h>

int main()
{
int n,rem,rev = 0;
printf("Enter a number : ");
scanf("%d",&n);

while(n!=0){
rem = n%10;
rev = rev*10 + rem;
n /= 10;
}

printf("The reversed num : %d",rev);


return 0;
}
3. Factorial Calculation – Calculate the factorial of a given number.

#include <stdio.h>

int main() {
int num, i;
int fact = 1;
printf("Enter a number: ");
scanf("%d", &num);

if (num < 0) {
printf("Negative numbers.\n");
} else {

for (i = 1; i <= num; i++) {


fact *= i;
}

printf("Factorial of %d = %d\n", num, fact);


}

return 0;
}
Character-Based Tasks
6. ASCII Value Finder – Take a character as input and print its ASCII
value.

#include <stdio.h>

int main() {
char ch;

printf("Enter a character: ");


scanf("%c", &ch);

printf("The ASCII value of '%c' is %d\n", ch, ch);

return 0;
}
7. Uppercase to Lowercase – Convert an uppercase letter to lowercase.

#include <stdio.h>

int main() {
char uppercase, lowercase;

printf("Enter an uppercase letter: ");


scanf("%c", &uppercase);

if (uppercase >= 'A' && uppercase <= 'Z') {


lowercase = uppercase + 32; // ASCII conversion
printf("The lowercase of '%c' is '%c'\n", uppercase, lowercase);
} else {
printf("Invalid input.\n");
}

return 0;
}
8. Count Vowels and Consonants – Count the number of vowels and
consonants in a string.

#include<stdio.h>
#include <ctype.h>
int main(){
char str[100];
int vowel = 0 , consonant = 0 , i = 0 ;
printf("Enter the string :");
scanf("%s", str);
while(str[i] != '\0'){
char ch = tolower(str[i]);
if((ch >= 'a' && ch <= 'z')){
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u'){
vowel++;
}else{
consonant++;
}
}
i++;
}

printf("Number of vowels: %d\n", vowel);


printf("Number of consonants: %d\n", consonant);

return 0;
}
9. Palindrome String Check – Check whether a string is a palindrome.

#include <stdio.h>
#include <string.h>

int main() {
char str[100];
int i, len, flag = 1;
printf("Enter a string: ");
scanf("%s", str);
len = strlen(str);
for (i = 0; i < len / 2; i++) {
if (str[i] != str[len - i - 1]) {
flag = 0; break;
}
}

if (flag) {
printf("The string is a palindrome.\n");
} else {
printf("The string is NOT a palindrome.\n");
}

return 0;
}
10. Character Frequency in String – Count occurrences of a specific
character in a string.

#include <stdio.h>

int main() {
char str[100], ch;
int i, count = 0;

printf("Enter a string: ");


scanf("%s", str);
printf("Enter the character to count: ");
scanf(" %c", &ch);

for (i = 0; str[i] != '\0'; i++) {


if (str[i] == ch) {
count++;
}
}

printf("The character '%c' appears %d times in the string.\n", ch, count);

return 0;
}
Array-Based Tasks
11. Find Maximum and Minimum – Find the largest and smallest element
in an array.

#include <stdio.h>

int main() {
int arr[100], n, i, max, min;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

max = min = arr[0];

for (i = 1; i < n; i++) {


if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}

printf("Max: %d\n", max);


printf("Min: %d\n", min);
return 0;
}

12. Reverse an Array – Reverse the elements of an array.

#include <stdio.h>

int main() {
int arr[100], n, i, temp;

printf("Enter the number of elements in the array: ");


scanf("%d", &n);

printf("Enter %d elements: ", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

for (i = 0; i < n / 2; i++) {


temp = arr[i];
arr[i] = arr[n - i - 1];
arr[n - i - 1] = temp;
}

printf("Reversed array: ");


for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

13. Sorting an Array – Sort an array in ascending order.

#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n - 1; i++) { // Number of passes
for (j = 0; j < n - i - 1; j++) { // Compare adjacent elements
if (arr[j] > arr[j + 1]) { // Swap if out of order
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[100], n, i;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

printf("Enter %d elements: ", n);


for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

bubbleSort(arr, n);
printf("Sorted array in ascending order: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

return 0;
}

By:
DEEPAK S

You might also like