1(A).
PROGRAM:
#include<stdio.h>
void main()
{
char name[20]= "SAI RAM";
char address[80]= "west tambharam,chennai";
int date=20;
int month=10;
int year=1990;
int mobile=987456321;
int age=25;
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\n ADDRESS:%s", address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n AGE:%d", age);
printf("\n=====================");
}
OUTPUT:
NAME: SAI RAM
ADDRESS:west tambaram,chennai
DOB:20:10:1990
MOBILE NUMBER:987456321
AGE:25
1(B).PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char name[20];
char address[80];
int date;
int month;
int year;
long int mobile;
char gender[20];
int age;
printf("\n ENTER YOUR NAME:=");
gets(name);
printf("\nENTER YOUR ADDRESS=");
gets(address);
printf("\nENTER YOUR date/month/year=");
scanf("%d/%d/%d",&date,&month,&year);
printf("\n ENTER YOUR AGE=");
scanf("%d",&age);
printf("\n ENTER YOUR GENDER(MALE/FEMALE)=");
scanf("%s",gender);
printf("\nENTER YOUR MOBILE NUMBER=");
scanf("%ld" ,&mobile);
printf("\n=====================");
printf("\n NAME: %s",name);
printf("\n ADDRESS:%s", address);
printf("\n DOB:%d:%d:%d", date , month, year);
printf("\n AGE:%d", age);
printf("\n GENDER:%s", gender);
printf("\n MOBILE NUMBER:%d", mobile);
printf("\n=====================");
return 0;
}
OUTPUT:
ENTER YOUR NAME:=karthikeyan
ENTER YOUR ADDRESS=west tambharam,chennai.
ENTER YOUR date/month/year=03/12/1992
ENTER YOUR AGE=28
ENTER YOUR GENDER(MALE/FEMALE)=MALE
ENTER YOUR MOBILE NUMBER=987654321
=====================
NAME: karthikeyan
ADDRESS:west tambharam,chennai.
DOB:3:12:1992
AGE:28
GENDER:MALE
MOBILE NUMBER:987654321
========================
2(A).PROGRAM:
#include <stdio.h>
void main()
{
int A,B,C;
printf("Enter 3 integer number \n");
scanf("%d",&A);
scanf("%d",&B);
scanf("%d",&C);
if(A>B){
if(A>C){
printf(" %d is the Greatest Number \n",A);
}
else{
printf("%d is the greatest Number \n",C);
}
}
else{
if(B>C){
printf("%d is the greatest Number \n",B );
}
else{
printf("%d is the greatest Number \n", C);
}
}
}
OUTPUT:
Enter three numbers: -4.5
3.9
5.6
5.60 is the largest number.
2(B).PROGRAM:
#include<stdio.h>
#include<conio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
//condition to check character is alphabet or not
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
{
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a VOWEL.\n",ch);
break;
default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{
printf("%c is not an alphabet.\n",ch);
}
return 0;
}
OUTPUT:
Enter a character
E is a vowel
Enter a character
X is a consonant
Enter a character
+ is not an alphabet
3.PROGRAM:
#include <stdio.h>
#include <conio.h>
void main()
{
int year;
printf("Enter a year :\n");
scanf("%d", &year);
if ((year % 400) == 0)
printf("%d is a leap year \n",year);
else
if ((year % 100) != 0 && (year % 4) == 0)
printf("%d is a leap year \n",year);
else
printf("%d is not a leap year \n",year);
}
OUTPUT:
Enter a year:
2000
2000 is a leap year
Enter a year:
1900
1900 is not a leap year
4.PROGRAM:
#include<stdio.h>
// functions declaration
int add(int n1, int n2);
int subtract(int n1, int n2);
int multiply(int n1, int n2);
int divide(int n1, int n2);
int square(int n1);
// main function
int main()
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("%d + %d = %d\n", num1, num2, add(num1, num2));
printf("%d - %d = %d\n", num1, num2, subtract(num1, num2));
printf("%d * %d = %d\n", num1, num2, multiply(num1, num2));
printf("%d / %d = %d\n", num1, num2, divide(num1, num2));
printf(“%d^%d=%d\n”,num1,num1,square( num1));
return 0;
// function to add two integer numbers
int add(int n1, int n2)
int result;
result = n1 + n2;
return result;
// function to subtract two integer numbers
int subtract(int n1, int n2)
int result;
result = n1 - n2;
return result;
// function to multiply two integer numbers
int multiply(int n1, int n2)
int result;
result = n1 * n2;
return result;
// function to divide two integer numbers
int divide(int n1, int n2)
int result;
result = n1 / n2;
return result;
// function to find square of a number
int square(int n1)
int result;
result = n1*n1;
return result;
}
OUTPUT:
Enter two numbers: 20 5
20 + 5 = 25
20 – 5 = 15
20 * 5 = 100
20 / 5 = 4
20^20 = 400
5.PROGRAM:
#include<stdio.h>
int main()
{
int num,copy_of_num,sum=0,rem;
printf("\nEnter a number:");
scanf("%d",&num);
while (num != 0)
{
rem = num % 10;
sum = sum + (rem*rem*rem);
num = num / 10;
}
if(copy_of_num == sum)
printf("\n%d is an Armstrong Number",copy_of_num);
else
printf("\n%d is not an Armstrong Number",copy_of_num);
return(0);
}
OUTPUT:
Enter a number: 370
370 is an Armstrong Number
6.PROGRAM:
#include <stdio.h>
int main()
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the number is perfectly divisible by 2 if(number % 2 == 0)
printf("%d is even.", number);
else
printf("%d is odd.", number);
return 0;
OUTPUT:
Enter an integer: -7
-7 is odd.
Enter an integer : 8
8 is even
7.PROGRAM:
int main()
int n, i; longfactorial = 1;
printf("Enter an integer: ");
scanf("%d",&n);
// show error if the user enters a negative integer
if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");
else
for(i=1; i<=n; ++i)
{
factorial *= i;
// factorial = factorial*i;
}
printf("Factorial of %d = %lu", n, factorial);
}
return 0;
}
OUTPUT:
Enter an integer: 10
Factoriaof 10 = 3628800
8.PROGRAM:
#include<stdio.h>
void main()
inti,n,sum=0,nu[100];
float avg;
clrscr();
printf("\nEnter the numbers\n");
for(i=0;i<3;i++)
scanf("%d",&nu[i]);
sum = sum + nu[i];
}
avg = (float)sum/n;
printf("\nAverage is : %.2f\n",n,avg);
getch();
}
OUTPUT:
Enter the numbers
32
45
54
22
Average is 38.25
9.PROGRAM:
#include<stdio.h>
int main(){
/* 2D array declaration*/
int disp[2][3];
/*Counter variables for the loop*/
int i, j;
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("Enter value for disp[%d][%d]:", i, j);
scanf("%d", &disp[i][j]);
//Displaying array elements
printf("Two Dimensional array elements:\n");
for(i=0; i<2; i++) {
for(j=0;j<3;j++) {
printf("%d ", disp[i][j]);
if(j==2){
printf("\n");
}
}
return 0;
}
OUTPUT:
Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
123
456
10.PROGRAM:
#include<stdio.h>
void main()
void swap(int,int);
inta,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
void swap(inta,int b)
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
OUTPUT:
Enter the value of a & b : 34 78
after swapping the value for a & b is 78,34
11.PROGRAM:
#include <stdio.h>
/* Function declarations */
int isPrime(int num);
void printPrimes(int lowerLimit, int upperLimit);
int main()
int lowerLimit, upperLimit;
printf("Enter the lower and upper limit to list primes: ");
scanf("%d%d", &lowerLimit, &upperLimit);
/* Call function to print all primes between the given range*/
printPrimes(lowerLimit, upperLimit);
return 0;
/* Print all prime numbers between lower limit and upper limit*/
void printPrimes(int lowerLimit, int upperLimit)
printf("All prime number between %d to %d are: ", lowerLimit, upperLimit);
while(lowerLimit <= upperLimit)
/* Print if current number is prime*/
if(isPrime(lowerLimit))
printf("%d, ", lowerLimit);
}
lowerLimit++;
/*Check whether a number is prime or not*/
/*Returns 1 if the number is prime otherwise 0*/
int isPrime(int num)
int i;
for(i=2; i<=num/2; i++)
/* If the number is divisible by any number*/
/*other than 1 and self then it is not prime*/
if(num % i == 0)
return 0;
return 1;
OUTPUT:
Enter the lower and upper limit to list primes:
1 100
All prime number between 1 100 are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
12.PROGRAM:
#include <stdio.h>
void reverseSentence();
int main() {
printf("Enter a sentence: ");
reverseSentence();
return 0;
}
void reverseSentence() {
char c;
scanf("%c", &c);
if (c != '\n') {
reverseSentence();
printf("%c", c);
}
}
OUTPUT:
Enter a sentence: margorp emosewa
awesome PROGRAM:
13.PROGRAM:
#include <stdio.h>
#include <conio.h>
max(int [],int);
void main()
int a[]={10,5,45,12,19};
int n=5,m;
clrscr();
m=max(a,n);
printf("\nmaximum number is %d",m);
getch();
max(int x[],int k)
int t,i;
t=x[0];
for(i=1;i<k;i++)
if(x[i]>t)
t=x[i];
return(t);
OUTPUT:
Maximum number is 45
14.PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char destination[] = "Hello ";
char source[] = "World!";
printf("Concatenated String: %s\n", strcat(destination,source));
return 0;
}
OUTPUT:
Concatenated String: Hello World!
15.PROGRAM:
i) Using Library Function
#include <stdio.h>
#include <string.h>
int main()
{
char a[100];
int length;
printf("\n Enter a string to calculate its length=");
gets(a);
length = strlen(a);
printf("\nLength of the string = %d\n", length);
return 0;
}
ii) Without Using Library Function
#include <stdio.h>
#include<string.h>
int main()
{
char i=0;a[100];
int length;
printf("\nEnter a string to calculate its length=");
scanf(“%s”,str);
while(string1[i] !='\0') {
i++;
}
length=i;
printf (“\n Length of the string = %d\n",length);
return 0;
}
OUTPUT:
Enter a string to calculate its length=Introduction
Length of the string: 12
16.PROGRAM:
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);
for (int i = 0; str[i] != '\0'; ++i) {
if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
OUTPUT:
Enter a string: This website is awesome.
Enter a character to find its frequency: e
Frequency of e = 4
17.PROGRAM:
#include<stdio.h>
struct student
{
int roll_no, mark1, mark2, mark3, total;
float average;
char name[10],grade;
};
void struct_funct_student(struct student stu);
int main()
{
struct student stud;
printf("\nRoll No.=");
scanf("%d",&stud.roll_no);
printf("Name=");
scanf("%s",stud.name);
printf("Mark1=");
scanf("%d",&stud.mark1);
printf("Mark2=");
scanf("%d",&stud.mark2);
printf("Mark3=");
scanf("%d",&stud.mark3);
struct_funct_student(stud);
return 0;
}
void struct_funct_student( struct student stu)
{
stu.total = stu.mark1 + stu.mark2 + stu.mark3;
stu.average = stu.total / 3;
if(stu.average >= 90)
stu.grade='S';
else if(stu.average >= 80)
stu.grade='A';
else if(stu.average >= 70)
stu.grade='B';
else if(stu.average >= 60)
stu.grade='C';
else if(stu.average >= 50)
stu.grade='D';
else
stu.grade='F';
printf("\n ROLL NO. \t NAME \t TOTAL \t AVG \t
GRADE \n");
printf("%d \t %s \t %d \t %f \t %c",
stu.roll_no,stu.name,stu.total,stu.average,stu.grade);
}
OUTPUT:
Roll No.= 1
Name= a
Mark1= 95
Mark2= 94
Mark3= 96
ROLL NO. NAME TOTAL AVG GRADE
1 a 285 95.000000 S
18.PROGRAM:
#include<stdio.h>
struct student
int sub1;
int sub2;
int sub3;
int sub4;
int sub5;
};
void main()
struct student s[10];
int i,total=0;
clrscr();
for(i=0;i<=9;i++)
printf("\nEnter Marks in Five Subjects = ");
scanf("%d%d%d",& s[i].sub1,&s[i].sub2,&s[i].sub3,&s[i].sub4,&s[i].sub5);
total=s[i].sub1+s[i].sub2+s[i].sub3+s[i].sub4+s[i].sub5;
printf("\nTotal marks of s[%d] Student= %d",i,total);
getch();
OUTPUT:
Enter Marks in Five Subjects
80 70 90 80 98
Total Marks of 1 student = 83.6
19.PROGRAM
#include "stdio.h"
#include "string.h"
#include<stdlib.h>
#include<fcntl.h>
struct dir
{
char name[20];
char number[10];
};
void insert(FILE *);
void update(FILE *);
void del(FILE *);
void display(FILE *);
void search(FILE *);
int record = 0;
int main(void) {
int choice = 0;
FILE *fp = fopen( "telephone.dat", "rb+" );
if (fp == NULL ) perror ("Error opening file");
while (choice != 6)
{
printf("\n1 insert\t 2 update\n");
printf("3 delete\t 4 display\n");
printf("5 search\t 6 Exit\n Enter choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: insert(fp); break;
case 2: update(fp); break;
case 3: del(fp); break;
case 4: display(fp); break;
case 5: search(fp); break;
default: ;
}
}
fclose(fp);
return 0;
}
void insert(FILE *fp)
{
struct dir contact, blank;
fseek( fp, -sizeof(struct dir), SEEK_END );
fread(&blank, sizeof(struct dir), 1, fp);
printf("Enter individual/company name: ");
scanf("%s", contact.name);
printf("Enter telephone number: ");
scanf("%s", contact.number);
fwrite(&contact, sizeof(struct dir), 1, fp);
}
void update(FILE *fp)
{
char name[20], number[10];
int result;
struct dir contact, blank;
printf("Enter name:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
{
result = fread(&contact, sizeof(struct dir), 1, fp);
if(result != 0 && strcmp(name, contact.name) == 0)
{
printf("Enter number:");
scanf("%s", number);
strcpy(contact.number, number);
fseek(fp, -sizeof(struct dir), SEEK_CUR);
fwrite(&contact, sizeof(struct dir), 1, fp);
printf("Updated successfully\n");
return;
}
}
printf("Record not found\n");
}
void del(FILE *fp)
{
char name[20], number[10];
int result, record=0;
struct dir contact, blank = {"", ""};
printf("Enter name:");
scanf("%s", name);
rewind(fp);
while(!feof(fp))
{
result = fread(&contact, sizeof(struct dir), 1, fp);
if(result != 0 && strcmp(name, contact.name) == 0)
{
fseek(fp, record*sizeof(struct dir), SEEK_SET);
fwrite(&blank, sizeof(struct dir), 1, fp);
printf("%d Deleted successfully\n", record-1);
return;
}
record++;
}
printf("not found in %d records\n", record);
void display(FILE *fp)
{
struct dir contact;
int result;
rewind(fp);
printf("\n\n Telephone directory\n");
printf("%20s %10s\n", "Name", "Number");
printf("*******************************\n");
while(!feof(fp))
{
result = fread(&contact, sizeof(struct dir), 1, fp);
if(result != 0 && strlen(contact.name) > 0)
printf("%20s %10s\n",contact.name, contact.number);
}
printf("*******************************\n");
void search(FILE *fp)
{
struct dir contact;
int result; char name[20];
rewind(fp);
printf("\nEnter name:");
scanf("%s", name);
while(!feof(fp))
{
result = fread(&contact, sizeof(struct dir), 1, fp);
if(result != 0 && strcmp(contact.name, name) == 0)
{
printf("\n%20s %10s\n",contact.name, contact.number);
return;
}
}
printf("Record not found\n");
OUTPUT:
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4
Telephone directory
Name Number
*******************************
bb 11111
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 5
Enter name: bb
bb 11111
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 1
Enter individual/company name: aa
Enter telephone number: 222222
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 2
Enter name: aa
Enter number: 333333
Updated successfully
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice:
Telephone directory
Name Number
*******************************
bb 11111
aa 333333
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 3
Enter name: aa
1 Deleted successfully
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 4
Telephone directory
Name Number
*******************************
bb 11111
*******************************
1 insert 2 update
3 delete 4 display
5 search 6 Exit
Enter choice: 6
20.PROGRAM:
#include <stdio.h>
void insert();
void count();
int main(void)
{
int choice = 0;
while (choice != 3)
{
printf("\n1 insert records\n");
printf("2 Count min balance holders\n");
printf("3 Exit\n");
printf("Enter choice:");
scanf("%d", &choice);
switch(choice)
{
case 1: insert(); break;
case 2: count(); break;
}
}
}
void insert()
{
unsigned int account,i;
char name[30];
double balance;
FILE* cfPtr;
if ((cfPtr = fopen("clients.dat", "w")) == NULL) {
puts("File could not be opened");
}
else {
int records,i=0;
printf("Enter the No. of records ");
scanf("%d", &records);
while (i<records)
{
printf("Enter the account, name, and balance.");
scanf("%d%29s%lf", &account, name, &balance);
fprintf(cfPtr, "%d %s %.2f\n", account, name, balance);
i++;
}
fclose(cfPtr);
}
}
void count()
{
unsigned int account;
char name[30];
double balance;
float minBal = 5000.00;
int count = 0;
FILE *cfPtr;
if ((cfPtr = fopen("clients.dat", "r")) == NULL)
printf("File could not be opened");
else
{
printf("%-10s%-13s%s\n", "Account", "Name", "Balance");
fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);
while (!feof(cfPtr))
{
if (balance < minBal)
{
printf("%-10d%-13s%7.2f\n", account, name, balance);
count++;
}
fscanf(cfPtr, "%d%29s%lf", &account, name, &balance);
}
fclose(cfPtr);
printf("The number of account holders whose balance is less than the minimum balance:
%d", count);
}
}
OUTPUT:
1 insert records
2 Count min balance holders
3 Exit
Enter choice:1
Enter the No. of records 2
Enter the account, name, and balance.1001 A 10000
Enter the account, name, and balance.1002 B 300
1 insert records
2 Count min balance holders
3 Exit
Enter choice:2
Account Name Balance
1002 B 300.00
The number of account holders whose balance is less than the minimum balance: 1
1 insert records
2 Count min balance holders
3 Exit
Enter choice: