Homework-1 : ( असार Week-3 ) Due Date : 2 July
- What is function? Write advanatges of function.
- What are the types of function? Explain.
- Explain the components of function.
- What is recursion? Explain with one example.
- Differentiate between library and user defined function.
- Differentiate between local and glolabl variable with examples.
Homework-2 : ( असार Week-4 ) Due Date : 9 July
- Write C program to find simple interest using user defined function(UDF).
- Write C program to print the sum of two numbers using function.
- Write C program to print square of given number using UDF.
- Write C program to print the greater number between two numbers using function.
- Write C program to print the smaller number between two numbers using function.
- Explan the automatic,external,static and register storage class with example.
#include<stdio.h>
void si(int p, int t, int r);
main()
{
int p,t,r;
printf("Enter principal , time and rate : ");
scanf("%d%d%d",&p,&t,&r);
si(p,t,r);
}
void si(int p, int t ,int r)
{
int ans;
ans=(p*t*r)/100;
printf("Simple Interest is %d",ans);
}
#include<stdio.h>
void sum(int a, int b);
main()
{
int a,b;
printf("Enter two numbers : ");
scanf("%d%d",&a,&b);
sum(a,b);
}
void sum(int a ,int b)
{
int ans;
ans=a+b;
printf(" %d + %d = %d",a,b,ans);
}
#include<stdio.h>
void sq(int n);
main()
{
int n;
printf("Enter a number : ");
scanf("%d",&n);
sq(n);
}
void sq(int n)
{
int ans;
ans=n*n;
printf(" Square of %d = %d",n,ans);
}
#include<stdio.h>
void greater(int a, int b);
main()
{
int a,b;
printf("Enter two numbers : ");
scanf("%d%d",&a,&b);
greater(a,b);
}
void greater(int a ,int b)
{
if(a>b)
printf(" %d is greater ",a);
else
printf(" %d is greater ",b);
}
#include<stdio.h>
void greater(int a, int b);
main()
{
int a,b;
printf("Enter two numbers : ");
scanf("%d%d",&a,&b);
greater(a,b);
}
void greater(int a ,int b)
{
if(a<b)
printf(" %d is smaller ",a);
else
printf(" %d is smaller ",b);
}
(i) Automatic storage class: The keyword auto is used to define automatic variable. Variables declared within function bodies are automatic by default. When a block is entered, the system allocates memory for the automatic variables. Within that block, these variables are defined and are considered "local" to the block. So the value of these variables is lost. If the block is re-entered, the system re-allocates memory but the previous values are unknown.
Example of Automatic Storage Class
#include<stdio.h>
void show();
main()
{
show();
show();
show();
}
void show()
{
auto int n=5;
printf("\n Number %d",n);
n=n+5;
}
Output is
Number 5
Number 5
Number 5
(ii) External storage class: The keyword extern is used to define external type variable. The external storage class is used to transmit information across the blocks and functions. When a variable is declared outside function storage is permanently assigned. Such a variable is considered to be global to all functions declared after it.
Example of External Storage Class
#include<stdio.h>
extern int a=1,b=2;
int show();
main()
{
int k;
k=show();
printf("\n %d ",k);
printf("\n a=%d \t b=%d",a,b);
}
int show()
{
int a,b;
a=b=4;
return(a+b);
}
Output is
8
a=1 b=2
(iii) Static storage class: The keyword static is used to define static type variable. A value is said to be static if it is allocated storage at the beginning of the segment and the storage remains allocated until the program execution terminates.
Example of Static Storage Class
#include<stdio.h>
void show();
main()
{
show();
show();
show();
}
void show()
{
static int n=5;
printf("\n Number %d",n);
n=n+5;
}
Output is
Number 5
Number 10
Number 15
(iv) Register storage class: The keyword register is used to define register type variable. The use of the register storage class is an attempt to improve execution speed. It is used only for frequently used variables only(that is limited either two or three).
Example of Register Storage Class
#include<stdio.h> void show(); main() { show(); } void show() { register int n=5,i; for(i=1;i<=5;i++) { printf("\n Number %d ",n); n=n+5; } } Output is Number 5 Number 10 Number 15 Number 20 Number 25
#include<stdio.h> void show(); main() { show(); } void show() { register int n=5,i; for(i=1;i<=5;i++) { printf("\n Number %d ",n); n=n+5; } } Output is Number 5 Number 10 Number 15 Number 20 Number 25
Homework-3 : ( असार Week-5 ) Due Date : 16 July
- What is recursion. Write C program to find factorial of given number using recursive function.
- Write C program to dislay 0,1,1,2,3,5 ... upto 10th terms using recursive function.
- Using recursive function , display first ten natural numbers.
- Using recursive function, calculate sum of n natural numbers.
- Write C program to input the weight of five students and count the number of students having weight in between 50 to 60 kg using function.
Recursion is a process by which a function calls itself repeatedly, until some specific condition has been satisfied. For the problem to solve recursively two conditions must be satisfied:
(a) The function should call itself.
(b) The problem statement must include a stopping point.
#include<stdio.h>
int fact(int);
main()
{
int n,ans;
printf( "Enter any number : ");
scanf("%d",&n);
ans=fact(n);
printf(" Factorial of %d is %d ",n,ans);
}
int fact(int n)
{
if(n<=1)
return (1);
else
return(n*fact(n-1));
}
#include<stdio.h>
int fibo(int n);
main()
{
int i,n=10;
for(i=0;i<10;i++)
{
printf(" %d\t ",fibo(i));
}
}
int fibo(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return (fibo(n-1)+fibo(n-2));
}
#include<stdio.h>
void series(int n);
main()
{
int n=1;
series(n);
}
void series(int n)
{
if(n<=10)
{
printf("%d\n",n);
series(n+1);
}
}
#include<stdio.h>
int sum(int);
main()
{
int n,ans;
printf( "Enter the value of n : ");
scanf("%d",&n);
ans=sum(n);
printf(" Sum is %d ",ans);
}
int sum(int n)
{
if(n<=0)
return 0;
else
return(n+sum(n-1));
}
#include<stdio.h>
int count(int w[5]);
main()
{
int w[5],i;
for(i=0;i<5;i++)
{
printf(" Enter w[%d] : ",i+1);
scanf("%d",&w[i]);
}
printf(" The number of students having weights between 50 to 60 kg is %d ",count(w));
}
int count(int w[])
{
int i,c=0;
for(i=0;i<5;i++)
{
if(w[i]>=50 && w[i]<=60)
c++;
}
return(c);
}
Homework-4: ( श्रावण Week-1 ) Due Date : 23 July
- Write a program to find the greatest number among given set of 'n' numbers using function.
- Write C program to input the weight of 100 students and count the number of students having weight in between 50 to 60 kg using function.
- Write C program that inputs 'n' numbers and sort them in ascending order using function.
- Write C program that takes 'n' number of strings and sort them in alphabetical order using function.
- Write C program to enter elements for 2 x 2 matrix and display its transpose using function.
- Write C program to calculate and display sum of two matrices with respective size 3x3 using function.
#include<stdio.h>
int max(int a[],int n);
main()
{
int a[100],n,i;
printf(" Enter vaule of n : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter a[%d] : ",i+1);
scanf("%d",&a[i]);
}
printf(" The greatest number is %d ",max(a,n));
}
int max(int a[],int n)
{
int i,m=a[0];
for(i=0;i<n;i++)
{
if(a[i]>m)
m=a[i];
}
return(m);
}
#include<stdio.h>
int count(int w[]);
main()
{
int w[100],i;
for(i=0;i<100;i++)
{
printf(" Enter w[%d] : ",i+1);
scanf("%d",&w[i]);
}
printf(" The number of students having weights between 50 to 60 kg is %d ",count(w));
}
int count(int w[])
{
int i,c=0;
for(i=0;i<100;i++)
{
if(w[i]>=50 && w[i]<=60)
c++;
}
return(c);
}
#include<stdio.h>
void sort(int num[],int n);
main()
{
int num[100],n,i;
printf(" Enter the array size not more than 100 : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter num[%d] : ", i+1);
scanf("%d",&num[i]);
}
sort(num,n);
}
void sort(int num[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf(" Numbers in ascending order \n ");
for(i=0;i<n;i++)
printf(" %d\t ",num[i]);
}
#include<stdio.h>
#include<string.h>
void sort(char str[][20],int n);
main()
{
char str[50][20];
int i,n;
printf(" How many strings : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf(" Enter str[%d] : ",i+1);
scanf("%s",str[i]);
}
sort(str,n);
}
void sort( char str[][20],int n)
{
int i,j;
char temp[20];
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
printf(" Sorted strings are : \n");
for(i=0;i<n;i++)
printf(" %s\n ",str[i]);
}
#include<stdio.h>
void transpose(int a[][2]);
main()
{
int a[2][2],i,j;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("Enter a[%d][%d] : ",i,j);
scanf("%d",&a[i][j]);
}
}
transpose(a);
}
void transpose(int a[][2])
{
int i,j;
printf("\n Transpose of 2x2 matrix: \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf(" %d\t ",a[j][i]);
}
printf("\n");
}
}
#include<stdio.h>
void sum(int m1[][3],int m2[][3]);
main()
{
int m1[3][3],m2[3][3],i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter m1[%d][%d] : ",i,j);
scanf("%d",&m1[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter m2[%d][%d] : ",i,j);
scanf("%d",&m2[i][j]);
}
}
sum(m1,m2);
}
void sum(int m1[][3],int m2[][3])
{
int m3[3][3],i,j;
printf("\n sum of two 3x3 matrices: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
m3[i][j]=m1[i][j]+m2[i][j];
printf(" %d\t ",m3[i][j]);
}
printf("\n");
}
}
Homework-5: ( श्रावण Week-2 ) Due Date : 30 July
Book page : 211
A. Short Answer Questions
1. What is structure?
2. How do you access any member from structure?
3. Define union.
B. Long Answer Questions:
1. Explain structure and union with respective examples.
2. Differentiate between structure and union.
3. Differentiate between structure and array.
4. Is it possible to pass structure variable to a function? Explain with suitable example.
5. What is nested structure? Explain with suitable example.
6. "Structure is called user defined data type". Justify your answer with example.