c programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 19

21. Write a program to evaluate the series 1^2+2^2+3^2+……+n^2 .

#include<stdio.h>
int main()
{
int n,i;
int sum=0;
printf("Enter the n i.e. max values of series: ");
scanf("%d",&n);
sum = (n * (n + 1) * (2 * n + 1 )) / 6;
printf("Sum of the series : ");
for(i =1;i<=n;i++){
if (i != n)
printf("%d^2 + ",i);
else
printf("%d^2 = %d ",i,sum);
}
return 0;
}

22. Write a C program to find 1+1/2! +1/3! +1/4! +. ..... +1/n!


#include <stdio.h>
#include<conio.h>
void main()
{
double a,sum=0,i;
clrscr();
printf("Enter a number \n");
scanf("%lf",&a);
for(i=1;i<=a;i++)
{
sum=sum+(1/i);
if(i==1)
{
printf("\n1+");
}
else if(i==a)
{
printf("(1/%lf)+",i);
}
else
{
printf("(1/%lf)+",i);
}
}
printf("sum of given series %.2lf",sum);
getch();
}

23. Write a program to print following patterns :


(i)*
**
***
****
*****
#include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
for(a=1;a<=5;a++)
{
for(b=1;b<=a;b++)
{
printf("*");
}
printf("\n");
}
getch();
}
(ii) *****
****
***
**
*
#include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
for(a=5;a>=1;a--)
{
for(b=1;b<=a;b++)
{
printf("*");
}
printf("\n");
}
getch();
}

24. Write a program to print following patterns :


i) 1 ii )12345 iii) 55555 iv) 1
12 1234 4444 22
123 123 333 333
1234 12 22 4444
12345 1 1 55555

i) #include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
for(a=1;a<=5;a++)
{
for(b=1;b<=a;b++)
{
printf("%d",a);
}
printf("\n");
}
getch();
}

ii) #include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
for(a=5;a>=1;a--)
{
for(b=1;b<=a;b++)
{
printf("%d",b);
}
printf("\n");
}
getch();
}

iii) #include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
for(a=5;a>=1;a--)
{
for(b=1;b<=a;b++)
{
printf("%d",a);
}
printf("\n");
}
getch();
}

iv) #include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
for(a=1;a<=5;a++)
{
for(b=1;b<=a;b++)
{
printf("%d",a);
}
printf("\n");
}
getch();
}

25. Write a program to print following patterns:


i) AAAAA ii) ABCDE
BBBB ABCD
CCC ABC
DD AB
E A

i) #include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
for(a=’A’;a<=’E’;a++)
{
for(b=’E’;b>=a;b--)
{
printf("%c",a);
}
printf("\n");
}
getch();
}

ii) #include<conio.h>
#include<stdio.h>
void main()
{
int a,b;
clrscr();
for(a=’E’;a>=’A’;a--)
{
for(b=’A’;b<=a;b++)
{
printf("%d",b);
}
printf("\n");
}
getch();
}

26. Write a C program to read and store the roll no and marks of 20 students
using array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],b[20],i;
clrscr();
for(i=0;i<=19;i++)
{
printf("Enter student roll no %d \n",i+1);
scanf("%d",&a[i]);
printf("Enter student subject mark %d \n",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<=19;i++)
{
printf("student roll no=%d \n",a[i]);
printf("student marks=%d \n",b[i]);
}
getch();
}

27. Write a program to find maximum element from 1-Dimensional array.


#include<conio.h>
#include<stdio.h>
void main()
{
int i,a[5],b;
clrscr();
printf("enter 5 values : \n");
for(i=0;i<5;i++)
{
scanf("\n%d",&a[i]);
}
printf("\n value of array \n");
b=a[0];
for(i=0;i<5;i++)
{
printf("%d \n",a[i]);
if(b<a[i])
{
b=a[i];
}
}
printf("mmaximum number is : %d",b);
getch();
}

28. Write a program to sort given array in ascending order.


#include<conio.h>
#include<stdio.h>
void main()
{
int i,a[5],b,n,j;
clrscr();
printf("enter number of elements : \n");
scanf("%d",&n);
printf("enter the elements \n");
for(i=0;i<n;++i)
{
scanf("\n%d",&a[i]);
}
for(i=0;i<n;++i)
{
for(j=i+1;j<n;++j)
{
if(a[i]>a[j])
{
b=a[i];
a[i]=a[j];
a[j]=b;
}
}
}
printf("The number in ascending order:");
for(i=0;i<n;++i)
{
printf("%d\n",a[i]);
}
getch();
}

29. Write a program to replace a character in given string.


