0% found this document useful (0 votes)
19 views15 pages

Progran

The document contains multiple C programming examples including string reversal, Armstrong number check, prime number generation, standard deviation calculation, and Fibonacci series generation. It also includes explanations of the logic behind each program and how they work. The programs demonstrate various concepts such as loops, recursion, and array manipulation.

Uploaded by

arbindahal07
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)
19 views15 pages

Progran

The document contains multiple C programming examples including string reversal, Armstrong number check, prime number generation, standard deviation calculation, and Fibonacci series generation. It also includes explanations of the logic behind each program and how they work. The programs demonstrate various concepts such as loops, recursion, and array manipulation.

Uploaded by

arbindahal07
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/ 15

6h) Write a program to reverse any string?

#include <stdio.h>

#include <string.h>

int main() {

char string[100];

printf("Enter a string: ");

scanf("%s", string);

int len = strlen(string);

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

char temp = string[i];

string[i] = string[len - i - 1];

string[len - i - 1] = temp;

printf("Reversed string: %s\n", string);

return 0;

Bujna ko lagi

I=0

char temp = string[0]; // temp = 'h'

string[0] = string[4]; // string[0] = 'o'

string[4] = temp; // string[4] = 'h'

output: o e l l h

i=1

char temp = string[1]; // temp = 'e'

string[1] = string[3]; // string[1] = 'l'

string[3] = temp; // string[3] = 'e'

output: o l l e h

Loop stops (i = 2 is the middle, so no need to swap further).


6e) Write a program in C that exactly simulates the task of the
function strrev()
#include <stdio.h>

#include <string.h>

int main() {

char str[100];

// Take input string

printf("Enter a string: ");

gets(str); // Using gets for simplicity

// Reverse the string using strrev() and print it

printf("Reversed string: %s\n", strrev(str));

return 0;

5a)
Write a program to check
whether a given n-digit number is an Armstrong number or not

#include <stdio.h>

#include <math.h>

