c assignment 1st year
c assignment 1st year
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;
}
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;
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;
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>
return 1; // If the loop completes without finding a divisor, the number is prime
}
int main() {
int 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
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>
while (i < j) {
if (str[i] != str[j]) {
return 0; // Not a palindrome
}
i++;
j--;
}
return 1; // Palindrome
}
int main() {
char str[100];
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("\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;
SWAP(a, b);
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;
// 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");
}
avg = (float)sum / n;
int main() {
int arr[100], n, choice;
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