Steam Table
Steam Table
/*Write a function that takes an integer number of any digits and return the number
reversed in the calling function read a number pass it to the function and display the
result. */
#include<stdio.h>
#include<conio.h>
int reverse(int);
main()
{
int n,r;
printf("Enter number:");
scanf("%d",&n);
r=reverse(n);/*function call by passing argument n i.e value of n is passed */
printf("Reverse number is %d",r);
getch();
}
int reverse(int n)
{
int rem,s=0;
while(n!=0)
{
rem=n%10;/* remainder i.e last digit of n*/
s=s*10+rem; /* multiplying previous sum by 10 i.e. shifiting its postion and
remainder is added */
n=n/10; /* quotient i.e to decrease number size by 1 digit */
}
return s;
}
/*WAP to count and find the sum of all numbers in the array which are exactly divisible by 7
but not by 5 */#include<stdio.h>
#include<conio.h>
int main()
{
int a[20],n,s=0,c=0,i;
printf("How many numbers do you have?:" );
scanf("%d",&n);
printf("Enter elements:");
for(i=0;i<n;i++) /* loop for each element of array */
{
scanf("%d",&a[i]);
if(a[i]%7==0 && a[i]%5!=0) /* checking whether number meets condtion or not */
{
s+=a[i];
c++;
}
}
printf("sum is : %d and count is : %d",s,c);
getch();
return 0; }
/*WAP to print the Fibonacci series up to n using recursive function. */
#include<stdio.h>
#include<conio.h>
void fibo(int,int,int);
int main()
{
int n;
printf("Enter number of terms:");
scanf("%d",&n);
fibo(0,1,n);
getch();
return 0;
}
void fibo(int a,int b, int n)/*a =0 ,b=1, n=n initially */
{
if(n>0)/*terminating condition */
{
printf("%d\t",a); /* value of a is displayed till n becomes 0 */
fibo(b,a+b,n-1); /*function call inside function fibo so recursive) */
}
}
/*WAP to print the Fibonacci series until the term is less than 500 using recursive function.
*/#include<stdio.h>
#include<conio.h>
void fibo(int,int);
int main()
{
int n;
fibo(0,1);
getch();
}
void fibo(int a,int b) /*a =0 ,b=1 initially */
{
if(a<500) /*terminating condition*/
{
printf("%d\t",a); /* value of a is displayed till a <500 */
fibo(b,a+b); /*function call inside function fibo so recursive) */
}}/*WAP to read two integers from the user.Pass it to functions that calculates the HCF and LCM
.Display the result form the main function.
-> to return more than one value use concept of pointer */
#include<stdio.h>
#include<conio.h>
void calc(int,int,int*,int*);
int main()
{
int a,b,lcm,hcf;
printf("Enter two numbers:");
scanf("%d%d",&a,&b);
calc(a,b,&hcf,&lcm);
printf("HCF is %d and LCM is %d.",hcf,lcm);
getch();
}
void calc(int x,int y,int *h,int *l)
{
int i;
for(i=1;i<=x;i++)
{
if(x%i==0 && y%i==0)
*h=i;
}
*l=(x*y)/(*h);
}
/*WAP to display all the prime numbers within the ranges specified by the users using
function */
#include<stdio.h>
#include<conio.h>
int prime(int);
void main()
{
int n1,n2,i,p;
printf("Enter range:");
scanf("%d%d",&n1,&n2);
for(i=n1;i<=n2;i++) /* loop to check that each number within range are prime or not */
{
p=prime(i); /* sending all numbers from n1 to n2 to function */ if(p==0) /* compares to return
value and return 0 if number is prime */ printf("%d\t",i);
}
getch();
}
int prime(int n) /*n=i value */
{
int i;
for(i=2;i<n;i++) /*loop to check that each number n is prime or not so dividing by 2 upto n-1
*/
{
if(n%i==0) /* if number get divide by any number it is sure that it is not prime so break */
break;
}
if(n==i) /* if number is prime then n equals to i and return 0 for such case*/
return 0;
else
return 1;
}
/*WAP to find using recursive function */
#include<stdio.h>
#include<conio.h>
int power(int,int);
int main()
{
int n,x;
printf("Enter base number and exponent:");
scanf("%d%d",&n,&x);
printf("%d to the power %d is:%d",n,x,power(n,x));
getch();
return 0;
}
int power(int n,int x)
{
if(x==0)
return 1;
else
return n*power(n,x-1);
}
Array and string
/*WAP that takes string from the user and pass it to function. The function finds and
returns the
number of words in the string.In the main program display the number of words.
->program logic:
->take string
->pass to function
->caculate number of words in function
-> return number of words from functions as result is to display in main function */
#include<stdio.h>
#include<conio.h>
int wcount(char [])
void main()
{
char s[[30];
int l;
gets(s);
l=wcount(s);
printf("NUMber of words %d",l+1);
getch();
return 0;
}
int wcount(char s[])
{
int i,l=0;
for(s=0;s[i]!='\0';i++)
{
if(s[i]==' ')
l++;
}
return l;
}
/* WAp that reads string form user and uses a user defined function to copy the contents of the
read string into another character array changing lower case letters to upper case and upper case
letters to lower case*/
#include<stdio.h>
#include<conio.h>
void process(char [],char []);
int main()
{
char a[50],b[50];
printf("Enter string:");
gets(a);
process(a,b);/*function doesnot return value but changes are seen in main as both a and b are
arrays */
printf("The changed string is: %s",b);
getch();
return 0;
}
void process(char a[],char b[])
{
int i;
for(i=0;a[i]!='\0';i++)
{
if(*(a+i)>='A' &&*(a+i)<='Z')
*(b+i)=*(a+i)+32; /*content of a is copied to b by changing to lowercase*/ else
if(*(a+i)>='a' && *(a+i)<='z')
*(b+i)=*(a+i)-32; /*content of a is copied to b by changing to uppercase*/ else
*(b+i)=*(a+i); /*characters of a is copied to b if space,numbers,other symbols are
encountered*/
}
*(b+i)='\0';
}
printf("Enter position:");
scanf("%d",&n);
insert(m,s,n);
printf("New string:");
puts(m);
getch();
return 0;
}
void insert(char *m,char *s,int p)
//void insert(char m[],char s[],int p)
{
int i,j;
char temp[50];
for(i=p-1,j=0;m[i]!='\0';i++,j++)
temp[j]=m[i];
temp[j]='\0';
for(i=p-1,j=0;s[j]!='\0';i++,j++)
m[i]=s[j];
for(j=0;temp[j]!='\0';i++,j++)
m[i]=temp[j];
m[i]='\0';
}
/*WAP to read 5 names of students and sort them alphabetically. The process must be done by
the user defined function */
#include<stdio.h>
#include<conio.h>
#include<string.h>
void sort(char [][10]);
int main()
{
int i;
char name[5][10];
printf("Enter name of five persons:");
for(i=0;i<5;i++)
gets(name[i]);
sort(name);
for(i=0;i<5;i++)
printf("%s\t",name[i]); //puts(name[i]);
getch();
}
void sort(char name[][10])
{
int i,j;
char temp[10];
for(i=0;i<4;i++)
{
for(j=i+1;j<5;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[i]);
strcpy(name[i],temp);
}}
}
}
/*WAP that passes array to a function and print the largest and smallest element */
#include<stdio.h>
#include<conio.h>
void func(int[],int);
int main()
{
int i,a[20],n;
printf("Enter elements number:");
scanf("%d",&n);
printf("Enter elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
func(a,n);//function call passing array and element
number getch();
}
void func(int a[],int n)
{
int max=a[0],min=a[0],i;
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
printf("max is:%d\n",max);
printf("min is:%d",min);
}
/*WAP that passes array to a function and print the largest and smallest element */
//using pointer notation
#include<stdio.h>
#include<conio.h>
void func(int*,int);
int main()
{
int i,a[20],n;
printf("Enter elements number:");
scanf("%d",&n);
printf("Enter elements:");
for(i=0;i<n;i++)
scanf("%d",a+i);
func(a,n);//function call passing array and element
number getch();
}
void func(int *a,int n)
{
int max=a[0],min=a[0],i;
for(i=1;i<n;i++)
{
if(max<*(a+i))
max=*(a+i);
if(min>*(a+i))
min=*(a+i);
}
printf("max is:%d\n",max);
printf("min is:%d",min);
}
/*WAP that passes array to a function and print the largest and smallest element */
//without function
#include<stdio.h>
#include<conio.h>
int main()
{
int i,a[20],n,max,min;
printf("Enter elements number:");
scanf("%d",&n);
printf("Enter elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
min=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
printf("max is:%d\n",max);
printf("min is:%d",min);
getch();
}
/*WAP that passes array to a function and returns the sum and difference of the largest and
smallest elements
of the array to the main function*/
//-> to return more than one value form function use pointer ,here sum and difference must be
return
#include<stdio.h>
#include<conio.h>
void sumdiff(int [],int *,int *,int);
int main()
{
int i,a[20],n,sum,diff;
printf("Enter elements number:");
scanf("%d",&n);
printf("Enter elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sumdiff(a,&sum,&diff,n);
printf("sum is:%d\n",sum);
printf("diff is:%d",diff);
getch();
}
void sumdiff(int a[],int *s,int *d,int n)
{
int max=a[0],min=a[0],i;
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
*s=max+min;
*d=max-min;
}
/*program for ascending order.both in array notation and pointer notation
comments are for array notation .you can do any one notation if not specified in
question ->element in n-2 position is second largest in ascending order
->element in 1 position is second smallest in ascending order */
#include<stdio.h>
#include<conio.h>
//void sort(int []);
//void readArray(int []);
//void displayArray(int []);
void sort(int *);
void readArray(int *);
void displayArray(int *);
int i,n;
int main()
{
int j,p[10];
int *nums;
nums=p;
printf("How many numbers are there:");
scanf("%d",&n);
printf("Enter %d numbers:",n);
readArray(nums);
sort(nums);
displayArray(nums);
printf("second largest is:%d",nums[n-2]);
printf("second smallest is:%d",nums[1]);
getch();
return 0;
}
//void sort(int num[])
void sort(int *num)
{
int j,temp;
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
{ if(*(num+i)>*(num+j)) //simply change > to < for descending order /*
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp
}*/
{temp=*(num+i);
*(num+i)=*(num+j);
*(num+j)=temp;
}
}
}
//void readArray(int nums[])
void readArray(int *nums)
{
for(i=0;i<n;i++)
scanf("%d",nums+i); //scanf("%d",&nums[i]);
}
void displayArray(int *nums)
{
for(i=0;i<n;i++)
printf("%d " ,*(nums+i));
}
/* WAP to multiply two matrix. Use user defined function to read matrix elements,process it and
display result.*/
#include<stdio.h>
#include<conio.h>
void read_matrix(float [][10],int ,int );
void display_matrix(float [][10],int ,int );
void mul_matrix(float [][10],float [][10],float [][10],int,int,int);
int main()
{
int k,l,m,n;
float a[10][10],b[10][10],pro[10][10];
printf("Enter first matrix order:(<10)");
scanf("%d%d",&m,&n);
printf("Enter order of second matrix:(<10):");
scanf("%d%d",&k,&l);
if(n!=k)
{
printf("Cannot multiplied");
}
else
{
printf("Enter first matrix elements:");
read_matrix(a,m,n);/* passing array and size of first matrix m,n */
printf("Enter second matrix elementS:");
read_matrix(b,n,l);/* passing array and size of second matrix n,l */
mul_matrix(a,b,pro,m,n,l);
printf("product is:");
display_matrix(pro,m,l);
}
getch();
return 0;
}
void read_matrix(float mat[][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%f",&mat[i][j]);
}
getch();
return 0;
}
#include <stdio.h>
enum week{ sunday, monday, tuesday, wednesday, thursday, friday, saturday};
int main(){
enum week today;
today=wednesday;
printf("%d day",today+1);
return 0;
}
Output
4 day
You can write any program in C language without the help of enumerations but,
enumerations helps in writing clear codes and simplify programming.
/* Create a structur student containing name,symbol number and name of 6 subjets,mark of
each subject and total mark as its members.WAP that uses this structure and reads data for a
student and gives the total marks as the output.
*/
#include<stdio.h>
#include<conio.h>
struct STU
{
char name[50];
int sym;
char sname[6][20]; /*for name of 6 subject.each subject name maximum length is 20
including null */
float marks[6];
float total;
};
int main()
{
struct STU s;
int i;
s.total=0;
printf("Enter name");
gets(s.name);
printf("Enter symbol number: ");
scanf("%d",&s.sym);
for(i=0;i<6;i++)
{
fflush(stdin);
printf("Enter subject name: ");
scanf("%s",s.sname[i]);
printf("Enter corresponding marks: ");
scanf("%f",&s.marks[i]);
s.total=s.total+s.marks[i];
}
printf("\n Total = %f",s.total);
getch();
}
/*Wap to manipulate complex numbers using structures. The structure should contain real and
imaginary part. Write separate functions to add and substract complex numbers. Display result
from main */
#include<stdio.h>
#include<conio.h>
typedef struct{
float real;
float imag;
}complex;//using typedef so complex is new data type
complex add( complex,complex); complex
sub(complex,complex);
int main()
{
complex n1,n2,ad,sb;
printf("Enter real and imaginart part of first complex
number:"); scanf("%f%f",&n1.real,&n1.imag);
printf("Enter real and imag part of second complex
number:"); scanf("%f%f",&n2.real,&n2.imag);
ad=add(n1,n2);
sb=sub(n1,n2);
printf("The sum is:%.2f+%.2fi\n",ad.real,ad.imag);
printf("The difference is:%.2f+(%.2f)i",sb.real,sb.imag);
getch();
return 0;
}
}
complex sub(complex num1, complex num2)
{
struct complex b;
b.real=num1.real-num2.real;
b.imag=num1.imag-num2.imag;
return b;
}
/*WAp to manipulate complex numbers using structure. Create function that add and
substract complex number.*/
/*-> to return more than one value from function use concept of pointer.
You can create two fucntion and return two values of sum and difference to main()
or you can create single function and display result of sum and difference from main() by using
pointer concept*/
#include<stdio.h>
#include<conio.h>
typedef struct complexnum
{
float real;
float imag;
}complex;
void sumdiff( complex,complex,complex*,complex*);
int main()
{
complex n1,n2,ad,sb;
printf("Enter real and imaginart part of first complex
number:"); scanf("%f%f",&n1.real,&n1.imag);
printf("Enter real and imag part of second complex
number:"); scanf("%f%f",&n2.real,&n2.imag);
sumdiff(n1,n2,&ad,&sb);
printf("The sum is:%.2f+%.2fi\n",ad.real,ad.imag);
printf("The difference is:%.2f+(%.2f)i",sb.real,sb.imag);
getch();
return 0;
}
void sumdiff(complex num1,complex num2,complex *s,complex *d)
{
s->real=num1.real+num2.real;
s->imag=num1.imag+num2.imag;
d->real=num1.real-num2.real;
d->imag=num1.imag-num2.imag;
}
/*Define a structure to hold the roll no. of a student and marks obtained by him in 5 subjects.
Declare an array to hold the data of 20 students.pass it to a function that displays the marks
of student who has a highest total marks */
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
float mark[5];
};
void decl(struct student[]);
int main()
{
int i,j;
struct student list[20];
/*Entering data*/
for(i=0;i<20;i++)
{
printf("Enter roll number:");
scanf("%d",&list[i].roll);
printf("Enter marks in 5 different subjects:");
for(j=0;j<5;j++)
{
scanf("%f",&list[i].mark[j]);
}
}
decl(list);
getch();
return 0;
}
scanf("%f",&list[i].mark[j]);
list[i].total+=list[i].mark[j];
}
}
decl(list);
getch();
return 0;
}
/*WAP that uses structure to read employee id,name,age and salary.Sort them on the basis of
salary in descending order.The program should display unsorted data followed by sorted data.
Pass whole structure array to function and sort data in fucntion and display result from main */
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct employee
{
int emp_id;
char name[25];
int age;
float salary;
};
void sort(struct employee [],int);
int main()
{
struct employee e[50];
int i,j,n;
printf("How many employee?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter data for employee:%d\n",i+1);
printf("Employee ID: ");
scanf("%d",&e[i].emp_id);
printf("Name: ");
fflush(stdin);
scanf("%s",e[i].name);
printf("Age: ");
scanf("%d",&e[i].age);
printf("Salary: ");
scanf("%f",&e[i].salary);
}
printf("Unsorted data On eMployee:");
for(i=0;i<n;i++)
{
printf("\n%d",e[i].emp_id);
printf("\t%s",e[i].name);
printf("\t%d",e[i].age);
printf("\t%.2f",e[i].salary);
}
}
void sort(struct employee e[],int n)
{
int i,j;
struct employee temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(e[i].salary<e[j].salary)
{
temp=e[i];//swapping whole structure
e[i]=e[j];
e[j]=temp;
}
/*WAP to compute any two instant hop distances in a format(feet:inches) using structure.
Build single function that add and substract given hop distances and display the result in the
main function */
#include<stdio.h>
#include<conio.h>
struct distan
{
int ft;
int in;
};
void sumdiff( struct distan,struct distan,struct distan*,struct distan*);
int main()
{
struct distan d1,d2,sum,diff;
printf("Enter first distance:");
scanf("%d%d",&d1.ft,&d1.in);
printf("Enter second distance:");
scanf("%d%d",&d2.ft,&d2.in);
sumdiff(d1,d2,&sum,&diff);
printf("The sum is:%d+%di\n",sum.ft,sum.in);
printf("The difference is:%d+(%.d)i",diff.ft,diff.in);
getch();
return 0;
}
void sumdiff( struct distan d1,struct distan d2,struct distan *s,struct distan *d)
{
long int t1,t2,t;
t1=d1.ft*12+d1.in;
t2=d2.ft*12+d2.in;
t=t1+t2;
s->ft=t/12;
s->in=t%12;
t=t2-t1;
d->ft=t/12;
d->in=t%12;
}
/*Create a structure time containing hour,minutes and seconds as its member using typedef.WAP
that uses this structure to input start time and stop time to a function. Create two user defined
functions which returns the sum and difference of the start time and stop time
-> 2 functions: sumfunc to sum time and diff to calculate difference */
#include<stdio.h>
#include<conio.h>
typedef struct {//using typedef in structure
int hr;
int min;
int sec; }time; //time is new user defined data type name
time sumfunc(time,time);
time diff(time,time);
int main()
{
time start,stop,sum,difference;
printf("enter start time: ");
scanf("%d%d%d",&start.hr,&start.min,&start.sec); //taking input start time. Using dot
operator for accessing structure members
printf("Enter stop time: ");
scanf("%d%d%d",&stop.hr,&stop.min,&stop.sec);// taking input stop time
sum=sumfunc(start,stop);//functions returns time data type value
difference=diff(start,stop);
printf("Sum is :%d hr %d min %d sec",sum.hr,sum.min,sum.sec);
printf("Difference is : %d hr %d min %d sec",difference.hr,difference.min,difference.sec);
getch();
return 0; }
time sumfunc(time start,time stop)
{
time sum;
long int t1,t2,t;
t1=start.hr*3600+start.min*60+start.sec;
t2=stop.hr*3600+stop.min*60+stop.sec;
t=t1+t2;
sum.hr=t/3600;
sum.min=(t%3600)/60;
sum.sec=(t%3600)%60;
return sum; // returns sum that is of struct time type
}
time diff(time start,time stop)
{
time d;
long int t1,t2,t;
t1=start.hr*3600+start.min*60+start.sec;
t2=stop.hr*3600+stop.min*60+stop.sec;
t=t2-t1;
d.hr=t/3600;
d.min=(t%3600)/60;
d.sec=(t%3600)%60;
return d;
}
/*Create a structure time containing hour,minutes and seconds as its member.WAP that uses this
structure to input start time and stop time to a function which returns the sum and difference of
the start time and stop time in the main program
-> making single function and returning sum and difference so using pointer */
#include<stdio.h>
#include<conio.h>
struct time{
int hr;
int min;
int sec;
};
void sumfunc(struct time,struct time,struct time*,struct time*);
int main()
{ struct time start,stop,sum,difference; printf("enter
start time: ");
scanf("%d%d%d",&start.hr,&start.min,&start.sec);
printf("Enter stop time: ");
scanf("%d%d%d",&stop.hr,&stop.min,&stop.sec);
sumfunc(start,stop,&sum,&difference);
printf("Sum is :%d hr %d min %d sec",sum.hr,sum.min,sum.sec);
printf("Difference is : %d hr %d min %d sec",difference.hr,difference.min,difference.sec);
getch();
return 0;}
void sumfunc(struct time start,struct time stop,struct time *s,struct time *d)
{
long int t1,t2,t;
t1=start.hr*3600+start.min*60+start.sec;
t2=stop.hr*3600+stop.min*60+stop.sec;
t=t1+t2;
s->hr=t/3600; //or (*s).hr=t/3600;
s->min=(t%3600)/60;
s->sec=(t%3600)%60;
t=t2-t1;
d->hr=t/3600;
d->min=(t%3600)/60;
d->sec=(t%3600)%60;
}
Files
/*WAP to read array of 10 numbers form user.Write it to file. Again rewind the file &
read contents of file and display only even numbers */
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *f;
int n[10],i,a[10];
f=fopen("nums","w+b");
printf("Enter 10 numbers :");
for(i=0;i<10;i++)
scanf("%d",&n[i]);
fwrite(&n,sizeof(n),1,f);//writing to file ,1 whole array n
rewind(f);
fread(&a,sizeof(a),1,f);//reading whole array from file
for(i=0;i<10;i++)
if((a[i]%2)==0)
printf("%d ",a[i]);
getch();
return 0;
}
/* WAP to open a new file ,read roll number, name,address and phone number of students
until the user says "no" after reading the data,write it to the file then display the content of the
file */
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
struct student
{
char name[20];
char address[20];
char phoneno[25];
int rollno;
}s;
int i;
char next[3];
FILE *fp;
fp=fopen("students.dat","wb");
if(fp==NULL)
{
printf("FIle Cannot be opened");
exit(1);
}
do{
fflush(stdin);
printf("\nEnter name:");
gets(s.name);
fflush(stdin);
printf("\nEnter address");
gets(s.address);
fflush(stdin);
printf("\nenter phone no");
gets(s.phoneno);
printf("\nEnter roll no");
scanf("%d",&s.rollno);
fwrite(&s,sizeof(s),1,fp);
printf("IF you donot have next data enter no \n else press any
key."); scanf("%s",next);
}while(strcmp("no",next));//takes input until user enters no
fclose(fp);
fp=fopen("students.dat","rb");
if(fp==NULL)
{
printf("\n file cannot be opened.");
exit(1);
}
while(fread(&s,sizeof(s),1,fp)==1)
{
printf("\n Name=%s \t Roll=%d \t Address = %s \t Phone =
%s",s.name,s.rollno,s.address,s.phoneno);
}
fclose(fp);
getch();
return 0;
}
/* WAP in C to read the following information of n students. Student name, student roll number
marks obtained(in 100) . Record all data in kec.txt file and program should print roll number
and name of student who have obtained greater than or equal to 40 marks. */
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int roll;
float marks;
}s;
int main()
{
int i,n;
float a;
FILE *fp;
fp=fopen("kec.txt","wb");
if(fp==NULL)
{
printf("File cannot be opened.");
}
else
{
printf("Enter number of students:");
scanf("%d",&n);
for(i=0;i<n;i++) // for n students
{
fflush(stdin);
printf("Enter name ");
gets(s.name);
printf("enter roll number:");
scanf("%d",&s.roll);
printf("Enter marks:");
scanf("%f",&a);
s.marks=a;
fwrite(&s,sizeof(s),1,fp);//reading information of 1 student and writing it to
file immediately.
fflush(stdin);
}
fclose(fp);
fp=fopen("kec.txt","rb");
while(fread(&s,sizeof(s),1,fp)==1)
{
if(s.marks>=40)
{
printf("%s\t %d \n",s.name,s.roll);
}
}
fclose(fp);
}
getch();
}
/*WAP to read name ,location and phone number of colleges until user is not satisfied and write
data to a file.If file already exist your program should add new data at the end of file. Search the
record by college name and display location and number of searched college. Use structure for
college name,location and number . USe typedef in structure.*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
char name[30];
char location[10];
double number;
}college;
int main()
{
college a;
int checkdata=0;
char ch,cname[30];
FILE *f;
f=fopen("college","a+b"); // append mode
if(f==NULL)
{
printf("file cannot be opened");
exit(1);
}
do
{
printf("Enter college name:");
gets(a.name);
fflush(stdin);
printf("Enter college location:");
gets(a.location);
printf("Enter phone number: ");
scanf("%lf",&a.number);
fflush(stdin);
fwrite(&a,sizeof(a),1,f);
printf("Enter y if you do want to enter other data otherwise enter any character to stop:");
ch=getche();
fflush(stdin);
}while(ch=='Y' || ch=='y');// user le y press garun jel samma data read
garcha rewind(f);
printf("Enter college name to search ");
gets(cname);
while(fread(&a,sizeof(a),1,f)==1)
{
if(strcmp(cname,a.name)==0)
{
printf("Location: %s and number :%.0lf ",a.location,a.number);
checkdata=1;
}
}
if(checkdata==0)
printf("Data not found:");
fclose(f);
getch();
return 0;
}
/*WAP that allows user to enter name,age sex and number of songs recorded for duet singers
using a file. The program should also have the provision for editing,searching,deleting and lisitng
the details of singer.The program should be menu driven */
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
struct singer
{
char name[20];
int age;
char sex;
int noofsongs;
};
struct singer si;
FILE *fptr,*fptrtemp;
char yes_no,singer_name[15];
char choice,fileName[30]="C:\\Dev-Cpp\\bin\\singer.txt";
int dataFound=0,recordNo=0;
fptr=fopen(fileName,"r+b");
if(fptr == NULL)
{
fptr=fopen(fileName,"w+b");
if(fptr==NULL)
{
printf("FIle cannot be created!!!");
return 0;
}
}
while(1)
{
printf("\n 1.ADD record");
printf("\n2.List record");
printf("\n3.MOdify Record.");
printf("\n4.Search record.");
printf("\n5.DElete record");
printf("\n 6.Exit");
printf("Enter your choice: ");
fflush(stdin);
choice=getchar();
printf("\n");
switch(choice)
{ case '1':
fseek(fptr,0,SEEK_END);
printf("Singer's name:\t");
fflush(stdin);
gets(si.name);
printf("\n AGe: \t");
scanf("%d",&si.age);
printf("SEx of singer: \t");
fflush(stdin);
si.sex=getchar();
printf("\nNUmber of songs: \t ");
scanf("%d",&si.noofsongs);
fwrite(&si,sizeof(si),1,fptr);
break;
case '2':
fseek(fptr,0,SEEK_SET);
printf("Name\tAge\tsex\t songs \n");
while(fread(&si,sizeof(si),1,fptr)==1)
{
printf("%s\t%d\t%c\t%d\n",si.name,si.age,si.sex,si.noofsongs);
}
break;
case '3':
printf("Enter name of singer to be modified!!!\t");
fflush(stdin);
gets(singer_name);
rewind(fptr);
dataFound=0;
recordNo=0;
while(fread(&si,sizeof(si),1,fptr)==1)
{
if(strcmp(si.name,singer_name)==0)
{
dataFound=1;
printf("old record is: \t");
printf("%s\t%d\t%c\t%d",si.name,si.age,si.sex,si.noofsongs);
printf("Enter new name,age,sex,and number of songs:");
gets(si.name);
scanf("%d",&si.age);
fflush(stdin);
si.sex=getchar();
fflush(stdin);
scanf("%d",&si.noofsongs);
fseek(fptr,sizeof(si)*(recordNo),SEEK_SET);
if(fwrite(&si,sizeof(si),1,fptr)==1)
{
printf("Modified");
break;
}
}
recordNo++;
}
if(dataFound==0)
printf("\n Matching data not found..");
break;
case '4':
dataFound=0;
printf("enter name of singer to search: ");
fflush(stdin);
gets(singer_name);
fseek(fptr,0,SEEK_SET);
while(fread(&si,sizeof(si),1,fptr)==1)
{
if(strcmp(si.name,singer_name)==0)
{
dataFound=1;
printf("%s\t%d\t%c\t%d\n",si.name, si.age,si.sex,si.noofsongs);
}
}
if(dataFound==0)
printf("NOT FOOUND");
break;
case '5':
printf("Enter name to be deleted: ");
scanf("%s",singer_name);
dataFound=0;
fptrtemp=fopen("C:\\Dev-Cpp\\bin\\temp.dat","wb");
rewind(fptr);
while(fread(&si,sizeof(si),1,fptr)==1)
{
if(strcmp(si.name,singer_name)!=0)
{
fwrite(&si,sizeof(si),1,fptrtemp);
}
else
dataFound=1;
}
fclose(fptr);
fclose(fptrtemp);
remove(fileName);
rename("C:\\Dev-Cpp\\bin\\temp.dat",fileName);
fptr=fopen(fileName,"r+b");
if(dataFound==1){
printf("DELeTed");
}
else
printf("Not found");
break;
case '6':
fclose(fptr);
getch();
return 0;
default:
printf("Invalid character!!!!!!!!!!");
}}
}
10.FORTRAN
! program to convet binay number to a decimal number
integer decimal,rem,binary
decimal=0
i=0
write(*,*)'Enter the binary number:
' read(*,*)binary
1 if(binary.eq.0) goto 2
rem=mod(binary,10)
decimal=decimal+rem*2**i
binary=binary/10
i=i+1
goto 1
2 write(*,*)'The decimal equivalent
is',decimal end
if(mod(k,j).eq.0)then
check=0
endif
2 continue
if(check.eq.1)then
write(*,*)k
end if
1 continue
pause
end
2
! program to sum 1+x+x^2+x^3
write(*,*)'Type x,n' read(*,*)x,n
i=1
sum=1
10 sum=sum+x**i
if(i.lt.n)then
i=i+1
goto 10
end if
write(*,3)sum
3 format(1x,f10.4)
pause
stop
end
! sum of cos series - fixed number of terms write(*,*)'Enter
values of x(degree) and number of terms : ' read(*,*)x,n
pi=3.1415
x=x*pi/180
term=1
sum=1
do 2 i=1,n-1
term=-term*x*x/((2*i-1)*(2*i))
sum=sum+term
2 continue
write(*,20)x,sum
20 format (1x, 'X=',F10.3 ,3x , 'COSX =',F10.3)
pause
end
!Write a fortran program to read ten integer numbers store them in array and
!arrange them in ascending order and display the sorted list.
integer A(10)
write(*,*)'Enter array elements'
read(*,*)(A(i),i=1,10)
do 100 i=1,9
do 100 j=i+1,10
if(A(i).gt.A(j))then
m=A(i)
A(i)=A(j)
A(j)=m
end if
100 continue
write(*,*)'Sorted ARRAY
' do 200 i=1,10
write(*,*)A(i)
200 continue
pause end
WAP to add 2 matrices
integer a(3,3),b(3,3),c(3,3)
write(*,*)'Enter matrix A elemets(3*3)'
read(*,*)((A(i,j),j=1,3),I=1,3)
write(*,*)'Enter matrix B elemets(3*3)'
read(*,*)((B(i,j),j=1,3),I=1,3)
do 40 i=1,3
do 40 j=1,3
c(i,j)=a(i,j)+b(i,j)
40 continue
print*,'The matrix addition is'
do 70 i=1,3
write(*,*)(c(i,j),j=1,3)
70 continue
pause
end
71
!Write a fortran program to read m*n matrix ,transpose it and display both
!the matrices
integer matrix(3,3),transpose(3,3)
print*,'Enter matrix elemets'
do 20 irow=1,3,1
do 20 icol=1,3,1
read(*,*)matrix(irow,icol)
20 continue do
40 m=1,3
do 40 n=1,3
transpose(n,m)=matrix(m,n
40 continue
print*,'The matrix you entered was'
do 60 n=1,3
write(*,*)(matrix(n,m),m=1,3)
60 continue
print*,'Transpose of entered matrix is'
do 70 n=1,3
write(*,*)(transpose(n,m),m=1,3)
70 continue
pause end
!program to multiply 2 matrices of any order enterd by user
real a(10,10),b(10,10),c(10,10)
100 write(*,*)'Order of matrix A'
read(*,*)ma,na
write(*,*)'Order of matrix B'
read(*,*)mb,nb if(na.ne.mb)
then write(*,*)'order
mismatch ' goto 100
end if
write(*,*)'Enter matrix A elements :'
read(*,*)((a(i,j),j=1,na),i=1,ma)
write(*,*)'Enter matrix B elements :'
read(*,*)((b(i,j),j=1,nb),i=1,mb)
do 10 i=1,ma do
20 j=1,nb
c(i,j)=0
do 30 k=1,na
c(i,j)=c(i,j)+a(i,k)*b(k,j)
30 continue
20 continue
10 continue do
40 i=1,ma
write(*,*) (c(i,j),j=1,nb)
40 continue
pause
stop end