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

Programming Answersheet

The document provides a collection of programming solutions in C, covering various topics such as printing the day of the week, displaying patterns, finding maximum and minimum in an array, checking for prime numbers, generating Fibonacci series, and more. It also includes explanations of compilers and interpreters, as well as the main components of a computer system. Each solution is presented with code snippets and brief descriptions of their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Programming Answersheet

The document provides a collection of programming solutions in C, covering various topics such as printing the day of the week, displaying patterns, finding maximum and minimum in an array, checking for prime numbers, generating Fibonacci series, and more. It also includes explanations of compilers and interpreters, as well as the main components of a computer system. Each solution is presented with code snippets and brief descriptions of their functionality.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Programming Answersheet

1. Print the day of the week based on user input

#include <stdio.h>

void main() {

int day;

printf("Enter a number (1-7): ");

scanf("%d", &day);

switch (day) {

case 1: printf("Monday"); break;

case 2: printf("Tuesday"); break;

case 3: printf("Wednesday"); break;

case 4: printf("Thursday"); break;

case 5: printf("Friday"); break;

case 6: printf("Saturday"); break;

case 7: printf("Sunday"); break;

default: printf("Invalid Input");

2. Display Pattern

#include <stdio.h>

void main() {

int i, j;

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

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

printf("%d ", j);

printf("\n");

}
3. Display Another Pattern

#include <stdio.h>

void main() {

int i, j, num = 1;

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

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

printf("%d ", num++);

printf("\n");

4. Find Maximum and Minimum in an Array

#include <stdio.h>

void main() {

int n, i, max, min;

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

scanf("%d", &n);

int arr[n];

printf("Enter elements: ");

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\nMin: %d", max, min);

}
5. Check Prime or Composite

#include <stdio.h>

void main() {

int num, i, isPrime = 1;

printf("Enter a number: ");

scanf("%d", &num);

if (num < 2) {

printf("Neither Prime nor Composite");

return;

for(i = 2; i <= num / 2; i++) {

if(num % i == 0) {

isPrime = 0;

break;

if(isPrime) printf("Prime");

else printf("Composite");

6. Fibonacci Series

#include <stdio.h>

void main() {

int n, i, t1 = 0, t2 = 1, nextTerm;

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

scanf("%d", &n);

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

printf("%d ", t1);

nextTerm = t1 + t2;

t1 = t2;
t2 = nextTerm;

7. Compare for, while, and do-while loop

#include <stdio.h>

void main() {

int i = 1;

printf("For Loop:\n");

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

printf("%d ", i);

printf("\nWhile Loop:\n");

i = 1;

while(i <= 5) {

printf("%d ", i);

i++;

printf("\nDo-While Loop:\n");

i = 1;

do {

printf("%d ", i);

i++;

} while(i <= 5);

8. Sum of Array Elements

#include <stdio.h>

void main() {

int arr[10], i, sum = 0;

printf("Enter 10 numbers: ");


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

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

sum += arr[i];

printf("Sum = %d", sum);

9. Check Palindrome

#include <stdio.h>

void main() {

int num, reversed = 0, original, remainder;

printf("Enter an integer: ");

scanf("%d", &num);

original = num;

while(num != 0) {

remainder = num % 10;

reversed = reversed * 10 + remainder;

num /= 10;

if(original == reversed) printf("Palindrome");

else printf("Not a Palindrome");

10. Distinguish between Compiler and Interpreter with examples.

Answer:

 Compiler: Translates the entire code at once and produces an


executable file (e.g., GCC for C, Java Compiler).

 Interpreter: Translates code line by line and executes it directly


(e.g., Python Interpreter, JavaScript).

11. Explain the main components of a computer system with a


neat diagram.

Answer: A computer system consists of:


 Input Devices: Keyboard, Mouse.

 Processing Unit: CPU (ALU, CU, Registers).

 Memory: RAM, ROM.

 Storage Devices: Hard Drive, SSD.

 Output Devices: Monitor, Printer.

12. Write a program to check whether a user-given integer is a


palindrome.

#include <stdio.h>

void main() {

int num, rev = 0, original, remainder;

printf("Enter an integer: ");

scanf("%d", &num);

original = num;

while(num != 0) {

remainder = num % 10;

rev = rev * 10 + remainder;

num /= 10;

if(original == rev) printf("Palindrome");

else printf("Not a Palindrome");

This document now contains all programming answers, including the


remaining ones. Let me know if you need any modifications!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy