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

Internship_Assignments_C_Programming

Uploaded by

Mengistu Tarko
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)
15 views

Internship_Assignments_C_Programming

Uploaded by

Mengistu Tarko
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/ 5

Internship Assignments - C Programming

1. Find Maximum of Two Numbers

#include <stdio.h>

int main() {

int a, b;

printf("Enter two numbers: ");

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

printf("Maximum: %d\n", (a > b) ? a : b);

return 0;

2. Find Maximum of Three Numbers

#include <stdio.h>

int main() {

int a, b, c;

printf("Enter three numbers: ");

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

printf("Maximum: %d\n", (a > b) ? (a > c ? a : c) : (b > c ? b : c));

return 0;

3. Simple Calculator

#include <stdio.h>

int main() {

char op;

double num1, num2;

printf("Enter an operator (+, -, *, /): ");

scanf(" %c", &op);

printf("Enter two operands: ");


scanf("%lf %lf", &num1, &num2);

switch (op) {

case '+': printf("Result: %.2lf\n", num1 + num2); break;

case '-': printf("Result: %.2lf\n", num1 - num2); break;

case '*': printf("Result: %.2lf\n", num1 * num2); break;

case '/':

if (num2 != 0) printf("Result: %.2lf\n", num1 / num2);

else printf("Error: Division by zero.\n");

break;

default: printf("Invalid operator.\n");

return 0;

4. Sum of Digits of a Number

#include <stdio.h>

int main() {

int num, sum = 0, digit;

printf("Enter a number: ");

scanf("%d", &num);

while (num > 0) {

digit = num % 10;

sum += digit;

num /= 10;

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

return 0;

5. 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\n", rev);

return 0;

6. Sum and Average of Array Elements

#include <stdio.h>

int main() {

int n, i, sum = 0;

float avg;

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

scanf("%d", &n);

int arr[n];

printf("Enter the elements: ");

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

scanf("%d", &arr[i]);

sum += arr[i];

avg = (float)sum / n;

printf("Sum: %d, Average: %.2f\n", sum, avg);

return 0;

7. Bit Manipulation: SET, GET, CLEAR, TOGGLE

#include <stdio.h>

void set_bit(int *num, int pos) { *num |= (1 << pos); }


int get_bit(int num, int pos) { return (num >> pos) & 1; }

void clear_bit(int *num, int pos) { *num &= ~(1 << pos); }

void toggle_bit(int *num, int pos) { *num ^= (1 << pos); }

int main() {

int num, pos, choice;

printf("Enter a number and position: ");

scanf("%d %d", &num, &pos);

printf("1-SET, 2-GET, 3-CLEAR, 4-TOGGLE: ");

scanf("%d", &choice);

switch (choice) {

case 1: set_bit(&num, pos); printf("Result: %d\n", num); break;

case 2: printf("Bit at position %d: %d\n", pos, get_bit(num, pos)); break;

case 3: clear_bit(&num, pos); printf("Result: %d\n", num); break;

case 4: toggle_bit(&num, pos); printf("Result: %d\n", num); break;

default: printf("Invalid choice.\n");

return 0;

8. Print Binary Pattern Using Bitwise Operators

#include <stdio.h>

void print_binary(int num) {

for (int i = 31; i >= 0; i--) {

printf("%d", (num >> i) & 1);

if (i % 4 == 0) printf(" ");

printf("\n");

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);
print_binary(num);

return 0;

9. Swap Nibbles of a Byte

#include <stdio.h>

int main() {

unsigned char byte, swapped;

printf("Enter a byte (0-255): ");

scanf("%hhu", &byte);

swapped = ((byte & 0x0F) << 4) | ((byte & 0xF0) >> 4);

printf("Original: %d, Swapped: %d\n", byte, swapped);

return 0;

You might also like