0% found this document useful (0 votes)
13 views33 pages

C Record 2024

Uploaded by

suhanr951
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views33 pages

C Record 2024

Uploaded by

suhanr951
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 33

PART – A

1. Program to find the roots of quadratic equation using else if ladder

#include<stdio.h>
#include<math.h>
void main ()
{
float a,b,c,d,root1,root2,real,imaginary;
printf("Enter the coefficients a,b,c:");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;

if(d>0)
{
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("Roots: %.2f , %.2f \n",root1,root2);
}
else if(d==0)
{
root1= -b/(2*a);
printf("Roots: %.2f \n",root1);
}
else{
real = -b/(2*a);
imaginary = (sqrt(-d))/(2*a);
printf("Roots: %.2f + %.2f , %.2f - %.2f \n",real,imaginary,real,imaginary);
}
getch();
}

Maps College, Mangalore. Page 1


Output:

Maps College, Mangalore. Page 2


2. Program to read two integer values & an operator as character and
perform basic arithmetic operations on them using switch case (+, -, *, /
operations).

#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2;
char operator;
int result;

printf("Enter two integers:");


scanf("%d%d",&num1,&num2);

printf("Enter any operator(+,-,*,/):");


scanf(" %c",&operator);

//using switch case


switch(operator){
case '+':
result=num1+num2;
printf("Result = %d + %d = %d \n",num1,num2,result);
break;

case '-':
result=num1-num2;
printf("Result = %d - %d = %d \n",num1,num2,result);
break;

case '*':
result=num1*num2;
printf("Result = %d * %d = %d \n",num1,num2,result);

Maps College, Mangalore. Page 3


break;

case '/':
if(num2!=0){
result=num1/num2;
printf("Result = %d / %d = %d\n",num1,num2,result);
}
else{
printf("Error:Division by zero is not allowed. \n");
}
break;
default :
printf("Error:Invalid operator. \n");
}
getch();
}

Output:

Maps College, Mangalore. Page 4


3. Program to reverse a number and find the sum of individual digits. Also
check for palindrome.

#include<stdio.h>
#include<conio.h>

void main()
{
int num,originalNum,remainder,reversedNum=0,sumOfDigits=0;

printf("Enter an integer:");
scanf("%d",&num);

//store the original number for palindrome check


originalNum=num;

//reverse the number and calculate sum of digits


while(num!=0){
remainder=num%10;
reversedNum=reversedNum*10+remainder;
sumOfDigits+=remainder;
num/=10;
}

//output the reversed number and sum of digits


printf("Reveresed Number: %d \n",reversedNum);
printf("Sum of Digits: %d \n",sumOfDigits);

//check if the original number is a palindrome


if(originalNum==reversedNum){
printf("%d is a palindrome.\n",originalNum);
}

Maps College, Mangalore. Page 5


else{
printf("%d is not a palindrome.\n",originalNum);
}
}

Output:

Maps College, Mangalore. Page 6


4. Program to calculate and display the first ‘n’ Fibonacci numbers.

#include<stdio.h>
int main()
{
int n,a=0,b=1,next,i;

printf("Enter the number of Fibonacci terms to be displayed:");


scanf("%d",&n);

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

Output:

5. Program to find given number is a prime or not.

Maps College, Mangalore. Page 7


#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,count=0;
printf("enter the number:");
scanf("%d",&num);

for(i=1;i<=num;i++)
{
if(num%i==0)
{
count++;
}
}
if(count==2)
{
printf("%d is a Prime Number. \n",num);
}
else{
printf("%d is not a Prime Number.\n",num);
}
}

Output:

6. Program to count occurrences of each character in a given string.

Maps College, Mangalore. Page 8


#include<stdio.h>
#include<string.h>
void main()
{
int i,count=0;
char str[100],ch;

printf("Enter the string:");


gets(str);

printf("Enter the character you want to search for:");


scanf(" %c",&ch);

for(i=0;i<=strlen(str);i++){
if(str[i]==ch){
count++;
}
}
printf("Total number of times %c has occured: %d\n",ch,count);
getch();
}

Output:

