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

c assignment 1st year

The document contains a series of programming assignments in C, each with a specific task such as calculating the sum and product of digits, reversing an integer, computing series sums, checking for prime numbers, and handling arrays. Each assignment includes a program code snippet and sample output demonstrating the functionality. The tasks range from basic arithmetic operations to more complex array manipulations and string handling.

Uploaded by

muskanafrin658
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

c assignment 1st year

The document contains a series of programming assignments in C, each with a specific task such as calculating the sum and product of digits, reversing an integer, computing series sums, checking for prime numbers, and handling arrays. Each assignment includes a program code snippet and sample output demonstrating the functionality. The tasks range from basic arithmetic operations to more complex array manipulations and string handling.

Uploaded by

muskanafrin658
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Assignm

ent to1print the sum and product


Write a program (WAP)
of digits of an integer.
PROGRAM:
#include <stdio.h>
int main() {
int num, digit, sum = 0, product = 1;

printf("Enter an integer: ");


scanf("%d", &num);

while (num > 0) {


digit = num % 10;
sum += digit;
product *= digit;
num /= 10;
}

printf("Sum of digits: %d\n", sum);


printf("Product of digits: %d\n", product);

return 0;
}

Output:
Enter an integer: 10
Sum of digits: 1
Product of digits: 0
PS C:\Users\muska\OneDrive\Desktop>
Assignment 2
WAP to reverse a non-negative integer.
PROGRAM:
#include <stdio.h>

int main() {
int num, reversed = 0, remainder;
printf("Enter a non-negative integer: ");
scanf("%d", &num);

while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}

printf("Reversed number: %d\n", reversed);

return 0;
}

Output:
Enter a non-negative integer: 14
Reversed number: 41
PS C:\Users\muska\OneDrive\Desktop>
Assignment 3
WAP to compute the sum of the first n terms of the
following series S =1+1/2+1/3+1/4+……
PROGRAM:
#include <stdio.h>

int main() {
int n, i;
float sum = 0.0;

// Prompt the user to enter the number of terms


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

// Calculate the sum of the series


for (i = 1; i <= n; i++) {
sum += 1.0 / i;
}

// Display the result


printf("Sum of the series up to %d terms: %f\n", n, sum);

return 0;
}

Output:
Enter the number of terms: 5
Sum of the series up to 5 terms: 2.283334
PS C:\Users\muska\OneDrive\Desktop>
Assignment 4
WAP to compute the sum of the first n terms of the
following series, S =1-2+3- 4+5…………….
PROGRAM:
#include <stdio.h>

int main() {
int n, i, sum = 0, sign = 1;

printf("Enter the number of terms: ");


scanf("%d", &n);

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


sum += sign * i;
sign *= -1; // Alternate sign for each term
}

printf("Sum of the series: %d\n", sum);

return 0;
}

Output:
Enter the number of terms: 2
Sum of the series: -1
PS C:\Users\muska\OneDrive\Desktop>
Assignment 5
Write a function to find whether a given no. is prime
or not. Use the same to generate the prime numbers
less than 100.
PROGRAM:
#include <stdio.h>

int is_prime(int num) {


if (num <= 1) {
return 0; // Numbers less than or equal to 1 are not prime
}

for (int i = 2; i * i <= num; i++) {


if (num % i == 0) {
return 0; // If divisible by any number between 2 and the square root of num, it's not
prime
}
}

return 1; // If the loop completes without finding a divisor, the number is prime
}

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (is_prime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
// C Program to Print Prime Numbers From 1 to 100

int j, numb, count;

// Checking for prime numbers


for (numb = 1; numb <= 100; numb++){
count = 0;
for (j = 2; j <= numb/2; j++){
if (numb % j == 0){
count++;
break;
}
}

// Checking and Printing Prime Numbers


if (count == 0 && numb != 1){
printf("%d''", numb);
}
}
return 0;
}

Output:
Enter a number: 7
7 is a prime number.
2''3''5''7''11''13''17''19''23''29''31''37''41''43''47''53''59''61''67''71''73''79''83''89''97''
PS C:\Users\muska\OneDrive\Desktop>
Assignment 6
Write a function that checks whether a given string is
Palindrome or not. Use this function to find whether
the string entered by the user is Palindrome or not.
PROGRAM:
#include <stdio.h>
#include <string.h>

int isPalindrome(const char *str) {


int len = strlen(str);
int i = 0, j = len - 1;

while (i < j) {
if (str[i] != str[j]) {
return 0; // Not a palindrome
}
i++;
j--;
}

return 1; // Palindrome
}

int main() {
char str[100];

printf("Enter a string: ");


scanf("%s", str);
if (isPalindrome(str)) {
printf("%s is a palindrome.\n", str);
} else {
printf("%s is not a palindrome.\n", str);
}

return 0;
}

