c programs
c programs
c programs
#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;
}
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();
}
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();
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}