int main() {

int num, temp, sum = 0, n = 0;

// Input number

printf("Enter a number: ");

scanf("%d", &num);

temp = num;

// Count digits
while (temp) {

temp /= 10;

n++;

temp = num;

// Calculate the Armstrong sum

while (temp) {

sum += pow(temp % 10, n);

temp /= 10;

// Check if Armstrong number

if (sum == num)

printf("%d is an Armstrong number.\n", num);

else

printf("%d is not an Armstrong number.\n", num);

return 0;

Bujam hai ta:

while (temp) {

temp /= 10;

n++;

For num = 153:

 Initially, temp = 153 and n = 0.


 After the first iteration, temp = 15, n = 1.
 After the second iteration, temp = 1, n = 2.
 After the third iteration, temp = 0, n = 3.

For num = 153 and n = 3:

 Initially, sum = 0 and temp = 153.


 First iteration: temp % 10 = 3, so pow(3, 3) = 27. sum = 0 + 27 = 27. Then, temp
= 15.
 Second iteration: temp % 10 = 5, so pow(5, 3) = 125. sum = 27 + 125 = 152. Then,
temp = 1.
 Third iteration: temp % 10 = 1, so pow(1, 3) = 1. sum = 152 + 1 = 153. Then,
temp = 0.

5e) Write a program to display first N Prime numbers, where N is


read from a user.

#include <stdio.h>

int main() {

int N, num = 2, count = 0, i;

// Ask the user for the number of primes to display

printf("Enter the value of N: ");

scanf("%d", &N);

// Loop to find and display N prime numbers

while (count < N) {

int isPrime = 1; // Assume num is prime

// Check if the number is divisible by any number between 2 and num-1

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

if (num % i == 0) { // If num is divisible by i, it's not prime

isPrime = 0;

break;

}
// If the number is prime, display it and increment count

if (isPrime) {

printf("%d ", num);

count++;

// Move to the next number

num++;

return 0;

Code bujam hai:

 the program starts checking from num = 2.


 For each num, it checks if it's divisible by any number from 2 to sqrt(num) (since a number
can't have a divisor larger than its square root).

 If num % i == 0, it means num is divisible by i, so it is not a prime number.

// Loop to find and display N prime numbers

while (count < N) {

int isPrime = 1; // Assume num is prime

 The while (count < N) loop continues running until we find N prime numbers. The loop
will stop when count reaches N.
 The variable isPrime is initially set to 1 (assuming the number is prime) for each number we
check.

// Check if the number is divisible by any number between 2 and num-1

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

if (num % i == 0) { // If num is divisible by i, it's not prime

isPrime = 0;

break;

 We use a for loop to check if the number num is divisible by any number from 2 to
sqrt(num).

 The condition i * i <= num is used because we only need to check divisibility up to the
square root of the number. If a number is divisible by a value larger than its square root,
the corresponding smaller factor would already have been checked.

 If we find that num is divisible by any value i (i.e., num % i == 0), num is not prime, so we
set isPrime = 0 and exit the loop with break.

// If the number is prime, display it and increment count

if (isPrime) {

printf("%d ", num);

count++;

 If isPrime is still 1 after checking all divisors, it means the number is prime, so we print the
number (printf("%d ", num);).

 We increment the count by 1 because we've found a prime number.


6a).Write a program to read marks of

48 students in a class and calculate the standard deviation of


marks.

#include <stdio.h>

#include <math.h>

int main() {

int marks[48], sum = 0;

double mean, variance = 0, stdDeviation;

printf("Enter marks of 48 students:\n");

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

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

sum += marks[i];

mean = sum / 48.0;

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

variance += (marks[i] - mean) * (marks[i] - mean);

stdDeviation = sqrt(variance / 48.0);

printf("Standard Deviation: %.2f\n", stdDeviation);


return 0;

Code bujam la

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

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

sum += marks[i];

 This loop runs 48 times to input marks for 48 students.


 Each mark is entered by the user and stored in the marks array.

 At the same time, the sum of the marks is calculated (sum += marks[i];).

marks[0] = 60, marks[1] = 62, marks[2] = 65, ..., marks[47] = 90

sum = 3100 (sum of all 48 marks)

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

variance += (marks[i] - mean) * (marks[i] - mean);

 The variance is calculated by finding the squared difference of each mark from the
mean and adding these squared differences together.
 The formula for variance is:

variance=∑(xi−mean)2/N

6b).Write a program to sort the characters of a string in


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

int main() {
char str[100], temp;
int i, j, len;

printf("Enter a string: ");


scanf("%s", str);

len = strlen(str);

// Sorting using Bubble Sort


for (i = 0; i < len - 1; i++) {
for (j = i + 1; j < len; j++) {
if (str[i] > str[j]) { // Swap if current char is greater than next char
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
}

printf("Sorted string: %s\n", str);


return 0;
}

Code bujam hai

h vs e Swap e h l l o
h vs l No Swap e h l l o
l vs l No Swap e h l l o
l vs o No Swap e h l l o

7b) Write a recursive function to display the terms of the Fibonacci series
from 1 to n.

#include <stdio.h>

void fibonacci(int n, int a, int b) {


if (n <= 0) return;

printf("%d ", a);

fibonacci(n - 1, b, a + b);

int main() {

int n;

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

scanf("%d", &n);

fibonacci(n, 0, 1); // Start with 0 and 1

printf("\n");

return 0;

7d).Write a program to define a function that checks whether any


integer is prime or not. Use this

function to display all the prime numbers between 1 to 100.

#include <stdio.h>

// Function to check if a number is prime

int isPrime(int num) {

if (num <= 1) return 0; // Not prime if less than or equal to 1

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

if (num % i == 0) return 0; // Not prime if divisible by any number


}

return 1; // Prime

int main() {

printf("Prime numbers between 1 and 100 are:\n");

// Check each number from 1 to 100

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

if (isPrime(i)) {

printf("%d ", i); // Print prime number

return 0;

7e ) Write a program to read an integer and pass it to


a function that calculates the number of even and odd digits
present in it. Display the results from main function.

#include <stdio.h>
void countEvenOdd(int num) {
int evenCount = 0, oddCount = 0;
while (num != 0) {
int digit = num % 10;

if (digit % 2 == 0)
evenCount++;
else
oddCount++;

num /= 10; }
printf("Even digits: %d\n", evenCount);
printf("Odd digits: %d\n", oddCount);
}

int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
countEvenOdd(num);

return 0;
}

7f).Write a program using recursion to compute the sum of the


following series:
12 – 32 + 52 – 72…………. + (-1)n+1 (2n-1)2

#include <stdio.h>

int sumSeries(int n) {
if (n == 1) {
return 1; }
int term = (2 * n - 1) * (2 * n - 1);
if (n % 2 == 0) {
return -term + sumSeries(n - 1); } else {
return term + sumSeries(n - 1);
}
}
int main() {
int n;
printf("Enter the number of terms (n): ");
scanf("%d", &n);
int result = sumSeries(n);
printf("Sum of the series is: %d\n", result);

return 0;
}
5f)Write a program to display the chess board pattern. [Hint:
Display “\xdb” for white color and blank
for black color]
#include <stdio.h>
void printChessBoard(int size) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if ((i + j) % 2 == 0) {
printf("\xdb ");
} else {
printf(" "); )
}
}
printf("\n");
}
}
int main() {
int size = 8;
printChessBoard(size);
return 0;
}

6c).Write a program to input any 2D array and display in matrix


form using
pointer notation.
#include <stdio.h>

int main() {
int rows, cols;
printf("Enter number of rows and columns: ");
scanf("%d %d", &rows, &cols);

int arr[rows][cols];

// Input elements in the 2D array


printf("Enter elements of the matrix:\n");
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
scanf("%d", (*(arr + i) + j));

// Display matrix using pointer notation


printf("The matrix is:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++)
printf("%d ", *(*(arr + i) + j));
printf("\n");
}
return 0;
}

You might also like