Output:
Enter a string: mam
mam is a palindrome.
PS C:\Users\muska\OneDrive\Desktop>
Assignment 7
WAP to compute the factors of a given number.
PROGRAM:
#include <stdio.h>

int main() {
int num, i;

printf("Enter a positive integer: ");


scanf("%d", &num);

printf("Factors of %d are: ", num);

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


if (num % i == 0) {
printf("%d ", i);
}
}

printf("\n");

return 0;
}

Output:
Enter a positive integer: 10
Factors of 10 are: 1 2 5 10
PS C:\Users\muska\OneDrive\Desktop>
Assignment 8
WAP to swap two numbers using macro.
PROGRAM:
#include <stdio.h>

#define SWAP(x, y) do { \
int temp = x; \
x = y; \
y = temp; \
} while (0)

int main() {
int a, b;

printf("Enter two integers: ");


scanf("%d %d", &a, &b);

printf("Before swapping: a = %d, b = %d\n", a, b);

SWAP(a, b);

printf("After swapping: a = %d, b = %d\n", a, b);


return 0;
}

Output:
Enter two integers: 10 20
Before swapping: a = 10, b = 20
After swapping: a = 20, b = 10
PS C:\Users\muska\OneDrive\Desktop>
Assignment 9
WAP to print a triangle of stars as follows (take
number of lines from user):
PROGRAM:
#include <stdio.h>

int main() {
int rows, i, j, space;

printf("Enter the number of rows: ");


scanf("%d", &rows);

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


// Print spaces before stars
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}

// Print stars
for (j = 1; j <= 2 * i - 1; ++j) {
printf("* ");
}

printf("\n");
}

return 0;
}
Output:
Enter the number of rows: 4
*
***
*****
*******
PS C:\Users\muska\OneDrive\Desktop>
Assignment 10
WAP to perform following actions on an array entered
by the user :
a. Print the even-valued elements
b. Print the odd-valued elements
c. Calculate and print the sum and average of the
elements of array
d. Print the maximum and minimum element of array
e. Remove the duplicates from the array
f. Print the array in reverse order
(The program should present a menu to the user and
ask for one of the options. The menu should also
include options to re-enter array and to quit the
program.)

PROGRAM:
#include <stdio.h>
void printEven(int arr[], int n) {
printf("Even elements: ");
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
printf("%d ", arr[i]);
}
}
printf("\n");
}

void printOdd(int arr[], int n) {


printf("Odd elements: ");
for (int i = 0; i < n; i++) {
if (arr[i] % 2 != 0) {
printf("%d ", arr[i]);
}
}
printf("\n");
}
void calculateSumAvg(int arr[], int n) {
int sum = 0;
float avg;

for (int i = 0; i < n; i++) {


sum += arr[i];
}

avg = (float)sum / n;

printf("Sum: %d\n", sum);


printf("Average: %.2f\n", avg);
}

void findMaxMin(int arr[], int n) {


int max = arr[0], min = arr[0];

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


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

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


printf("Minimum element: %d\n", min);
}

void removeDuplicates(int arr[], int *n) {


int i, j;

for (i = 0; i < *n; i++) {


for (j = i + 1; j < *n; j++) {
if (arr[i] == arr[j]) {
for (int k = j; k < *n - 1; k++) {
arr[k] = arr[k + 1];
}
(*n)--;
j--;
}
}
}
}

void printReverse(int arr[], int n) {


printf("Array in reverse order: ");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
}

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

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


scanf("%d", &n);

printf("Enter the elements:\n");


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

do {
printf("\nDo as per your choice:\n");
printf("1. Print even-valued elements\n");
printf("2. Print odd-valued elements\n");
printf("3. Calculate sum and average\n");
printf("4. Find maximum and minimum\n");
printf("5. Remove duplicates\n");
printf("6. Print in reverse order\n");
printf("7. Re-enter array\n");
printf("8. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printEven(arr, n);
break;
case 2:
printOdd(arr, n);
break;
case 3:
calculateSumAvg(arr, n);
break;
case 4:
findMaxMin(arr, n);
break;
case 5:
removeDuplicates(arr, &n);
printf("Array after removing duplicates:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
break;
case 6:
printReverse(arr, n);
break;
case 7:
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
break;
case 8:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 8);

return 0;
}

Output:
Enter the number of elements: 4
Enter the elements:
3456

Do as per your choice:


1. Print even-valued elements
2. Print odd-valued elements
3. Calculate sum and average
4. Find maximum and minimum
5. Remove duplicates
6. Print in reverse order
7. Re-enter array
8. Quit
Enter your choice: 2
Odd elements: 3 5

Do as per your choice:


1. Print even-valued elements
2. Print odd-valued elements
3. Calculate sum and average
4. Find maximum and minimum
5. Remove duplicates
6. Print in reverse order
7. Re-enter array
8. Quit
Enter your choice: 8
Exiting...
PS C:\Users\muska\OneDrive\Desktop>

You might also like