0% found this document useful (0 votes)
83 views

Steam Table

The document contains C code examples demonstrating the use of functions. Some key examples include: 1. A function that reverses an integer number by passing it to a function, reversing the digits, and returning the result. 2. Functions that calculate the sum and count of numbers in an array divisible by 7 but not 5. 3. Recursive functions to print the Fibonacci series up to a given number or until a term exceeds 500. 4. Functions to calculate HCF and LCM of two numbers by passing the numbers and pointer variables. 5. A function that checks if a number within a range is prime by passing each number and returning a flag.

Uploaded by

Sudeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Steam Table

The document contains C code examples demonstrating the use of functions. Some key examples include: 1. A function that reverses an integer number by passing it to a function, reversing the digits, and returning the result. 2. Functions that calculate the sum and count of numbers in an array divisible by 7 but not 5. 3. Recursive functions to print the Fibonacci series up to a given number or until a term exceeds 500. 4. Functions to calculate HCF and LCM of two numbers by passing the numbers and pointer variables. 5. A function that checks if a number within a range is prime by passing each number and returning a flag.

Uploaded by

Sudeep
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Function programs:

/*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 in C to read a string and display it in reverse order.Use user defined


functions to count the number of characters in it and reverse it */
#include<stdio.h>
#include<conio.h>
void rev(char str[]);
int main()
{
char str[30];
printf("Enter any string");
gets(str);
rev(str);/*function call, giving argument str which is character array */
printf("Reverse string is: %s",str);
getch();
return 0;
}
void rev(char str[]) /* no need to return string since it is an array */
{
int len,i;
char t;
for(len=0;str[len]!='\0';len++);/* for loop terminate here so it gives length as it executes until
null character is found */
printf("Number of characters are: %d\n",len);
for(i=0;i<len/2;i++) /*logic to reverse characters by swapping */
{
t=str[i];
str[i]=str[len-1-i];
str[len-1-i]=t;
}
}

/* 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';
}
/* 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.
USe pointer to process string*/
// just change a[i] to *(a+i)
#include<stdio.h>
#include<conio.h>
void process(char *,char *);
int main()
{
char a[50],b[50];
printf("Enter string:");
scanf("%[^\n]",a); /*takes input until next line is entered by user */
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';
}

/*WAP to generate the following pattern using unformatted output function


#include<stdio.h>
#include<conio.h> k
int main() ok
{ wok
int i,j,k; hwok
char a[]="pulchowk"; chwok
for(i=0;i<8;i++) lchwok
{ ulchwok
for(j=1;j<8-i;j++) pulchowk*/
putchar(' '); /* single quote */
for(k=7-i;k<8;k++)
{
putchar(a[k]);
}
putchar('\n'); /*single quote for character */
}
getch();
return 0;
}
/*WAP in c to re-arrange the alphabets of a given string in alphabetical order using
user defined function. USe pointer to process tha string .Your program should
convert the string"cOmpUtEr into cEmOprtU" */
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
//void ascending(char s[40])
void ascending(char *s)
{
int i,j,n;
char t;
n=strlen(s);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
/* if(toupper(s[i])>toupper(s[j])) //using array notation
{ t=s[i];
s[i]=s[j];
s[j]=t;} */
if(toupper(*(s+i))>toupper(*(s+j))) //using pointer notation
{
t=*(s+i);
*(s+i)=*(s+j);
*(s+j)=t;
}}}}
int main()
{
char str[40];
gets(str);
ascending(str);
printf("The rearranged alphabets are: %s",str);
getch();
return 0;
}
/*Write a function that takes a string as the input and counts the number of
vowels,consonants,numeric character and other characters.The calling fucntion should read the
string and pass it to function.After the fucntion call, the count of vowels,consonants ,numeric
character and othercharacter AND other character should be displayed in the calling fucntion .*/
/* ->more than one value cannot be returned from function so using pointer will be effective*/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void count(char[],int *,int *,int *,int *);
int main()
{
char str[50];
int vc=0,cc=0,nc=0,oc=0;
printf("Enter string:");
gets(str);
count(str,&vc,&cc,&nc,&oc);
printf("vowels:%d \t consonants: %d \t numeric character:%d\t others:%d ",vc,cc,nc,oc);
getch();
return 0;
}
void count(char s[],int *v,int *c,int *n,int *o)
{
int i;
for(i=0;s[i]!='\0';i++)
{
if(toupper(s[i])=='A' || toupper(s[i])=='E'|| toupper(s[i])=='I'|| toupper(s[i])=='O'||
toupper(s[i])=='U')
*v=*v+1;
else if(toupper(s[i])>='A' && toupper(s[i])<='Z')
*c=*c+1;
else if(s[i]>='0' && s[i]<='9')
*n=*n+1;
else
*o=*o+1;
}
}
/*WAP to insert a string into another string in the location specified by the user. Read the
string in the main program and pass them to function along with the inserting position and return
the resulting string */
#include<stdio.h>
#include<conio.h>
void insert(char *,char *,int);
//void insert(char [],char [],int);
main()
{
char m[50],s[50];
int n;
printf("Enter main string: ");
gets(m);
printf("Enter string to insert :");
gets(s);

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]);
}

