0% found this document useful (0 votes)
7 views5 pages

C Slip Answers 21-30

The document contains multiple C programs that demonstrate various functionalities such as accepting employee details, calculating squares and cubes using macros, finding the longest city name, converting strings to lowercase, and reading file contents. Each program includes user input for specific data types and structures, and displays results based on certain conditions. The programs also illustrate basic operations with arrays and structures in C.

Uploaded by

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

C Slip Answers 21-30

The document contains multiple C programs that demonstrate various functionalities such as accepting employee details, calculating squares and cubes using macros, finding the longest city name, converting strings to lowercase, and reading file contents. Each program includes user input for specific data types and structures, and displays results based on certain conditions. The programs also illustrate basic operations with arrays and structures in C.

Uploaded by

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

Write a program to accept ‘n’ employee details (eno, ename, salary) and display all employee details whose

salary is more than 10000


#include<stdio.h>
#include<conio.h>
struct Employee{
int eno;
char ename[50];
float salary;
};
void main()
{
int n,i;
printf("Enter the number of employees: ");
scanf("%d",&n);
struct Employee employees[n];
for(i=0;i<n;i++)
{
printf("\nEnter details of the employee %d",i+1);
printf("\nEnter the employee number: ");
scanf("%d",&employees[i].eno);
printf("\nEnter the employee name: ");
scanf("%s",&employees[i].ename);
printf("\nEnter the employee salary: ");
scanf("%f",&employees[i].salary);
}
printf("\nDetails of the employee more than 10000 rs salary:");
for(i=0;i<n;i++){
if(employees[i].salary>10000){
printf("\nEmployee Number: %d",employees[i].eno);
printf("\nEmployee Name: %s",employees[i].ename);
printf("\nEmployee Salary: %.2f",employees[i].salary);
}
}
getch();
}

Write a C program to calculate SQUARE of given number using MACRO.


#include<stdio.h>
#include<conio.h>
#define squareofnumber(x) (n*n)
void main()
{
int n,square;
printf("Enter the number: ");
scanf("%d",&n);
square=squareofnumber(n);
printf("\nSquare of the given number is: %d",square);
getch();
}
Write a program to accept names of ‘n’ cities. Find the name of the city having maximum characters.
#include<stdio.h>
#include<conio.h>
#define max_length 100
#define max_cities 100
void main()
{
int n,i,maxlength=0;
char city[max_length][max_cities];
printf("Enter the number of cities: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the name of city %d: ",i+1);
scanf("%s",&city[i]);
}
for(i=1;i<n;i++){
if(strlen(city[i])>strlen(city[maxlength])){
maxlength=i;
}
}
printf("\nThe city with maximum length is: %s",city[maxlength]);
getch();
}

Write a C program to accept a string and convert it into lower case using standard library function
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i;
char str[100];
printf("Enter the string: ");
scanf("%d",&str);
for(i=0;i<strlen(str);i++){
str[i]=tolower(str[i]);
}
printf("String in lower case is: %s",str);
getch();
}

Write a program to accept details of n items (code, name, price) using structure and display the details.
#include<stdio.h>
#include<conio.h>
struct Item{
int code;
char name[50];
int price;
};
void main()
{
int n,i;
struct Item items[n];
printf("Enter the number of items: ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter the details of the item %d",i+1);
printf("\nEnter the code of item: ");
scanf("%d",&items[i].code);
printf("\nEnter the name of item: ");
scanf("%s",&items[i].name);
printf("\nEnter the price of item: ");
scanf("%d",&items[i].price);
}
for(i=0;i<n;i++)
{
printf("\nDetails of items are...");
printf("\nItem Code: %d",items[i].code);
printf("\nItem Name: %s",items[i].name);
printf("\nItem Price: %d",items[i].price);
}
getch();
}

Write a program to accept a string and an index from user and displays the character at that specific index.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[100];
int index;
printf("Enter the string: ");
scanf("%s",&str);
printf("Enter the index: ");
scanf("%d",&index);
if(index>=0 && index<strlen(str)){
printf("Character at the index %d: %c",index,str[index]);
}
else
{
printf("Invalid index!!!Index should be between 0 and %d",strlen(str)-1);

}
getch();
}

Write a C program to find minimum elements of 1D array


#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,arr[100],min;
printf("Enter the size of the array: ");
scanf("%d",&n);
printf("Enter the elements in the array: ");
for(i=0;i<n;i++){
scanf("%d",&arr[i]);
}
min=arr[0];
for(i=0;i<n;i++){
if(arr[i]<min){
min=arr[i];
}
}
printf("Minimum number in the array is: %d",min);
getch();
}

Write a program to calculate cube of a number by using macro


#include<stdio.h>
#include<conio.h>
#define cubeofnumber(x) (n*n*n)
void main()
{
int n,cube;
printf("Enter the number: ");
scanf("%d",&n);
cube=cubeofnumber(n);
printf("Cube of the given number is: %d",cube);
getch();
}

Write a program to accept details of ‘n’ books (bno, bname, price). Display all the details. Also display all
books having price > 100.
#include<stdio.h>
#include<conio.h>
struct Book{
int bno;
char name[50];
int price;
};
void main()
{
int n,i;
struct Book books[n];
printf("Enter the number of the books: ");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter the details of book %d",i+1);
printf("\nEnter the number of book: ");
scanf("%d",&books[i].bno);
printf("\nEnter the name of book: ");
scanf("%s",&books[i].name);
printf("\nEnter the price of book: ");
scanf("%d",&books[i].price);
}
printf("\nDetails of books are: ");
for(i=0;i<n;i++){
printf("\nBook Number: %d",books[i].bno);
printf("\nBook Name: %s",books[i].name);
printf("\nBook Price: %d",books[i].price);
}
printf("\n\nBook Name having price greater than 100: ");
for(i=0;i<n;i++){
if(books[i].price>100){
printf("\nBook Name: %s",books[i].name);
printf("\tBook Price: %d",books[i].price);
}
}
getch();
}

Write a program to read the contents of a text file and count the number of lines in the file.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *file;
char c;
int linecount=0;
char filename[100];
printf("Enter the file name: ");
scanf("%s",&filename);
file=fopen(filename,"r");
if(file==NULL){
printf("Could not open the file!!!");
return 1;
}
while((c=fgetc(file))!=EOF){
if(c=='\n'){
linecount++;
}
}
fclose(file);
printf("Number of lines in the file: %d",linecount+1);
getch();
}

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