#include<stdio.h>
#include<conio.h>
void main()
{
char a[100],n1,n2;
int i;
printf("Enter a string \n");
gets(a);
printf("Enter character to serch :\n");
scanf("%c",&n1);
getchar();
printf("Enter a char to replace place of old:\n");
scanf("%c",&n2);
for(i=0;a[i]!='\0';i++)
{
if(a[i]==n1)
{
a[i]=n2;
break;
}
}
printf("string after replace of %c with %c =%s",n1,n2,a);
getch();
}

30. Write a program to reverse string.


#include<stdio.h>
#include<conio.h>
void main()
{
char a[100];
printf("Enter a string to reverse\n");
gets(a);
strrev(a);
printf("Reverse of the string : %s\n",a);
getch();
}

31.Write a program to convert string into upper case.


#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str);

for (int i = 0; str[i] != '\0'; i++) {


str[i] = toupper(str[i]);
}

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


return 0;
}

32. Write a program that defines a function to add first n numbers.


#include <stdio.h>
int sumFirstN(int n) {
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Sum of first %d numbers is: %d\n", n, sumFirstN(n));
return 0;
}
33.Write a function in the program to return 1 if number is prime otherwise return
0.
#include <stdio.h>
int isPrime(int num) {
if (num <= 1) return 0;
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0) return 0;
}
return 1;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}

34. Write a program to find factorial of a number using recursion.


#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is: %d\n", num, factorial(num));
return 0;
}

35. Write a function that will scan a character string passed as an argument and
convert all lowercase character into their uppercase equivalents.
#include <stdio.h>
#include <ctype.h>
void toUpperCase(char str[]) {
for (int i = 0; str[i] != '\0'; i++) {
if (islower(str[i])) {
str[i] = toupper(str[i]);
}
}
}
int main() {
char str[100];
printf("Enter a string: ");
gets(str);
toUpperCase(str);
printf("Converted string: %s\n", str);
return 0;
}

36. Write a program to read structure elements from keyboard.


#include <stdio.h>
struct person {
char name[50];
int age;
};
int main() {
struct person p;
printf("Enter name: ");
scanf("%s", p.name);
printf("Enter age: ");
scanf("%d", &p.age);
printf("Name: %s, Age: %d\n", p.name, p.age);
return 0;}

37. Define a structure type struct personal that would contain person name,
joining date and salary using this structure to read this information of 5 people
and print the same on screen.
#include <stdio.h>
struct personal {
char name[50];
char joining_date[15];
float salary;
};
int main() {
struct personal people[5];
for (int i = 0; i < 5; i++) {
printf("Enter name, joining date, and salary for person %d:\n", i + 1);
scanf("%s %s %f", people[i].name, people[i].joining_date, &people[i].salary);
}
printf("\nDetails of 5 people:\n");
for (int i = 0; i < 5; i++) {
printf("Name: %s, Joining Date: %s, Salary: %.2f\n", people[i].name,
people[i].joining_date, people[i].salary);
}
return 0;
}

38. Define structure data type called time_struct containing three member’s
integer hour, integer minute and integer second. Develop a program that would
assign values to the individual number and display the time in the following
format: 16: 40:51
#include <stdio.h>
struct time_struct {
int hour;
int minute;
int second;
};
int main() {
struct time_struct t = {16, 40, 51};
printf("Time: %02d:%02d:%02d\n", t.hour, t.minute, t.second);
return 0;
}
39. Design a structure student_record to contain name, branch and total marks
obtained. Develop a program to read data for 10 students in a class and print
them.
#include <stdio.h>
struct student_record {
char name[50];
char branch[50];
int total_marks;
};
int main() {
struct student_record students[10];
for (int i = 0; i < 10; i++) {
printf("Enter name, branch, and total marks for student %d:\n", i + 1);
scanf("%s %s %d", students[i].name, students[i].branch,
&students[i].total_marks);
}
printf("\nDetails of 10 students:\n");
for (int i = 0; i < 10; i++) {
printf("Name: %s, Branch: %s, Total Marks: %d\n", students[i].name,
students[i].branch, students[i].total_marks);
}
return 0;
}

40. Write a program to print address of variable using pointer.


#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
printf("Address of x: %p\n", ptr);
return 0;
}

41. Write a C program to swap the two values using pointers.


#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}

42. Write a C program to print the address of character and the character of string
usingpointer.
#include <stdio.h>
int main() {
char str[] = "Hello";
char *ptr = str;
for (int i = 0; str[i] != '\0'; i++) {
printf("Character: %c, Address: %p\n", *(ptr + i), (ptr + i));
}
return 0;
}

43.Write a program to access elements using pointer.


#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr;

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


printf("Element %d: %d\n", i, *(ptr + i));
}
return 0;
}