void display_matrix(float mat[][10],int m,int n)


{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf(" %.4f ",mat[i][j]);
printf("\n");
}
}
void mul_matrix(float a[][10],float b[][10],float pro[][10],int m,int n,int l)
{ /*product of two matrix calculation starts
*/ int i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<l;j++)
{
pro[i][j]=0;
for(k=0;k<n;k++)
pro[i][j]+=a[i][k]*b[k][j];
}
}/* product calculation ends here */
}
/* WAP to multiply two matrix without using user defined function.i.e. read from
main,process in main and display from main */
/*compare and know the difference in with & without function*/
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,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 be multiplied");
}
else
{
printf("Enter first matrix elements:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
printf("Enter second matrix elementS:");
for(i=0;i<k;i++)
for(j=0;j<l;j++)
scanf("%f",&b[i][j]);
for(i=0;i<m;i++) /* product calculation starts here */
{
for(j=0;j<l;j++)
{
pro[i][j]=0;
for(k=0;k<n;k++)
pro[i][j]+=a[i][k]*b[k][j];
}
}/* product calculation ends here */
printf("product is:");
for(i=0;i<m;i++) /* To display matrix */
{
for(j=0;j<l;j++)
printf(" %.4f ",pro[i][j]);
printf("\n");
}
}

getch();
return 0;
}

/* Program to calculate transpose of a matrix */


#include<stdio.h>
#include<conio.h>
int main()
{
int a[10][10],b[10][10],i,j,m,n;
printf("Enter row and column of matrix:");
scanf("%d%d",&m,&n);
printf("Enter elements of A:");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
//rows and columns are interchanged:transpose
for(i=0;i<n;i++)
for(j=0;j<m;j++)
b[i][j]=a[j][i];

printf("Transpose matrix is:");


for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
getch();
}
/* program to find norm and trace of 3*3 matrix using separate function
trace and norm and display result in main */
#include<stdio.h>
#include<conio.h>
#include<math.h>
void read_matrix(float [][3]);
float trace(float [][3]);
float norm(float [][3]);
int main()
{
float a[3][3],t,n;
printf("Enter matrix elements:");
read_matrix(a);
t=trace(a);
n=norm(a);
printf("trace: %f and norm: %f ",t,n);
getch();
return 0;
}
void read_matrix(float a[][3])
{ int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%f",&a[i][j]);
}
float norm(float a[][3])
{
int i,j;
float n=0.0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
n=n+a[i][j]*a[i][j];
return(sqrt(n));
}
float trace(float a[][3])
{
int i,j;
float t=0.0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ t+=a[i][i];
}
}
return t;
}
Structure:
Enumeration
Enumerated Types are a special way of creating your own Type in C. The type is a "list of key
words".An enumeration is a user-defined data type consists of integral constants and each
integral constant is give a name. Keyword enum is used to defined enumerated data type.

enum type_name{ value1, value2,...,valueN };/*semicolon here */

Here, type_name is the name of enumerated data type or tag.


And value1, value2,....,valueN are values of type type_name.
By default, value1 will be equal to 0, value2 will be 1 and so on but, the programmer can change
the default value.
Example of enumerated type

#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 add(complex num1,complex num2)


{
struct complex a;
a.real=num1.real+num2.real;
a.imag=num1.imag+num2.imag;
return a;

}
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;
}

void decl(struct student list[])


{
float total[20],max;
struct student s;
int i,j;
for(i=0;i<20;i++) /*20 students ko lagi. */
{
total[i]=0;
for(j=0;j<5;j++)/*each student ko 5 subject */
{
total[i]=total[i]+list[i].mark[j];
}
}
max=total[0];
s=list[0];
for(i=1;i<20;i++)
{
if(total[i]>max)
{
max=total[i];
s=list[i];}
}
printf("Roll no:%d has highest total.\n",s.roll);
printf("Marks of student having highest total:");
for(i=0;i<5;i++)
printf("%.2f\t",s.mark[i]);
}
/*Define a structure to hold the roll no. of a student ,marks obtained by him in 5 subjects and
total of 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];
float total;
};
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:");
list[i].total=0;
for(j=0;j<5;j++)
{

scanf("%f",&list[i].mark[j]);
list[i].total+=list[i].mark[j];
}
}
decl(list);
getch();
return 0;
}

void decl(struct student list[])


{
int i;
float tot=list[0].total;
struct student max;
max=list[0];

for(i=1;i<20;i++) /*20 students ko lagi. */


{
44 Prepared by: Er. Prakash Kafle
if(list[i].total>tot)
{
tot=list[i].total;
max=list[i];
}
}
printf("Roll no:%d has highest total.\n",max.roll);
printf("Marks of student having highest total:");
for(i=0;i<5;i++)
printf("%.2f\t",max.mark[i]);

/*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);
}

sort(e,n); //function call


printf("\nSorted data is:");
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);
}
getch();
return 0;

}
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;
}

/* if(strcmp(e[i].name,e[j].name)>0) //for sorting in alphabetical order


{
temp=e[i];
e[i]=e[j];
e[j]=temp;
} */
}
}
}
/*WAP in C to read a set of names,roll number ,date of birth and date of admission of the
students in the college form the keyboardwhere the date of birth and date of admission consist of
three members such as day ,month and year as a separate structure and sort them in ascending
order of date of birth. */
#include<stdio.h>
#include<conio.h>
struct date{
int day,month,year;
};
struct student
{
char name[10];
int rollno;
struct date dob,doa;
};
int main()
{
struct student s[20],temp;
int i,j,n;
printf("How many students:");
scanf("%d",&n);
/*Read n data */
for(i=0;i<n;i++)
{
printf("Enter data of student:%d",i+1);
printf("\nName: ");
scanf("%s",s[i].name);
printf("Roll number:");
scanf("%d",&s[i].rollno);
printf("Enter DOB (day month yr): ");
scanf("%d%d%d",&s[i].dob.day,&s[i].dob.month,&s[i].dob.year);
printf("Enter date of admission(day month yr):");
scanf("%d%d%d",&s[i].doa.day,&s[i].doa.month,&s[i].doa.year);
}
/* sorting data */
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{ /*if one year is greater than other then months and days need not be compared. If one
month is greater than
other when years are equal then days not need to be compared. Days are to be compared
when both year and month are equal */
if(s[i].dob.year>s[j].dob.year || (s[i].dob.year == s[j].dob.year
&& s[i].dob.month>s[j].dob.month)||
(s[i].dob.year==s[j].dob.year && s[i].dob.month==s[j].dob.month
&& s[i].dob.day>s[j].dob.day))
{
temp=s[i];//swapping whole structure
s[i]=s[j];
s[j]=temp;
}
}
}
/* displaying data */
printf("Sorted data of students:");
for(i=0;i<n;i++)
{
printf("\n%s",s[i].name);
printf("\t%d",s[i].rollno);
printf("\t%d/%d/%d",s[i].dob.day,s[i].dob.month,s[i].dob.year);
printf("\t%d/%d/%d",s[i].doa.day,s[i].doa.month,s[i].doa.year);
}
getch();
return 0;
}
/*WAP to compute any two instant hop distances in a format(feet:inches) using structure. Build
functions to add and substract given hop distances and display the results in the main function */
/*(This program mainly illustrates the concept of returning structures form called function
to calling function)*/
#include<stdio.h>
#include<conio.h>
struct distan
{
int ft;
int in;
};

struct distan sumd( struct distan,struct distan);


struct distan diffd( 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);
sum=sumd(d1,d2);
diff=diffd(d1,d2);
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;
}

struct distan sumd( struct distan d1,struct distan d2)


{
struct distan s;
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;
return s; }
struct distan diffd( struct distan d1,struct distan d2)
{
struct distan d;
long int t1,t2,t;
t1=d1.ft*12+d1.in;
t2=d2.ft*12+d2.in;
t=t2-t1;
d.ft=t/12;
d.in=t%12;
return d; }

/*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

program to solve quadratic equation


real r1,r2,d,rp,ip,a,c,b
write(*,*)'Type a,b,c'
read(*,*)a,b,c
d=b*b-4*a*c
if(d) 10,20,30
10 rp=-b/(2*a)
ip=sqrt(abs(d))/(2.0*a)
write(*,*)'imaginary roots are:',rp,'+i',ip,'and',rp,'-i',ip
pause
stop !goto 100
20 r1=-b/(2.0*a)
write(*,*)'real andd
equal',r1,r1 pause
goto 100 !stop
30 r1=(-b+sqrt(d))/(2*a)
r2=(-b-sqrt(d))/(2*a)
write(*,*)'Roots are real and unequal',r1,r2
100pause
stop
end
!program to find HCF of 2 numbers
integer a,b,i,hcf
write(*,*)'Enter two numbers'
read(*,*)a,b
do 100 i=1,a,1
if(mod(a,i).eq.0 .and. mod(b,i).eq.0)then
hcf=i
endif
100 continue
write(*,*)'HCF is',hcf
pause
end
!Write a program in fortran to evaluate the following series: \
! 1/1^2+1/2^2+1/3^2+...... + 1/n^2
write(*,*)'enter number of terms'
read(*,*)n
sum=0
do 100 i=1,n
sum=sum+1.0/(i**2)
100 continue
write(*,*)'sum= ',sum
pause
end

!program to display prime number within the range specified by the


user integer f,s,k,check
write(*,*)'enter first and second number'
read(*,*)f,s
if(f.lt.2)then
f=2
endif
do 1 k=f,s,1
check=1
do 2 j=2,k/2,1

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 display the greatest and


! smallest number from the list of tem
elements integer a(10),g,s
write(*,*)'Enter ten numbers '
read(*,*)(a(i),i=1,10) g=a(1)
s=a(1)
do 111 i=2,10
if(g.lt.a(i)) g=a(i)
if(s.gt.a(i)) s=a(i)
111 continue
write(*,*)'Greatest number is',g
write(*,*)'Smallest number is',s
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

You might also like