[go: up one dir, main page]

0% found this document useful (0 votes)
15 views65 pages

Program 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 65

98 cppm het kotadiya

Program 1:- write a program to print simple print


“HELLO WORLD”
----------------------------------------------------------------------
---------------
#include <stdio.h>

#include<conio.h>

int main() {

printf("Hello, World!\n");

return 0;

OUTPUT

:-

Program2:-write a program to findnaddition of two


number
----------------------------------------------------------------------
---------------
#include <stdio.h>
#include<conio.h>
98 cppm het kotadiya

int main() {
int num1, num2, sum;
printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

sum = num1 + num2;

printf("The sum of %d and %d is %d\n", num1,


num2, sum);

return 0;
}

OUTPUT
98 cppm het kotadiya

PROGRAM 3:- write a program to find the calculate


simple interest
----------------------------------------------------------------------
---------------
#include <stdio.h>
#include<conio.h>

int main() {
float principal, rate, time, simpleInterest;

printf("Enter the principal amount: ");


scanf("%f", &principal);

printf("Enter the annual interest rate (in


percentage): ");
scanf("%f", &rate);

printf("Enter the time period (in years): ");


scanf("%f", &time);

simpleInterest = (principal * rate * time) / 100;


98 cppm het kotadiya

printf("The simple interest is: %.2f\n",


simpleInterest);

return 0;
}

OUTPUT

PROGRAM 4:-write a program to find is odd or even


#include <stdio.h>
#include<conio.h>

int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
98 cppm het kotadiya

if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}

return 0;
}
OUTPUT

PROGRAM 5:- write aprogram to print 1 to 10


numbers
#include<stdio.h>
#include<conio.h>
98 cppm het kotadiya

int main() {

int i;
for (i = 1; i <= 10; i++) {
printf("%d\n", i);
}

return 0;
}

OUTPUT

PROGRAM 6:- write a program to print table of any


numbers
#include <stdio.h>
98 cppm het kotadiya

#include <conio.h>

int main() {
int number, i;

printf("Enter a number: ");


scanf("%d", &number);

printf("Multiplication table for %d:\n", number);


for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number *
i);
}

return 0;
}
OUTPUT
98 cppm het kotadiya

PROGRAM 7:- write a program to print following


pattern
#include <stdio.h>

int main() {
int i, j;

// Loop for the number of rows


for (i = 1; i <= 3; i++) {
// Loop to print the required number of
columns
for (j = 1; j <= i; j++) {
printf("%d", i);
}
// Move to the next line after each row
98 cppm het kotadiya

printf("\n");
}

return 0;
}

OUTPUT

PROGRAM 8:- write a program to print following


pattern
#include <stdio.h>
#include <conio.h>

int main() {
int i, j;

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


98 cppm het kotadiya

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


printf("*");
}
printf("\n");
}

return 0;
}
OUTPUT

PROGRAM 9:-write a program to print following


pattern
#include <stdio.h>
#include <conio.h>
98 cppm het kotadiya

int main() {
int i, j;
for (i = 3; i >= 1; i--) {
for (j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}

return 0;
}
OUTPUT

PROGRAM 10:- write a program to print following


pattern
#include <stdio.h>
98 cppm het kotadiya

#include<conio.h>

int main() {
int i, j;

int rows = 5;

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


// Print leading spaces
for (j = 0; j < i; j++) {
printf(" ");
}

printf("*");

for (j = 0; j < (2 * (rows - i - 1) - 1); j++) {


printf(" ");
}

if (i < rows - 1) {
printf("*");
}
98 cppm het kotadiya

printf("\n");
}

return 0;
}

OUTPUT

PROGRAM 11:-write a program to print following


pattern
#include<stdio.h>
#include<conio.h>
int main() {
int i, j;

int rows = 3;
98 cppm het kotadiya

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


for (j = 1; j <= i; j++) {
printf("%d ", j);
}
printf("\n");
}

return 0;
}

OUTPUT

PROGRAM 12:-write a program to find square of


number
#include <stdio.h>
98 cppm het kotadiya

#include<conio.h>

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

int square = number * number;

printf("The square of %d is %d.\n", number,


square);

return 0;
}
OUTPUT
98 cppm het kotadiya

PROGRAM:-13 write a program to find cube of


number

#include <stdio.h>
#include <conio.h>

int main() {

int number;

printf("Enter a number: ");


scanf("%d", &number);

int cube = number * number * number;


98 cppm het kotadiya

printf("The cube of %d is %d.\n", number, cube);

return 0;
}

OUTPUT

PROGRAM 14:- write a program to area of


rectangle
#include <stdio.h>
#include <conio.h>

int main() {
float width, height;

printf("Enter the width of the rectangle: ");


98 cppm het kotadiya

scanf("%f", &width);

printf("Enter the height of the rectangle: ");


scanf("%f", &height);

float area = width * height;

printf("The area of the rectangle is %.2f.\n",


area);

return 0;
}
OUTPUT

PROGRAM 15:- write a program to area of triangle


#include <stdio.h>
98 cppm het kotadiya

#include<conio.h>

int main() {
float base, height;

printf("Enter the base of the triangle: ");


scanf("%f", &base);

printf("Enter the height of the triangle: ");


scanf("%f", &height);

float area = 0.5 * base * height;

printf("The area of the triangle is %.2f.\n", area);

return 0;
}
OUTPUT
98 cppm het kotadiya

PROGRAM 16:- write a program to check prime


number or not
#include <stdio.h>
#include<conio.h>

