0% found this document useful (0 votes)
17 views37 pages

NadamAIMLB2Cfile

Uploaded by

farazansari99914
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)
17 views37 pages

NadamAIMLB2Cfile

Uploaded by

farazansari99914
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/ 37

Index:

S.no Pg.no Program Name


Program: C program to find sum and average of three numbers
#include<stdio.h>
void main ()
{
printf("enter three numbers");
float a,b,c,sum,avg;
scanf("%f%f%f",&a,&b,&c);
sum= a+b+c;
avg= sum/3;
printf("%f is sum \n %f is
average",sum,avg);
}

Output:
Program: C program to find the sum of individual digits of a given positive
Integer
#include <stdio.h>
void main()
{
printf("enter the positive integer");
int a,b;
int sum=0;
scanf("%d",&a);
while(a>=1)
{
b=a%10;
sum= sum+b;
a=a/10;
}
printf("sum of digits is %d",sum);
}

Output:
Program: C program to generate the first n terms of the Fibonacci
Sequence
#include <stdio.h>
void main ()
{
int n,c;
int b=1;
int a=0;
printf("enter the number of terms");
scanf("%d",&n);

for(int i=0;i<n;i++)
{
c=a+b;
printf("%d\n",c);
a=b;
b=c;
}

Output:
Program: C program to generate prime numbers between 1 to n
#include <stdio.h>
int prime(int n) {
if (n <= 1) {
return 0;
}
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
return 0;
}
}
return 1;
}
int main() {
int n;

printf("Enter a positive integer n: ");


scanf("%d", &n);
if (n <= 1) {
printf("There are no prime numbers in the
range 1 to %d\n", n);
} else {
printf("Prime numbers between 1 and %d
are:\n", n);
for (int i = 2; i <= n; i++) {
if (prime(i)) {
printf("%d ", i);
}
}
printf("\n");
}
return 0;
}

Output:
Program: C program to Check whether given number is Armstrong Number or
Not
#include<stdio.h>
void main(){
int a,n,d=0,p=1,sum=0;
printf("enter the number:");
scanf("%d",&n);
a=n;
while (a!=0)
{
a=a/10;
d++;
}
a=n;
int x[d];
for (int i=1; i<=d;i++)
{
x[i]=a%10;
a=a/10;
}
int y[d];
for (int i=1;i<=d;i++)
{
for(int j=1; j<=d;j++)
{
p=p*x[i];
}
y[i]=p;
p=1;
}
for(int i=1;i<=d;i++)
{
sum=sum+y[i];
}
if(sum==n)
{
printf ("armstong");
}
else{
printf("not armstrong");
}
}
Output:
Program: C program to evaluate algebraic expression (ax+b)/(ax-b)
#include<stdio.h>
void main()
{
float a,b,x,p,q,ans;
printf("enter the numbers a,b,x\n");
scanf("%f%f%f",&a,&b,&x);
p=(x*a)+b;
q=(x*a)-b;
ans=p/q;
printf("%f",ans);
}

Output:
Program: C program to check whether given number is perfect number or
Not
#include <stdio.h>

void main() {
int a, j = 0, sum = 0;
printf("Enter the number:");
scanf("%d", &a);
int x[a];
for (int i = 1; i < a; i++) {
if (a % i == 0) {
x[j] = i;
j++;
}
}

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


sum += x[i];
}

if (sum == a) {
printf("Perfect\n");
}
else {
printf("Not perfect\n");
}
}

Output:
Program: C program to check whether given number is strong number
or not
#include <stdio.h>

int main() {
int n, d = 0, pd, sum = 0;
printf("Enter the number: ");
scanf("%d", &n);
int a = n;
int x[1000];
while (a > 0) {
a = a / 10;
d++; }
a = n;
for (int i = 0; i < d; i++) {
x[i] = a % 10;
a = a / 10; }
for (int i = 0; i < d; i++) {
pd = 1;
int digit = x[i];
while (digit >= 1) {
pd = pd * digit;
digit = digit - 1;}x[i] = pd;}
for (int i = 0; i < d; i++) {sum = sum +
x[i];}
if (sum == n) {printf("Strong\n");} else
{printf("Not Strong\n");}
return 0;
}

Output:
Program: C program to find the roots of a quadratic equation
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, d, r1, r2;
printf("Enter coefficients a, b, and c
(compared to the form ax^2+bx+c=0): ");
scanf("%lf %lf %lf", &a, &b, &c);
d= (b*b)-(4*a*c);
if (d> 0) {
r1= (-b+sqrt(d))/(2*a);
r2= (-b-sqrt(d))/(2*a);
printf("Roots are real and different.\n");
printf("Root 1 = %.2lf\n", r1);
printf("Root 2 = %.2lf\n", r2);
} else if (d== 0) {
r1 = r2 = -b / (2 * a);
printf("Roots are real and equal.\n");
printf("Root 1 = Root 2 = %.2lf\n", r1);
} else {
double realPart = -b/(2*a);
double imaginaryPart=sqrt(-d)/(2*a);
printf("Roots are complex and
different.\n");
printf("Root 1 = %.2lf + %.2lfi\n",
realPart, imaginaryPart);
printf("Root 2 = %.2lf - %.2lfi\n", realPart,
imaginaryPart);
}
return 0;
}

Output:
Program: C program perform arithmetic operations using switch statement
#include <stdio.h>
int main() {
char operator;
double n1, n2, r;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &n1, &n2);
switch (operator) {
case '+':
r= n1 + n2;
printf("%.2lf + %.2lf = %.2lf\n", n1, n2, r);
break;
case '-':
r = n1 - n2;
printf("%.2lf - %.2lf = %.2lf\n", n1, n2, r);
break;
case '*':
r = n1 * n2;
printf("%.2lf * %.2lf = %.2lf\n", n1, n2, r);
break;
case '/':
if (n2 != 0) {
r = n1 / n2;
printf("%.2lf / %.2lf = %.2lf\n", n1, n2, r);
} else {
printf("cant divide by zero >:( \n");
}
break;
default:
printf("Error: Invalid operator\n");
}
return 0;
}
Output:
Program: C program to find factorial of a given integer using
non-recursive function
#include<stdio.h>
int factorial(int n)
{
int pd=1;
while(n>=1)
{pd=pd*n;
n=n-1;}
return pd;
}
void main()
{
int n;
printf("Enter the number: ");
scanf("%d",&n);
printf("%d is the factorial",factorial(n));
}

Output:
Program: C program to find both the largest and smallest number in a list
of integers
#include<stdio.h>
void main() {
int a,b, max, min;
int k[100];
printf("enter the number of entries");
scanf("%d",&a);
printf("enter the numbers");
for(int i=0; i<a;i++)
{
scanf("%d",&k[i]);
}
max=min=k[0];
for(int i=1; i<a; i++)
{
if(max<k[i])
{
max=k[i];
}
if (min>k[i])
{
min=k[i];
}
}
printf("%d is largest and %d is
smallest",max,min);
}

Output:
Program: C Program to Sort the Array in an Ascending Order
#include <stdio.h>

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


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

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


if (arr[j] < arr[min_index]) {
min_index = j;}}if (min_index != i) {
arr[i] = arr[i] + arr[min_index];
arr[min_index] = arr[i] - arr[min_index];
arr[i] = arr[i] - arr[min_index];}}}
int main() {
int n;
printf("Enter the number of elements in the
array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]); }
selectionSort(arr, n);
printf("Sorted array in ascending order:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]); }return 0;}

Output:
Program: C Program to find whether given matrix is symmetric or not
#include<stdio.h>
void main(){
int n;
int a[100][100];
printf("enetr the order of the square matrix");
scanf("%d",&n);
printf("enter the terms of matrix rowwise");
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
scanf("%d",&a[i][j]);
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n ; j++)
{
if(a[i][j]!=a[j][i])
{
printf("not symmetric");
return;
}
}}
printf("symmetric");
}