7. Program to read string with alphabets, digits and special characters and
convert upper case letters to lower case and vice a versa and retain the
digits and special characters as it is.

Maps College, Mangalore. Page 9


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

for(i=0;str[i]!='\0';i++){
if(str[i]>='A' && str[i]<='Z'){
str[i]=str[i]+32; //uppercase to lowercase
}
else if (str[i]>='a' && str[i]<='z'){
str[i]=str[i]-32; //lowercase to uppercase
}
}
printf("Converted String: %s \n",str);
getch();
}

Output:

8. Program to search for number of occurrences of number in a list of


numbers using one-dimensional array also display its positions.

Maps College, Mangalore. Page 10


#include<stdio.h>
#include<string.h>
void main()
{
int n,i,num,arr[20],occurances=0;

printf("Enter the size of an array:");


scanf("%d",&n);

printf("Enter the elements in an array:\n");


for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}

printf("enter the number to search for:");


scanf("%d",&num);

printf("Positions of %d are:",num);
for(i=0;i<n;i++)
{
if(arr[i]==num)
{
printf("%d",i);
occurances++;
}
}

if(occurances==0)
{
printf("none");

Maps College, Mangalore. Page 11


}
printf("\n Total occurances: %d \n",occurances);
}

Output:

PART B

Maps College, Mangalore. Page 12


1. Program to find the largest and smallest elements with their position in a
one-dimensional array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20];
int c,size,big,low,pos,loc;
pos=loc=0;
//clrscr();

printf("ENTER A NUMBER TO STORE IN AN ARRAY: \n");


scanf("%d",&size);

printf("ENTER %d VALUES:\n",size);
for(c=0;c<size;c++)
scanf("%d",&a[c]);

printf("\n ELEMETS OF ARRAY A: \n");


for(c=0;c<size;c++)
printf("%d\n",a[c]);

big=low=a[0];

for(c=1;c<size;c++)
{
if(a[c]>big)
{
big=a[c];
pos=c;
}

Maps College, Mangalore. Page 13


if(a[c]<low)
{
low=a[c];
loc=c;
}
}
printf("\n THE BIGEST VALUE: %d IN THE POSITION: %d",big,pos+1);
printf("\n THE LOWEST VALUE: %d IN THE POSITION: %d",low,loc+1);
getch();
}

Output:

2. Program to read ‘n’ integer values into a single dimension array and
arrange them in ascending order using bubble sort method.

#include<stdio.h>

Maps College, Mangalore. Page 14


#include<conio.h>
void main()
{
int a[100];
int i,n,j,temp;
//clrscr();

printf("ENTER TOTAL NUMBER OF ELEMENTS:");


scanf("%d",&n);

printf("ENTER THE ARRAY ELEMENTS ONE BY ONE:\n");


for(i=0;i<n;i++)
scanf("%d",&a[i]);

printf("GIVEN LIST: \n");


for(i=0;i<n;i++)
printf("%d\t",a[i]);

/*bubble sort*/
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}

Maps College, Mangalore. Page 15


printf("\n SORTED LIST: \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
getch();
}

Output:

3. Menu driven Program to perform addition and multiplication of two


Matrices.

#include<stdio.h>

Maps College, Mangalore. Page 16


#include<conio.h>
int main()
{
int C[10][10],A[10][10],B[10][10];
int i,j,ch,rows,cols,k;
//clrscr();
printf("ENTER NUMBER OF ROWS: ");
scanf("%d",&rows);
printf("ENTER NUMBER OF COLUMNS: ");
scanf("%d",&cols);
printf("ENTER ELEMENTS IN MATRIX A:\n");
for(i=0;i<rows;i++)
{
for (j=0;j<cols;j++)
{
printf("\t");
scanf("%d",&A[i][j]);
}
}
printf("MATRIX A:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%d",A[i][j]);
printf("\t");
}
printf("\n");
}
printf("\n Enter elements in the matrix B:\n");
for(i=0;i<rows;i++)

Maps College, Mangalore. Page 17


{
for(j=0;j<cols;j++)
{
printf("\t");
scanf("%d",&B[i][j]);
}
}
printf("Matrix B:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%d",B[i][j]);
printf("\t");
}
printf("\n");
}
printf("1.Addition of Matrix A&B\n");
printf("2.Multiplication of Matrix A&B\n");
printf("Enter your choice(1,2):\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("A+B=\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
C[i][j]=A[i][j]+B[i][j];
printf("%d",C[i][j]);
printf("\t");

Maps College, Mangalore. Page 18


}
printf("\n");
}
break;
case 2:
printf("A*B=\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
C[i][j]=0;
for(k=0;k<cols;k++)
{
C[i][j]=C[i][j]+A[i][k]*B[k][j];
}
printf("%d",C[i][j]);
printf("\t");
}
printf("\n");
}
break;
default:
printf("\n Wrong choice");
}
getch();
}

Output:

Maps College, Mangalore. Page 19


4. Program to find nCr and nPr using recursive function to calculate
factorial.

Maps College, Mangalore. Page 20


#include<stdio.h>
#include<conio.h>
int main()
{
int n,r,npr,ncr;
//clrscr();

printf("Enter value of n and r: ");


scanf("%d%d",&n,&r);

npr=factorial(n)/factorial(n-r);
ncr=npr/factorial(r);

printf("Npr value is:%d \n",npr);


printf("Ncr value is:%d \n",ncr);
}
int factorial(int x)
{
int f ;
if(x==1)
return 1;
else
f=x*factorial(x-1);
return(f);
}

Output:

Maps College, Mangalore. Page 21


5. Program to read a string and count number of letters, digits, vowels,
consonants, spaces and special characters present in it using user defined
function.

#include<stdio.h>
#include<conio.h>
int isVowel(char c);
int isConsonant(char c);
int isDigit(char c);
int isWhitespace(char c);
int isSpecial(char c);
void main()
{
char str[500];
int V=0,C=0,D=0,W=0,S=0,T=0,i;
//clrscr();
printf("Enter a string:\n");
gets(str);

for(i=0;str[i]!='\0';i++)
{
V+=isVowel(str[i]);
C+=isConsonant(str[i]);
D+=isDigit(str[i]);
W+=isWhitespace(str[i]);
S+=isSpecial(str[i]);
T++;
}
printf("Vowels:%d\n",V);
printf("Consonant:%d\n",C);
printf("Digits:%d\n",D);
printf("White space:%d\n",W);

Maps College, Mangalore. Page 22


printf("Total letters:%d\n",T);
printf("Special Character:%d\n",S);
getch();
}

int isVowel(char c){


if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
{
return 1;
}
else{
return 0;
}
}
int isConsonant(char c){
if(((c>='a'&&c<='z')||(c>='A'&&c<='Z'))&&!isVowel(c))
{
return 1;
}
else{
return 0;
}
}
int isDigit(char c){
if(c>='0'&&c<='9')
{
return 1;
}
else{
return 0;
}

Maps College, Mangalore. Page 23


}
int isWhitespace(char c){
if(c==' ')
{
return 1;
}
else{
return 0;
}
}
int isSpecial(char c)
{
if(c==' ')
{
return 1;
}
else{
return 0;
}
}

Output:

Maps College, Mangalore. Page 24


6. Program to sort a list of strings in ascending order using Pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
char *T;
int I,J,K,n;
char *ARRAY[100];
//clrscr();

printf("Enter how many strings you want:\n");


scanf("%d",&n);
for(I=0;I<n;I++)
{
printf("Enter %d string:\n",I+1);
ARRAY[I]=(char *)malloc(20*sizeof(char));
scanf("%s",ARRAY[I]);
}

printf("Entered strings are:\n");


for(I=0;I<n;I++)
{
printf("%s\t",ARRAY[I]);
}

printf("\n sorted list are: \n");


for(I=0;I<n;I++)
{
for(J=0; J<n-1; J++)
{
K=strcmp(ARRAY[J],ARRAY[J+1]);

Maps College, Mangalore. Page 25


if(K>0)
{
T=ARRAY[J];
ARRAY[J]=ARRAY[J+1];
ARRAY[J+1]=T;
}
}
}
for(I=0;I<n;I++)
{
printf("%s\t", ARRAY[I]);
}
getch();
}