int main() {
int number, i;
int isPrime = 1;

printf("Enter a number: ");


scanf("%d", &number);

if (number <= 1) {
isPrime = 0;
} else {
98 cppm het kotadiya

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


if (number % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime) {
printf("%d is a prime number.\n", number);
} else {
printf("%d is not a prime number.\n",
number);
}

return 0;
}
OUTPUT
98 cppm het kotadiya

PROGRAM 17:-write a program to check Armstrong


number or not.
#include <stdio.h>
#include <math.h>

int is_armstrong_number(int num) {


int original_num, remainder, result = 0,
num_digits = 0;

original_num = num;
while (original_num != 0) {
original_num /= 10;
num_digits++;
}
98 cppm het kotadiya

original_num = num;
while (original_num != 0) {
remainder = original_num % 10;
result += pow(remainder, num_digits);
original_num /= 10;
}

if (result == num) {
return 1;
} else {
return 0;
}
}

int main() {
int number;

printf("Enter a number: ");


scanf("%d", &number);

if (is_armstrong_number(number)) {
98 cppm het kotadiya

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


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

return 0;
}

OUTPUT

PROGRAM 18:-write a program to check palindrome


number or not
#include <stdio.h>
#include <conio.h>
98 cppm het kotadiya

int is_palindrome_number(int num) {


int original_num = num, reversed_num = 0,
remainder;

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

if (original_num == reversed_num) {
return 1;
} else {
return 0;
}
}

int main() {
int number;
98 cppm het kotadiya

printf("Enter a number: ");


scanf("%d", &number);

if (is_palindrome_number(number)) {
printf("%d is a palindrome number.\n",
number);
} else {
printf("%d is not a palindrome number.\n",
number);
}

return 0;
}

OUTPUT
98 cppm het kotadiya

PROGRAM 19:- write a program to display


Fibonacci series
#include <stdio.h>
#include <conio.h>
void fibonacci_series(int n) {
int first = 0, second = 1, next, i;

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


printf("%d ", first);
next = first + second;
first = second;
second = next;
}
}

int main() {
int terms;

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


scanf("%d", &terms);

printf("Fibonacci series: ");


98 cppm het kotadiya

fibonacci_series(terms);

return 0;
}
OUTPUT

PROGRAM 20:- write a program to find factorial or


given number
#include <stdio.h>
#include <conio.h>

int main() {
int n, i;
unsigned long long factorial = 1;

printf("Enter an integer: ");


98 cppm het kotadiya

scanf("%d", &n);

if (n < 0)
printf("Factorial of a negative number doesn't
exist.\n");
else {
for (i = 1; i <= n; ++i) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", n, factorial);
}

return 0;
}

OUTPUT
98 cppm het kotadiya

PROGRAM 21 :- write a program to swapping two


numbers using third variable

#include <stdio.h>
#include<conio.h>

int main() {
int a, b, temp;

printf("Enter two numbers: ");


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

temp = a;
a = b;
b = temp;

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

return 0;
}
OUTPUT
98 cppm het kotadiya

PROGRAM 22:- :- write a program to swapping two


numbers without using third variable
#include <stdio.h>

int main() {
int a, b;

printf("Enter two numbers: ");


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

a = a + b;
b = a - b;
a = a - b;

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


98 cppm het kotadiya

return 0;
}
OUTPUT

PROGRAM 23 :- write a program to input any


character & check whether it is number, char or
character
#include <stdio.h>
#include <conio.h>

int main() {
char ch;

printf("Enter any character: ");


98 cppm het kotadiya

scanf("%c", &ch);

if (isdigit(ch))
printf("'%c' is a digit.\n", ch);
else if (isalpha(ch))
printf("'%c' is an alphabet.\n", ch);
else
printf("'%c' is a special character.\n", ch);

return 0;
}
OUTPUT

PROGRAM 24:- write a program to use of while loop


which display sun of first 10 natural numbers
#include <stdio.h>
98 cppm het kotadiya

#include <conio.h>

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

while (i <= 10) {


sum += i;
i++;
}

printf("The sum of the first 10 natural numbers


is: %d\n", sum);

return 0;
}
OUTPUT
98 cppm het kotadiya

PROGRAM 25:- write a program to check whether


an input alphabet is a vowel or not both lower
upper case are checked

#include <stdio.h>
#include <conio.h>
int main() {
char ch;

printf("Enter an alphabet: ");


scanf("%c", &ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' ||


ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' ||
ch == 'U') {
printf("%c is a vowel.\n", ch);
} else {
printf("%c is not a vowel.\n", ch);
}
98 cppm het kotadiya

return 0;
}
OUTPUT

PROGRAM 26 :- write a menu driven program to


perform addition, subtraction, multiplication and
using switch case
#include <stdio.h>
#include <conio.h>

int main() {
int num1, num2, choice;
printf("Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("Enter your choice (1-3): ");
98 cppm het kotadiya

scanf("%d", &choice);

printf("Enter two numbers: ");


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

switch (choice) {
case 1:
printf("Result: %d + %d = %d\n", num1,
num2, num1 + num2);
break;
case 2:
printf("Result: %d - %d = %d\n", num1,
num2, num1 - num2);
break;
case 3:
printf("Result: %d * %d = %d\n", num1,
num2, num1 * num2);
break;
default:
printf("Invalid choice.\n");
}

return 0;
98 cppm het kotadiya

}
OUTPUT

Program 27:- WRITE A MENU DRIVEN PROGRAME


TO PERFROME
#include <stdio.h>
#include <math.h>

void fibonacci_series(int n);


unsigned long long factorial(int n);
int is_prime(int n);
int is_palindrome(int n);
int is_armstrong(int n);
double area_of_circle(double radius);
double area_of_rectangle(double length, double
width);
98 cppm het kotadiya

double area_of_triangle(double base, double


height);

int main() {
int choice;
int num;
double radius, length, width, base, height;

while (1) {

printf("\nMenu:\n");
printf("1. Fibonacci Series\n");
printf("2. Factorial Number\n");
printf("3. Prime Number Check\n");
printf("4. Palindrome Number Check\n");
printf("5. Armstrong Number Check\n");
printf("6. Area of Circle\n");
printf("7. Area of Rectangle\n");
printf("8. Area of Triangle\n");
printf("9. Exit\n");
printf("Enter your choice (1-9): ");
scanf("%d", &choice);
98 cppm het kotadiya

switch (choice) {
case 1:
printf("Enter the number of terms for
Fibonacci series: ");
scanf("%d", &num);
fibonacci_series(num);
break;

case 2:
printf("Enter a number to find its
factorial: ");
scanf("%d", &num);
printf("Factorial of %d is %llu\n", num,
factorial(num));
break;

case 3:
printf("Enter a number to check if it is
prime: ");
scanf("%d", &num);
if (is_prime(num)) {
98 cppm het kotadiya

printf("%d is a prime number.\n",


num);
} else {
printf("%d is not a prime number.\n",
num);
}
break;

case 4:
printf("Enter a number to check if it is a
palindrome: ");
scanf("%d", &num);
if (is_palindrome(num)) {
printf("%d is a palindrome number.\n",
num);
} else {
printf("%d is not a palindrome
number.\n", num);
}
break;

case 5:
printf("Enter a number to check if it is an
Armstrong number: ");
98 cppm het kotadiya

scanf("%d", &num);
if (is_armstrong(num)) {
printf("%d is an Armstrong number.\n",
num);
} else {
printf("%d is not an Armstrong
number.\n", num);
}
break;

case 6:
printf("Enter the radius of the circle: ");
scanf("%lf", &radius);
printf("Area of the circle: %.2lf\n",
area_of_circle(radius));
break;

case 7:
printf("Enter the length and width of the
rectangle: ");
scanf("%lf %lf", &length, &width);
printf("Area of the rectangle: %.2lf\n",
area_of_rectangle(length, width));
98 cppm het kotadiya

break;

case 8:
printf("Enter the base and height of the
triangle: ");
scanf("%lf %lf", &base, &height);
printf("Area of the triangle: %.2lf\n",
area_of_triangle(base, height));
break;

case 9:
printf("Exiting...\n");
return 0;

default:
printf("Invalid choice. Please select a
number between 1 and 9.\n");
break;
}
}

return 0;
}
98 cppm het kotadiya

void fibonacci_series(int n) {
int first = 0, second = 1, next;
printf("Fibonacci series up to %d terms:\n", n);
for (int i = 1; i <= n; ++i) {
if (i == 1) {
printf("%d ", first);
continue;
}
if (i == 2) {
printf("%d ", second);
continue;
}
next = first + second;
first = second;
second = next;
printf("%d ", next);
}
printf("\n");
}
98 cppm het kotadiya

unsigned long long factorial(int n) {


if (n == 0 || n == 1) return 1;
unsigned long long result = 1;
for (int i = 2; i <= n; ++i) {
result *= i;
}
return result;
}

int is_prime(int n) {
if (n <= 1) return 0;
if (n <= 3) return 1;
if (n % 2 == 0 || n % 3 == 0) return 0;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return 0;
}
return 1;
}
98 cppm het kotadiya

int is_palindrome(int n) {
int original = n, reversed = 0, remainder;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
return (original == reversed);
}

int is_armstrong(int n) {
int original = n, sum = 0, digit, count = 0;
while (original != 0) {
original /= 10;
count++;
}
original = n;
while (original != 0) {
digit = original % 10;
sum += pow(digit, count);
original /= 10;
98 cppm het kotadiya

}
return (sum == n);
}

double area_of_circle(double radius) {


return M_PI * radius * radius;
}

double area_of_rectangle(double length, double


width) {
return length * width;
}

double area_of_triangle(double base, double


height) {
return 0.5 * base * height;
OUTPUT
98 cppm het kotadiya

PROGRAM 28 :- write a program to find average of


marks using array
#include <stdio.h>
#include <conio.h>

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

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


scanf("%d", &n);

float marks[n];
98 cppm het kotadiya

printf("Enter marks for each student:\n");


for (i = 0; i < n; i++) {
printf("Student %d: ", i + 1);
scanf("%f", &marks[i]);
sum += marks[i];
}

average = sum / n;
printf("The average marks are: %.2f\n",
average);

return 0;
}
OUTPUT
98 cppm het kotadiya

PROGRAM 29:- write a program to concatenate two


string & find length of string
#include <stdio.h>
#include <conio.h>

int main() {
char str1[100], str2[100], result[200];

printf("Enter the first string: ");


fgets(str1, sizeof(str1), stdin);
str1[strcspn(str1, "\n")] = 0; // Remove newline
character

printf("Enter the second string: ");


fgets(str2, sizeof(str2), stdin);
str2[strcspn(str2, "\n")] = 0; // Remove newline
character

strcpy(result, str1);
strcat(result, str2);

int length = strlen(result);


98 cppm het kotadiya

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


printf("Length of the concatenated string: %d\
n", length);

return 0;
}
OUTPUT

PROGRAM 30 : write a program to accessing a


variable through its pointer
#include <stdio.h>
#include <conio.h>

int main() {
int num = 42;
98 cppm het kotadiya

int *ptr = &num;

printf("Value of num: %d\n", num);


printf("Value of num accessed through pointer:
%d\n", *ptr);

*ptr = 100;
printf("Modified value of num: %d\n", num);

return 0;
}
OUTPUT

PROGRAM 31 :- write a program to display array


elements in matrix form
#include <stdio.h>
98 cppm het kotadiya

int main() {
int rows, cols;

printf("Enter number of rows: ");


scanf("%d", &rows);

printf("Enter number of columns: ");


scanf("%d", &cols);

int matrix[rows][cols];

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


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

printf("Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", matrix[i][j]);
98 cppm het kotadiya

}
printf("\n");
}

return 0;
}

PROGRAM 32:- write a program to used of one


dimensional array to print 1 to given N number

#include <stdio.h>
98 cppm het kotadiya

int main() {
int N;

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


scanf("%d", &N);

if (N <= 0) {
printf("Please enter a positive integer.\n");
return 1;
}

int numbers[N];

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


numbers[i] = i + 1;
}

printf("Numbers from 1 to %d are:\n", N);


for (int i = 0; i < N; i++) {
printf("%d ", numbers[i]);
}
98 cppm het kotadiya

printf("\n");

return 0;
}

OUTPUT

PROGRAM :-33 write a program to shorting array


in ascending order #include <stdio.h>

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


for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
98 cppm het kotadiya

arr[j] = arr[j + 1];


arr[j + 1] = temp;
}
}
}
}

int main() {
int array[] = {12, 4, 56, 1, 78, 23, 45};
int n = sizeof(array) / sizeof(array[0]);

sortArray(array, n);

printf("Sorted Array: ");


for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}

return 0;
}
98 cppm het kotadiya

PROGRAM 34 :- write a program to shorting array


in decending order #include <stdio.h>

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


for (int i = 0; i < n - 1; i++) {
98 cppm het kotadiya

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


if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main() {
int array[] = {12, 4, 56, 1, 78, 23, 45};
int n = sizeof(array) / sizeof(array[0]);

sortArrayDescending(array, n);

printf("Sorted Array in Descending Order: ");


for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}

return 0;
98 cppm het kotadiya

PROGRAM 35:-write a program to array in selection


sort
#include <stdio.h>

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


for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
98 cppm het kotadiya

minIndex = j;
}
}
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}

int main() {
int array[] = {29, 10, 14, 37, 13};
int n = sizeof(array) / sizeof(array[0]);

selectionSort(array, n);

printf("Sorted Array: ");


for (int i = 0; i < n; i++) {
printf("%d ", array[i]);
}
98 cppm het kotadiya

return 0;
}

PROGRAM 36:-write a program to used array in


linear seach
#include <stdio.h>

int linearSearch(int arr[], int n, int target) {


for (int i = 0; i < n; i++) {
if (arr[i] == target) {
98 cppm het kotadiya

return i;
}
}
return -1;
}

int main() {
int array[] = {23, 45, 12, 56, 78, 1, 90};
int n = sizeof(array) / sizeof(array[0]);
int target;

printf("Enter the number to search: ");


scanf("%d", &target);

int result = linearSearch(array, n, target);

if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
98 cppm het kotadiya

return 0;
}

PROGRAM 37 :- write a program to used character


single dimensional array
#include <stdio.h>

int main() {s
98 cppm het kotadiya

char str[100];

printf("Enter a string: ");


scanf("%s", str);

printf("You entered: %s\n", str);

return 0;
}

You might also like