Output:
Program: C program to perform addition of two matrices
#include<stdio.h>
void main(){
int a,b,c,d;
int x[100][100],y[100][100],z[100][100];
printf("enter the order of both the matrices ");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a!=c){printf("not possible");return;}
if(b!=d){printf("not possible");return;}
printf("enter the terms of matrix 1 rowwise");
for(int i=0; i<a; i++){
for(int j=0; j<b; j++) {
scanf("%d",&x[i][j]); }
} printf("terms of matrix 2 rowwise");
for(int i=0; i<c; i++){
{for(int j=0; j<d; j++)
{
scanf("%d",&y[i][j]);
}
}}
for(int i=0; i<c; i++)
{
for(int j=0; j<d; j++)
{
z[i][j]=x[i][j]+y[i][j];
printf("%d\t",z[i][j]);
}
printf("\n");
}
}

Output:
Program: C program that uses functions to perform Multiplication of Two Matrices
#include <stdio.h>
void main() {
int a, b, c, d;
int x[100][100], y[100][100], z[100][100];
printf("Enter the order of matrix 1 (rows and
columns): ");
scanf("%d %d", &a, &b);
printf("Enter the order of matrix 2 (rows and
columns): ");
scanf("%d %d", &c, &d);
if (b != c) {
printf("Matrix multiplication is not
possible.\n");
return;
}
printf("Enter the elements of matrix 1 row-
wise:\n");
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
scanf("%d", &x[i][j]);
}
}
printf("Enter the elements of matrix 2 row-
wise:\n");
for (int i = 0; i < c; i++) {
for (int j = 0; j < d; j++) {
scanf("%d", &y[i][j]);}}for (int i = 0; i
< a; i++) {
for (int j = 0; j < d; j++) {
z[i][j] = 0;
for (int k = 0; k < b; k++) {
z[i][j] += x[i][k] * y[k][j];
}} }printf("\nResultant matrix after
multiplication:\n");
for (int i = 0; i < a; i++) {
for (int j = 0; j < d; j++) {printf("%d\t",
z[i][j]);}printf("\n");}}

Output:
Program: Write a C program to find factorial of a given integer using recursive function
#include <stdio.h>
int factorial(int n)
{
if(n==1)
{
return 1;
}
else {
return n*factorial(n-1);
}
}
void main()
{
int n;
printf("enetr the number");
scanf("%d",&n);
printf("%d is factorial", factorial(n));
}

Output:
Program: Write C program to find GCD of two integers by using recursive function
#include <stdio.h>
int fGCD(int a, int b) {
if (b == 0) {
return a;
} else {
return fGCD(b, a % b);
}}
int main() {
int n1, n2;
printf("Enter first integer: ");
scanf("%d", &n1);
printf("Enter second integer: ");
scanf("%d", &n2);
if (n1 >= 0 && n2 >= 0) {
int gcd = fGCD(n1, n2);
printf("GCD of %d and %d is: %d\n", n1, n2,
gcd);
} else {
printf("GCD is not defined for negative
numbers.\n");
}
return 0;
}

Output:
Program: Write C program to find GCD of two integers by using non recursive function
#include <stdio.h>
int fGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int n1, n2;
printf("Enter first integer: ");
scanf("%d", &n1);
printf("Enter second integer: ");
scanf("%d", &n2);
if (n1 >= 0 && n2 >= 0) {
int gcd = fGCD(n1, n2);
printf("GCD of %d and %d is: %d\n", n1, n2,
gcd);
} else {
printf("GCD is not defined for negative
numbers.\n");
}
return 0;
}
Ouput:
Program: Write a C program to use function to insert a sub-string in to given main string from a
given position
#include <stdio.h>
#include <string.h>
void insertSubstring(char mainString[], char
subString[], int position) {
int mainLength = strlen(mainString);
int subLength = strlen(subString);
if (position < 0 || position > mainLength) {
printf("Invalid position for insertion.\n");
return;
}
for (int i = mainLength; i >= position; i--) {
mainString[i + subLength] = mainString[i];
}
for (int i = 0; i < subLength; i++) {
mainString[position + i] = subString[i];
}}
int main() {
char mainString[100], subString[50];
int position;
printf("Enter the main string: ");
scanf("%s", mainString);
printf("Enter the substring: ");
scanf("%s", subString);
printf("Enter the position for insertion: ");
scanf("%d", &position);
if (position < 0 || position > strlen(mainString)) {
printf("Invalid position for insertion.\n");
return 1;
}
insertSubstring(mainString, subString, position);
printf("Modified string: %s\n", mainString);
return 0;
}
Ouput:
Program: Write a C program that uses functions to delete n Characters from a given position in a
given string
#include <stdio.h>
#include <string.h>
void deleteChars(char str[], int position, int n) {
int length = strlen(str);
if (position < 0 || position >= length || n <= 0) {
printf("Invalid position or number of
characters to delete.\n");
return;
}
for (int i = position; i < length - n; i++) {
str[i] = str[i + n];
}
str[length - n] = '\0';
}

int main() {
char inputString[100];
int position, n;
printf("Enter a string: ");
scanf("%s", inputString);
printf("Enter the position for deletion: ");
scanf("%d", &position);
printf("Enter the number of characters to delete:
");
scanf("%d", &n);
deleteChars(inputString, position, n);
printf("Modified string: %s\n", inputString);
return 0;}
Output:
Program: Write a C program using user defined functions to determine whether the given string is
palindrome or not
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[]) {
int length = strlen(str);

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


if (str[i] != str[length - 1 - i]) {
return 0;
}
}
return 1; }

int main() {
char inputString[100];
printf("Enter a string: ");
scanf("%s", inputString);
if (isPalindrome(inputString)) {
printf("%s is a palindrome.\n", inputString);
} else {
printf("%s is not a palindrome.\n",
inputString);
}
return 0;
}
Output:
Program: Write a C program that displays the position or index in the main string S
where the sub string T begins, or - 1 if S doesn't contain T
#include <stdio.h>
#include <string.h>
int findSubstring(char S[], char T[]) {
int lenS = strlen(S);
int lenT = strlen(T);
for (int i = 0; i <= lenS - lenT; i++) {
int j;
for (j = 0; j < lenT; j++) {
if (S[i + j] != T[j]) {
break;}}
if (j == lenT) {
return i;
}}
return -1;
}
int main() {
char mainString[100], subString[50];
printf("Enter the main string: ");
scanf("%s", mainString);
printf("Enter the substring: ");
scanf("%s", subString);
int position = findSubstring(mainString,
subString);
if (position != -1) {
printf("Substring '%s' found at position %d in
the main string.\n", subString, position);
} else {
printf("Substring '%s' not found in the main
string.\n", subString);
}
return 0;
}
Ouput:
Program: Write C program to count the number of lines, words and characters in a given text
#include <stdio.h>
void countStats(char text[]) {
int lines = 0, words = 0, characters = 0;
int inWord = 0;
for (int i = 0; text[i] != '\0'; i++) {
characters++;
if (text[i] == '\n') {
lines++;
}
if (text[i] == ' ' || text[i] == '\t' || text[i] == '\n' ||
text[i] == '\r') {
inWord = 0;
} else if (!inWord) {
inWord = 1;
words++;
}
}
printf("Number of lines: %d\n", lines);
printf("Number of words: %d\n", words);
printf("Number of characters: %d\n", characters);
}
int main() {
char inputText[1000];
char terminator;
printf("Enter the text (Press '$' to end input):\n");
int i = 0;
while ((terminator = getchar()) != '$' && i <
sizeof(inputText) - 1) {
inputText[i++] = terminator;
}
inputText[i] = '\0';
countStats(inputText);
return 0;
}
Ouput:
Program: Write a C program to find the length of the string using Pointer
#include <stdio.h>
int stringLength(const char *str) {
const char *ptr = str;

while (*ptr != '\0') {


ptr++;
}

return ptr - str;}


int main() {
char inputString[100];
printf("Enter a string: ");
scanf("%s", inputString);
int length = stringLength(inputString);
printf("Length of the string: %d\n", length);
return 0;
}
Output:
Program: Write a C program to Display array elements using calloc( ) function
#include <stdio.h>
#include <stdlib.h>
int main() {
int *array;
int size;
printf("Enter the size of the array: ");
scanf("%d", &size);
array = (int *)calloc(size, sizeof(int));
if (array == NULL) {
printf("Memory allocation failed.\n");
return 1;}
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &array[i]); }
printf("Array elements are: ");
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
printf("\n");
free(array);
return 0;
}
Output:
Program: C Program to Calculate Total and Percentage marks of a student using
Structure
#include <stdio.h>
struct Student {
char name[50];
int marks[5];
int totalMarks;
float percentage;
};
void calculateResult(struct Student *student) {
student->totalMarks = 0;
for (int i = 0; i < 5; i++) {
student->totalMarks += student->marks[i];
}
student->percentage = (float)(student-
>totalMarks) / 5.0;
}
int main() {
struct Student student;
printf("Enter student name: ");
scanf("%s", student.name);
printf("Enter marks for 5 subjects:\n");
for (int i = 0; i < 5; i++) {
printf("Subject %d: ", i + 1);
scanf("%d", &student.marks[i]);
}
calculateResult(&student);
printf("\nStudent Result:\n");
printf("Name: %s\n", student.name);
printf("Total Marks: %d\n",
student.totalMarks);
printf("Percentage: %.2f%%\n",
student.percentage);
return 0;
}
Output:
Program: Write a C program that uses functions and structures to perform the
following operations:
i) Reading a complex number
ii) Writing a complex number
iii) Addition of two complex numbers
iv) Multiplication of two complex numbers
#include <stdio.h>
struct Complex {
float real;
float imaginary;
};
void readComplex(struct Complex *num) {
printf("Enter real part: ");
scanf("%f", &num->real);

printf("Enter imaginary part: ");


scanf("%f", &num->imaginary);
}
void writeComplex(const struct Complex *num) {
printf("%.2f + %.2fi\n", num->real, num-
>imaginary);
}
struct Complex addComplex(const struct Complex
*num1, const struct Complex *num2) {
struct Complex result;
result.real = num1->real + num2->real;
result.imaginary = num1->imaginary + num2-
>imaginary;
return result;
}
struct Complex multiplyComplex(const struct
Complex *num1, const struct Complex *num2) {
struct Complex result;
result.real = num1->real * num2->real - num1-
>imaginary * num2->imaginary;
result.imaginary = num1->real * num2-
>imaginary + num1->imaginary * num2->real;
return result;
}
int main() {
struct Complex complex1, complex2, sum,
product;
printf("Enter the first complex number:\n");
readComplex(&complex1);
printf("Enter the second complex number:\n");
readComplex(&complex2);
printf("\nEntered complex numbers:\n");
printf("Complex Number 1: ");
writeComplex(&complex1);
printf("Complex Number 2: ");
writeComplex(&complex2);
sum = addComplex(&complex1, &complex2);
printf("\nSum of complex numbers: ");
writeComplex(&sum);
product = multiplyComplex(&complex1,
&complex2);
printf("Product of complex numbers: ");
writeComplex(&product);
return 0;
}
Output:
Program: Write a C program to display the contents of a file
#include <stdio.h>

int main() {
FILE *file;
char filename[100];
char ch;
printf("Enter the filename: ");
scanf("%s", filename);
file = fopen("pokemone.txt", "r");
if (file == NULL) {
printf("Unable to open the file.\n");
return 1;
}
printf("\nContents of the file '%s':\n", filename);
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
return 0;
}
Ouput:
Program: Write a C program to copy the contents of one file to another
#include <stdio.h>
int main() {
FILE *sourceFile, *destinationFile;
char sourceFilename[100],
destinationFilename[100];
char ch;
printf("Enter the source filename: ");
scanf("%s", sourceFilename);
sourceFile = fopen("pokemone.txt", "r");
if (sourceFile == NULL) {
printf("Unable to open the source file.\n");
return 1;
}
printf("Enter the destination filename: ");
scanf("%s", destinationFilename);
destinationFile = fopen("doraemon.txt", "w");
if (destinationFile == NULL) {
printf("Unable to open the destination file.\n");
fclose(sourceFile);
return 1;
}
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destinationFile);
}
fclose(sourceFile);
fclose(destinationFile);
printf("File copy successful!\n");
return 0;
}
Output:
Program: Write a C program to merge two files into a third file
#include <stdio.h>
int main() {
FILE *file1, *file2, *mergedFile;
char filename1[1000], filename2[1000],
mergedFilename[1000];
char ch;
printf("Enter the first filename: ");
scanf("%s", filename1);
file1 = fopen("pokemone.txt", "r");
if (file1 == NULL) {
printf("Unable to open the first file.\n");
return 1;}
printf("Enter the second filename: ");
scanf("%s", filename2);
file2 = fopen("doraemon.txt", "r");
if (file2 == NULL) {
printf("Unable to open the second file.\n");
fclose(file1);
return 1;
}
printf("Enter the merged filename: ");
scanf("%s", mergedFilename);
mergedFile = fopen("pyramid", "w");
if (mergedFile == NULL) {
printf("Unable to open the merged file.\n");
fclose(file1);
fclose(file2);
return 1;}
while ((ch = fgetc(file1)) != EOF) {
fputc(ch, mergedFile); }
while ((ch = fgetc(file2)) != EOF) {
fputc(ch, mergedFile); }
fclose(file1);
fclose(file2);
fclose(mergedFile);
printf("Files merged successfully!\n");
return 0;
}
Ouput:

You might also like