44. Write a program to read, print and addition of two Matrices using pointer and
user define functions.
#include <stdio.h>
void readMatrix(int matrix[3][3], int rows, int 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));
}
}
}
void printMatrix(int matrix[3][3], int rows, int cols) {
printf("Matrix:\n");
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
printf("%d ", *(*(matrix + i) + j));
}
printf("\n");
}
}
void addMatrices(int a[3][3], int b[3][3], int result[3][3], int rows, int cols) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
*(*(result + i) + j) = *(*(a + i) + j) + *(*(b + i) + j);
}
}
}
int main() {
int a[3][3], b[3][3], result[3][3];
int rows = 3, cols = 3;
printf("Matrix A:\n");
readMatrix(a, rows, cols);
printf("Matrix B:\n");
readMatrix(b, rows, cols);
addMatrices(a, b, result, rows, cols);
printf("Sum of matrices:\n");
printMatrix(result, rows, cols);
return 0;
}

45. Write a program for sorting using pointer.


#include <stdio.h>
void sortArray(int *arr, int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (*(arr + i) > *(arr + j)) {
int temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
sortArray(arr, n);
printf("Sorted array:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}

46. Write a program to read n integer number from keyboard and store them into
a file All.txt. Read All.txt file, separate even and odd numbers and store them into
files Even.txt and Odd.txt respectively and display contents of all the three files.
#include <stdio.h>
int main() {
FILE *allFile, *evenFile, *oddFile;
int n, num;
allFile = fopen("All.txt", "w");
if (!allFile) {
printf("Error opening file!\n");
return 1;
}
printf("Enter how many numbers: ");
scanf("%d", &n);
printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &num);
fprintf(allFile, "%d ", num);
}
fclose(allFile);
allFile = fopen("All.txt", "r");
evenFile = fopen("Even.txt", "w");
oddFile = fopen("Odd.txt", "w");
while (fscanf(allFile, "%d", &num) != EOF) {
if (num % 2 == 0) {
fprintf(evenFile, "%d ", num);
} else {
fprintf(oddFile, "%d ", num);
}
}
fclose(allFile);
fclose(evenFile);
fclose(oddFile);
printf("Numbers have been separated into Even.txt and Odd.txt.\n");
return 0;
}

47.Write a program to accept the contents from the user and store it in the file
one line at a time and print the contents of the file.
#include <stdio.h>
int main() {
FILE *file;
char line[100];
file = fopen("output.txt", "w");
if (!file) {
printf("Error opening file!\n");
return 1;
}
printf("Enter lines of text (type 'exit' to stop):\n");
while (1) {
gets(line);
if (strcmp(line, "exit") == 0) break;
fprintf(file, "%s\n", line);
}
fclose(file);
file = fopen("output.txt", "r");
printf("\nContents of the file:\n");
while (fgets(line, sizeof(line), file)) {
printf("%s", line);
}
fclose(file);
return 0;
}

48. Read a text file which name is given in command line and print the total
number of character in each line and total number of lines in a file.
#include <stdio.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
printf("Usage: %s <filename>\n", argv[0]);
return 1;
}
FILE *file = fopen(argv[1], "r");
if (!file) {
printf("Error opening file!\n");
return 1;
}
char ch;
int lines = 0, chars = 0;
while ((ch = fgetc(file)) != EOF) {
chars++;
if (ch == '\n') lines++;
}
printf("Total characters: %d\n", chars);
printf("Total lines: %d\n", lines);
fclose(file);
return 0;
}

49.Write a program to merge two files into the third file.


#include <stdio.h>
int main() {
FILE *file1, *file2, *mergedFile;
char ch;
file1 = fopen("file1.txt", "r");
file2 = fopen("file2.txt", "r");
mergedFile = fopen("merged.txt", "w");
if (!file1 || !file2 || !mergedFile) {
printf("Error opening files!\n");
return 1;
}
while ((ch = fgetc(file1)) != EOF) {
fputc(ch, mergedFile);
}
while ((ch = fgetc(file2)) != EOF) {
fputc(ch, mergedFile);
}
printf("Files merged successfully!\n");
fclose(file1);
fclose(file2);
fclose(mergedFile);
return 0;
}

50. Program for deleting the spaces from the contents of file.
#include <stdio.h>
int main() {
FILE *file, *temp;
char ch;
file = fopen("input.txt", "r");
temp = fopen("temp.txt", "w");
if (!file || !temp) {
printf("Error opening file!\n");
return 1;
}
while ((ch = fgetc(file)) != EOF) {
if (ch != ' ') {
fputc(ch, temp);
}
}
fclose(file);
fclose(temp);
remove("input.txt");
rename("temp.txt", "input.txt");
printf("Spaces removed successfully!\n");
return 0;
}

You might also like