Output:

Maps College, Mangalore. Page 26


7. Program to enter the information of a student like name, register
number, marks in three subjects into a structure and display total, average
and grade Display details in a neat form.

#include<stdio.h>
#include<conio.h>
struct student
{
int regno;
char name[10];
int m1;
int m2;
int m3;
};
void main()
{
struct student s[10];
int n,i,total;
float avg;
printf("**********OUTPUT**********");
printf("\n ENTER THE NUMBER OF STUDENTS: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("ENTER THE REGNO: ");
scanf("%d",&s[i].regno);
fflush(stdin);
printf("\n ENTER THE STUDENT NAME: ");
gets(s[i].name);
printf("\nENTER THE MARKS IN 3 SUBJECTS: ");
scanf("%d %d %d",&s[i].m1,&s[i].m2,&s[i].m3);
printf("\n...............\n");
}
printf("\n RNO \t NAME \t M1 \t M2 \t M3 \t TOTAL \t AVERAGE\t\
tGRADE\n");
for(i=0;i<n;i++)
{

Maps College, Mangalore. Page 27


total=s[i].m1+s[i].m2+s[i].m3;
avg=(total/3.0);
printf("%d\t",s[i].regno);
printf("%s\t",s[i].name);
printf("%d\t %d\t %d\t %d\t %f\t",s[i].m1,s[i].m2,s[i].m3,total,avg);

if(s[i].m1<35||s[i].m2<35||s[i].m3<35)
{
printf("fail");
}
else if(avg>=70)
{
printf("distinction");
}
else if(avg>=60)
{
printf("first class");
}
else if(avg>=50)
{
printf("second class");
}
else
{
printf("third class");
}
printf("\n");
}

getch();

Maps College, Mangalore. Page 28


Output:

8. Write a menu driven program to

Maps College, Mangalore. Page 29


a. Create a text file
b. Append the contents of a text file to another existing file by accepting
filenames
c. Display the content of entered filename
d. Exit
Create two text files during the execution of the program. Display their
content. Perform Appending. Display the contents again. Always check for
the existence of the inputted file names.

#include <stdio.h>
#include <stdlib.h>
void create_file() {
char filename[100], data[100];
printf("Enter filename: ");
scanf("%s", filename);
FILE *fp = fopen(filename, "w");
if (fp == NULL)
{
printf("Error creating file.\n");
return;
}
printf("Enter data: ");
scanf("\n");
fgets(data, sizeof(data), stdin);
fputs(data, fp);
fclose(fp);
printf("File created successfully.\n");
}

void append_file() {
char file1[100], file2[100];
printf("Enter filename to append from: ");
scanf("%s", file1);

Maps College, Mangalore. Page 30


printf("Enter filename to append to: ");
scanf("%s", file2);
FILE *fp1 = fopen(file1, "r");
FILE *fp2 = fopen(file2, "a");
if (fp1 == NULL || fp2 == NULL)
{
printf("Error opening file.\n");
return;
}
char data[100];
while (fgets(data, sizeof(data), fp1) != NULL) {
fputs(data, fp2);
}
fclose(fp1);
fclose(fp2);
printf("File appended successfully.\n");
}

void display_file() {
char filename[100];
printf("Enter filename: ");
scanf("%s", filename);
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
printf("Error opening file.\n");
return;
}
char data[100];
while (fgets(data, sizeof(data), fp) != NULL){
printf("%s", data);

Maps College, Mangalore. Page 31


}
fclose(fp);
}

int main() {
int choice;
while (1) {
printf("\n Menu:\n");
printf("1. Create File\n");
printf("2. Append File\n");
printf("3. Display File\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice) {
case 1:
create_file();
break;
case 2:
append_file();
break;
case 3:
display_file();
break;
case 4:
exit(0);
default:
printf("Invalid choice.\n");
}
}
return 0;

Maps College, Mangalore. Page 32


}

Output:

Maps College, Mangalore. Page 33

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy