Cool
Cool
Write a C program to read 3 integer numbers and find the average of those 3
numbers?
CODE:
#include <stdio.h>
int main()
{
int a,b,c;
float avg;
scanf("%d %d %d",&a,&b,&c);
avg=(a+b+c)/3.0;
printf("Average of 3 nos. is: %.2f",avg);
return 0;
}
OUTPUT:
2. Write a C program to check whether the given no. is even or odd using ternary
operator?
CODE:
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
(n%2==0)?printf("n is even no."):printf("n is odd no.");
return 0;
}
OUTPUT SCREENSHOTS:
3. Write a C program to read 4 integers and display max no. & min no.?
CODE:
#include <stdio.h>
int main()
{
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
int max,min;
max=min=a;
if(max<a)
max=a;
else if(min>a)
min=a;
if(max<b)
max=b;
else if(min>b)
min=b;
if(max<c)
max=c;
else if(min>c)
min=c;
if(max<d)
max=d;
else if(min>d)
min=d;
printf("Max no. is %d",max);
printf("\nMin no. is %d",min);
return 0;
}
OUTPUT:
4. Write a C program for opting the program offered by VIT University? The students
are eligible for choosing the program when he/she gets the rank less than 1,00,000.
Programs offered:
1) 1 to 1000 rank can choose B.Tech CSE
2) 1001 to 10000 rank can choose B.Tech Electronics
3) 10001 to 50000 rank can choose B.Tech Mechanical
4) 50001 to 100000 rank can choose B.Tech Civil.
CODE:
#include <stdio.h>
int main()
{
int rank;
printf("Enter the Rank\n");
scanf("%d",&rank);
if(rank > 100000)
{
printf("Not elgible to get Admission in VIT University");
return 0;
}
else
{
if(rank>0 && rank<=1000)
{
printf("Programs that you are eligible to choose are:\n");
printf("1. B.Tech CSE\n");
printf("2. B.Tech EEE\n");
printf("3. B.Tech ME\n");
printf("4. B.Tech CE\n");
return 0;
}
else if(rank>1000 && rank<=10000)
{
printf("Programs that you are eligible to choose are:\n");
printf("1. B.Tech EEE\n");
printf("2. B.Tech ME\n");
printf("3. B.Tech CE\n");
return 0;
}
else if(rank>10000 && rank<=50000)
{
printf("Programs that you are eligible to choose are:\n");
printf("1. B.Tech ME\n");
printf("2. B.Tech CE\n");
return 0;
}
else
{
printf("Programs that you are eligible to choose are:\n");
printf("Only B.Tech CE\n");
return 0;
}
}
}
OUTPUT:
5. Write a C program to print whether the give no. is prime or not in the range of 200
to 500?
CODE:
#include <stdio.h>
#include <stdbool.h>
int main()
{
for(int i=200;i<=500;i++)
{
if(i%2 !=0 && i%3!=0)
{
bool isPrime=true;
for(int j=5;j*j<=i;j+=6)
{
if(i%j==0 || i%(j+2)==0)
{
isPrime=false;
break;
}
}
if(isPrime)
printf("%d ",i);
}
}
return 0;
}
OUTPUT:
6. Write a C program to print the following pattern for n time consider test case as
n=5?
A
AB
ABC
ABCD
ABCDE
CODE:
/*5. Write a C program to print the following pattern for n time consider test case as n=5?
A
AB
ABC
ABCD
ABCDE
*/
#include <stdio.h>
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
char ch ='A';
for(int j=0;j<=i;j++)
{
printf("%c ",ch);
ch++;
}
printf("\n");
}
return 0;
}
OUTPUT:
7. Write a C program to find sum of even nos. & sum of odd nos. in an array?
CODE:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int e=0,o=0;
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
e+=a[i];
else
o+=a[i];
}
printf("Even sum is: %d",e);
printf("\nOdd sum is: %d",o);
return 0;
}
OUTPUT:
8. Write a C program to calculate max and 2nd max from the given 10 nos. without
using sorting techniques?
CODE:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int a[n];
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int m1=a[0],m2=a[0];
for(int i=0;i<n;i++)
{
if(a[i]>m1)
{
m2=m1;
m1=a[i];
}
}
printf("Max element is: %d",m1);
printf("\n2nd Max element is: %d",m2);
}
OUTPUT:
9. Write a C program for 2x2 matrix and display sum, subtraction,
multiplication of 2 matrices.
CODE:
#include <stdio.h>
int main()
{
int a[2][2],b[2][2],add[2][2],sub[2][2],mul[2][2];
printf("Give input for 1st matrix:\n");
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
scanf("%d",&a[i][j]);
printf("Give input for 2nd matrix:\n");
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
scanf("%d",&b[i][j]);
int main()
{
int a[3][3];
for(int i=0;i<3;i++)
for( int j=0;j<3;j++)
scanf("%d",&a[i][j]);
printf("\nTransposed Matrix is:\n");
for(int i=0;i<3;i++)
{
for( int j=0;j<3;j++)
printf("%d ",a[j][i]);
printf("\n");
}
return 0;
}
OUTPUT:
11. Write a C program to compute sum of left diagonal & right diagonal elements?
CODE:
#include <stdio.h>
int main()
{
int a[3][3];
for(int i=0;i<3;i++)
for( int j=0;j<3;j++)
scanf("%d",&a[i][j]);
int left_Dsum=0,right_Dsum=0;
for(int i=0;i<3;i++)
{
left_Dsum+=a[i][i];
if(i!=(3/2))
right_Dsum+=a[i][3-i-1];
}
printf("Total of Left and Right diagonal including centre element only once
is:\n%d",left_Dsum+right_Dsum);
return 0;
}
OUTPUT:
5 10 15 12 11
4 2 7 9 10
55 20 17 18 19
13 12 15 11 16
7 6 3 1 2
CODE:
#include <stdio.h>
#include <stdbool.h>
bool isPrime(int n)
{
if(n<=1) return false;
if(n==2 || n==3) return true;
if(n%2 == 0 || n%3 ==0) return false;
for(int i=5;i*i<=n;i+=6)
if(n%i==0 || n%(i+2)==0) return false;
return true;
}
int main()
{
int n;
scanf("%d",&n);
int a[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\n\n");
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(isPrime(a[i][j]))
printf("* ");
else
{
if(EvenOdd(a[i][j])) printf("+ ");
else printf("- ");
}
}
printf("\n");
}
return 0;
}
OUTPUT:
int main()
{
int n,fact=1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
fact*=i;
printf("\nFactorial of the given n is: %d",fact);
return 0;
}
OUTPUT:
int main()
{
int a=0,b=1,c,n;
scanf("%d",&n);
for(int i=2;i<n;i++)
{
c=a+b;
a=b;
b=c;
}
printf("\nNth Fibonacci is: %d",c);
return 0;
}
OUTPUT:
int fact(int x)
{
int f=1;
for(int i=1;i<=x;i++)
f*=i;
return f;
}
int main()
{
int n;
scanf("%d",&n);
float sum=1;
for(int i=1;i<=n;i++)
sum+= (float) i/fact(i);
printf("\nSum of the sequence upto n is: %f",sum);
return 0;
}
OUTPUT:
int main()
{
int n;
scanf("%d",&n);
int rev=0;
while(n>0)
{
int rem=n%10;
rev=rev*10+rem;
n/=10;
}
printf("\nReverse of the given no. is: %d",rev);
return 0;
}
OUTPUT:
14. Write a C program to read 5 students names print the names of the given students?
CODE:
#include <stdio.h>
int main()
{
char str[5][20];
for(int i=0;i<5;i++)
scanf("%19s",str[i]);
for(int i=0;i<5;i++)
printf("\nThe names are: %s\n",str[i]);
return 0;
}
OUTPUT:
15. Write a C program to find length of a given string without using length function?
CODE:
#include <stdio.h>
int main() {
int n;
printf("Enter the number of strings: ");
scanf("%d", &n);
char str[n][20];
int i = 0;
while (str[0][i] != '\0') {
i++;
}
printf("Length of the first string is: %d\n", i);
return 0;
}
OUTPUT:
int main() {
int n;
char strings[n][20];
printf("Enter %d strings:\n", n);
for (int i = 0; i < n; i++) {
scanf("%s", strings[i]);
}
if (isPalindrome) {
printf("%s is a palindrome.\n", strings[i]);
} else {
printf("%s is not a palindrome.\n", strings[i]);
}
}
return 0;
}
OUTPUT:
1. Write a program to read 10 students fname and lname and combine them and then
print?
CODE:
#include <stdio.h>
int main() {
char first_names[10][20];
char last_names[10][20];
char full_names[10][40];
concatenate(full_names[i], first_names[i]);
concatenate(full_names[i], " ");
concatenate(full_names[i], last_names[i]);
}
return 0;
}
OUTPUT:
2. Write a program to compute SI function with 3 arguments?
CODE:
#include <stdio.h>
int main() {
float principal, rate, time;
printf("Enter principal amount: ");
scanf("%f", &principal);
return 0;
}
OUTPUT SCREENSHOTS:
CODE:
#include <stdio.h>
int factorial(int n) {
int result = 1;
return 0;
}
OUTPUT:
4. Write a c program to swap given 2 values using funtions pass by value and
reference?
CODE:
#include <stdio.h>
int swapbyRef(int *a,int *b)
{
return *b=*a+*b-(*a=*b);
}
OUTPUT:
OUTPUT:
CODE:
A) Factorial of a given no.
#include <stdio.h>
int fact(int n)
{
int f = 1;
if(n<2) return f;
f *=n;
return f*fact(n-1);
}
int main()
{
int n;
scanf("%d",&n);
int result = fact(n);
printf("Factorial of the given number using Recursion is: %d",result );
return 0;
}
OUTPUT:
OUTPUT:
7. Write a program to read a line of text and calculate or display no. of vowels and no.
of consonants apart from that display the no. of spaces in the given line.
CODE:
#include <stdio.h>
#include <string.h>
int main()
{
char c[75];
scanf("%s",c);
int vowels=0,spaces=0,consonants=0;
for(int i=0;i<strlen(c);i++)
{
if(c[i] == 'a' || c[i]=='e'||c[i]=='i'||c[i]=='o' || c[i] =='u')
vowels++;
else if(c[i]==' ') spaces++;
else if(c[i]>='a' && c[i]<='z') consonants++;
}
return 0;
}
OUTPUT:
8. Write a program to check whether the given string is a substring of given line of
text.
CODE:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char str[20],check[20];
printf("Enter original String\n");
scanf("%s",str);
printf("Enter Sub String\n");
scanf("%s",check);
for(int i=0;i<=strlen(str)-strlen(check);i++)
{
int j;
for(j=0;j<strlen(check);j++)
{
while (str[i] == ' ')
i++;
if(str[i+j]!=check[j]) break;
}
if(j==strlen(check))
{
printf("yes it is sub string");
exit(0);
}
}
printf("Not a Sub String");
return 0;
}
OUTPUT:
9. Write a c program to compute sum of N elements using dynamic
memory allocation?
CODE:
#include <stdio.h>
#include<stdlib.h>
int main()
{
int n,sum=0,*array;
array=(int *)malloc(n*sizeof(int));
for(int i=0;i<n;i++)
{
scanf("%d",&array[i]);
sum+=array[i];
}
printf("Sum of elements\n");
printf("%d",sum);
return 0;
}
OUTPUT:
10. Writ a c program to find maximum and minimum of given n elements using
Dynamic Memory Allocation?
CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter number of elements\n");
scanf("%d",&n);
int *array;
array = (int *)malloc(n*sizeof(int));
for(int i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
int min=array[0],max = array[0];
for(int i=0;i<n;i++)
{
if(array[i]<min) min=array[i];
else if(array[i]>max) max = array[i];
}
printf("Max value in the array is: %d \nMin value in the array is: %d",max,min);
return 0;
}
OUTPUT:
1. Write a case-insensitive function that will take a string and count of each letter in the
string.
E.g: VIT Preparing for RIVERA
condition: you function should take single string argument and return a dynamically
allocated array of 26 integers representing the count of each of the letter A-z respectively.
CODE:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include <stdlib.h>
int main()
{
char str[50];
fgets(str, sizeof(str), stdin);
int *array = count(str);
for(int i=0;i<26;i++)
{
if(array[i]>0)
printf("%c %d\n",(char) (97+i),array[i]);
}
free(array);
}
OUTPUT:
2. Write a c program to need N student records and calculate the average marks of each
student and also display it by calling the function called display? passing structure
into function arguments
The attributes of student record are
Student no. - int
Student name - string of length 25
M1 - int
M2 - int
M3 - int
Average - float?
CODE:
#include <stdio.h>
#include <string.h>
// Define a structure for student record
struct StudentRecord {
int studentNo;
char studentName[25];
int m1;
int m2;
int m3;
float average;
};
int main() {
int n;
printf("Enter the number of students: ");
scanf("%d", &n);
return 0;
}
OUTPUT SCREENSHOTS:
3. Write a c program to read 2 distances in feet and inches and display the sum of two
distances. Using structure?
CODE:
#include <stdio.h>
// Add inches
sum.inches = d1.inches + d2.inches;
return sum;
}
int main() {
struct Distance distance1, distance2, sum;
// Input distance 1
printf("Enter distance 1 (feet inches): ");
scanf("%d %d", &distance1.feet, &distance1.inches);
// Input distance 2
printf("Enter distance 2 (feet inches): ");
scanf("%d %d", &distance2.feet, &distance2.inches);
// Add distances
sum = addDistances(distance1, distance2);
return 0;
}
OUTPUT:
4. Write a C program to maintain a list of N elements dynamically and implement the
following operations
1) Insert
2) Delete
3) Display
CODE:
#include <stdio.h>
#include <stdlib.h>
if (head == NULL)
{
head = newNode;
}
else
{
Node* current = head;
while (current->next != NULL)
{
current = current->next;
}
current->next = newNode;
}
}
if (current == NULL)
{
printf("Node not found\n");
return;
}
if (prev == NULL)
{
head = current->next;
}
else
{
prev->next = current->next;
}
free(current);
}
void display()
{
Node* current = head;
if (current == NULL)
{
printf("Linked list is empty\n");
}
else
{
while (current != NULL)
{
printf("%d ", current->val);
current = current->next;
}
printf("\n");
}
}
int main()
{
int choice, data;
while (1)
{
printf("\nMenu:\n");
printf("1. Insert at the end\n");
printf("2. Delete a value\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Enter value to insert: ");
scanf("%d", &data);
insertEnd(data);
break;
case 2:
printf("Enter value to delete: ");
scanf("%d", &data);
delete(data);
break;
case 3:
printf("Linked list: ");
display();
break;
case 4:
printf("Exiting program.\n");
exit(0);
default:
printf("Invalid choice. Please try again.\n");
}
}
return 0;
}
OUTPUT:
1. Write a C program to compute maximum of n values using varaidic functions?
CODE:
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
va_end(list);
return max_value;
}
int main() {
printf("Maximum value: %d\n", max(6, 113, 1, 222, 5, 6, 99));
return 0;
}
OUTPUT:
2. Write a C program to compute sum of n values using varaidic functions?
CODE:
#include <stdio.h>
#include <stdarg.h>
int total = 0;
for (int i = 0; i < n; i++) {
total += va_arg(args, int);
}
va_end(args);
return total;
}
int main() {
// Example usage of sum function
printf("Sum: %d\n", sum(5, 10, 20, 30, 40, 50)); // Sum: 150
printf("Sum: %d\n", sum(3, 1, 2, 3)); // Sum: 6
return 0;
}
OUTPUT SCREENSHOTS:
3. Write a C program to read the contents of the file named "sample.txt" and print the
contents into the console ?
CODE:
#include <stdio.h>
int main() {
FILE *file;
char ch;
OUTPUT:
4. Describe all formatted and unformatted i/p o/p statements and show implemented
examples of each Statement?
CODE:
#include <stdio.h>
#include <string.h>
int main() {
// Formatted Input
int num;
printf("Formatted Input: Enter an integer: ");
scanf("%d", &num);
// Formatted Output
printf("Formatted Output: The number you entered is: %d\n", num);
// Unformatted Input
char ch;
printf("Unformatted Input: Enter a character: ");
ch = getchar(); // Consume newline character from previous scanf
ch = getchar(); // Read the actual character entered by the user
// Unformatted Output
printf("Unformatted Output: You entered the character: ");
putchar(ch);
putchar('\n');
return 0;
}
OUTPUT SCREENSHOTS:
5. Write a c program to write sample text information into a file and display the same
on the screen along with number of vowels and no. Of consonants count.
CODE:
#include <stdio.h>
#include <ctype.h>
int main() {
// Write sample text information into a file
FILE *file = fopen("sample.txt", "w");
if (file == NULL) {
printf("Error: Could not open the file.\n");
return 1;
}
// Read the file and display the text along with vowel and consonant counts
file = fopen("sample.txt", "r");
if (file == NULL) {
printf("Error: Could not open the file.\n");
return 1;
}
char buffer[1000];
fgets(buffer, sizeof(buffer), file);
fclose(file);
return 0;
}
OUTPUT:
6. Write a c program to write the text into the file consisting of special characters and
calculate or compute no. Of special characters available in the given file..
CODE:
#include <stdio.h>
#include <ctype.h>
int main() {
// Write text with special characters into a file
FILE *file = fopen("special.txt", "w");
if (file == NULL) {
printf("Error: Could not open the file.\n");
return 1;
}
const char *text = "This is a sample text with special characters: !@#$%^&*()";
fprintf(file, "%s", text);
fclose(file);
printf("Text with special characters written to file successfully.\n");
char buffer[1000];
fgets(buffer, sizeof(buffer), file);
fclose(file);
return 0;
}
OUTPUT:
7. Write a c program to read or write a line of text in the position of nth line?
CODE:
#include <stdio.h>
#include <stdlib.h>
8. Write a c program to display last n characters of from the given file “sample.txt”.
CODE:
#include <stdio.h>
#include <stdlib.h>
fclose(file);
}
int main() {
FILE *fp;
int n;
char ch;
// Prompt the user to enter the number of characters to read from the file
printf("Enter number of characters to read from the file: ");
scanf("%d", &n);
// Move the file pointer to the appropriate position to read the last n characters
fseek(fp, -n, SEEK_END);
return 0;
}
OUTPUT:
Q) Write a function count(number, array, length) that counts the number of times number
appears in array. The array has length elements. The function should be recursive.
Code:
if(len==0) return 0;
Q) A Martian leap year is all years divisible by 5 or divisible by 7, but not divisible by both.
Write a C function it reads in the year from the user to consider, it prints out “YES” if the year is
a Martian leap year and “NO” otherwise.
Code:
else printf("No");
Code:
#include <stdio.h>
typedef struct {
int Empno;
char Empname[50];
int Age;
float Basic;
float DA;
float HRA;
float MA;
float PT;
float Tax;
float EPF;
} Employee;
// Function to compute Gross salary
int main() {
int N;
scanf("%d", &N);
Employee employees[N];
scanf("%d", &employees[i].Empno);
scanf("%s", employees[i].Empname);
printf("Age : ");
scanf("%d", &employees[i].Age);
printf("Basic : ");
scanf("%f", &employees[i].Basic);
employees[i].PT = 500;
// Output
printf("\nABC Company\n\nVellore\n");
printf("**********************************************************\n");
printf("**********************************************************\n");
printf("**********************************************************\n");
printf("Earnings: Deductions:\n");
printf("**********************************************************\n");
printf("**********************************************************\n");
return 0;
Q) Design a structure to store time and date. Write a function to find the difference between two
times in minutes.
Code:
#include <stdio.h>
typedef struct {
int day;
int month;
int year;
int hour;
int minute;
} DateTime;
int main() {
return 0;