NadamAIMLB2Cfile
NadamAIMLB2Cfile
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;
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++;
}
}
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>
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);
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;
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: