Beruflich Dokumente
Kultur Dokumente
Aim:
To write a program to find divisor or factorial of a given number.
Theory:
Flowchart:
Program code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++)
f=f*i;
printf("Factorial of %d is: %d",num,f);
return 0;
}
Output:
Viva Questions:
Q1: What if the user inputs a negative number?
Ans:
Theory:
Flowchart:
Program code:
#include <stdio.h>
#include <stdlib.h>
void towers(int, char, char, char);
int main()
{ int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("The sequence of moves involved in the Tower of Hanoi are :\n");
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char firstpeg, char thirdpeg, char secondpeg)
{ if (num == 1)
{ printf("\n Move disk 1 from peg %c to peg %c", firstpeg, thirdpeg);
return;
}
towers(num - 1, firstpeg, secondpeg, thirdpeg);
printf("\n Move disk %d from peg %c to peg %c", num, firstpeg, thirdpeg);
towers(num - 1, secondpeg, thirdpeg, firstpeg);
}
Output:
Viva Questions:
Q1: What is the advantage of using functions?
Ans:
Q4: How many number of moves are doneto move all disks
from A to B?
Ans:
Q5: Why we have used int instead of void in main?
Ans:
Experiment No-3 Date:
Aim:
Write a program to find a sum of geometric series.
Theory:
Algorithm/Flowchart:
Program code:
#include <stdio.h>
#include <math.h>
int main()
{ float a, r, i, last_term, sum = 0;
int n;
printf("Enter the first term of the G.P. series: ");
scanf("%f", &a);
printf("Enter the total numbers in the G.P. series: ");
scanf("%d", &n);
printf("Enter the common ratio of G.P. series: ");
scanf("%f", &r);
sum = (a *(1 - pow(r, n + 1))) / (1 - r);
last_term = a * pow(r, n - 1);
printf("last_term term of G.P.: %f", last_term);
printf("\n Sum of the G.P.: %f", sum);
return 0;}
Output:
Viva questions:
Aim:
Write a recursive program to print the first m Fibonacci number.
Theory:
Algorithm/Flowchart:
Program code:
#include <stdio.h>
void main()
{int fib1 = 0, fib2 = 1, fib3, num, count = 0;
printf("Enter the value of num \n");
scanf("%d", &num);
printf("First %d FIBONACCI numbers are ...\n", num);
printf("%d\n", fib1);
printf("%d\n", fib2);
count = 2; /* fib1 and fib2 are already used */
while (count < num)
{
fib3 = fib1 + fib2;
count++;
printf("%d\n", fib3);
fib1 = fib2;
fib2 = fib3;
}
}
Output:
Viva questions:
Q4:What is recurrsion ?
Ans:
Experiment No-5 Date:
Aim:
Write a menu driven program for matrices to do following operation
depending on
: addition of two matrices
: subtraction of two matrices
: transpose
: product of matrices
Theory:
Algorithm/Flowchart:
Program code:
#include<stdio.h>
#include<conio>
void main()
{
int I,j,r1,r2,c1,c2,a[20[20],b[20[20],c[20][20],k,s,ch;
char con;
do
{
clrscr();
printf(" Enter 1 for Matrix addition\n 2 for Matrix multiplication\n 3 for Transpose of a
matrix\n 4 for Sum of diagonals of a matrix");
printf("Enter your choice");
scanf("%d",&ch)
switch(ch)
{
case 1: printf("Enter the order of the first matrix");
scanf("%d%d",&r1,&c1);
printf("Enter the order of the second matrix");
scanf("%d%d",&r2,&c2);
if(r1==r2&&c1==c2)
{
printf("Enter the elements in to the first matrix");
for(i=0;ifor(j=0;jscanf("%d",&a[i][j]);
printf("Enter the elements in to the second matrix");
for(i=0;ifor(j=0;jscanf("%d",&b[i][j]);
printf("Entered first matrix");
for(i=0;i{
for(j=0;j{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Entered second matrix");
for(i=0;i{
for(j=0;j{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0;i{
for(j=0;j{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Sum:\n");
for(i=0;i{
for(j=0;j{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("Addition is not possible");
break;
case 2: printf("Enter the order of the first matrix");
scanf("%d%d",&r1,&c1);
printf("Enter the order of the second matrix");
scanf("%d%d",&r2,&c2);
if(c1==r2)
{
printf("Enter the elements in to the first matrix");
for(i=0;ifor(j=0;jscanf("%d",&a[i][j]);
printf("Enter the elements in to the second matrix");
for(i=0;ifor(j=0;jscanf("%d",&b[i][j]);
printf("Entered first matrix");
for(i=0;i{
for(j=0;j{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Entered second matrix");
for(i=0;i{
for(j=0;j{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("Product:\n");
for(i=0;i{
for(j=0;j{
c[i][j]==0;
for(k=0;k{
c[i][j]= c[i][j]+a[i][k]*b[k][j];
}
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("Multiplication is not possible");
break;
case 3: printf("Enter the order of the matrix");
scanf("%d%d",&r1,&c1);
printf("Enter the elements in to the matrix");
for(i=0;ifor(j=0;jscanf("%d",&a[i][j]);
printf("Entered matrix");
for(i=0;i{
for(j=0;j{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Transpose:\n");
for(i=0;i{
for(j=0;j{
printf("%d\t",a[i][j]);
}
printf("\n");
}
break;
case 4: printf("Enter the order of the matrix");
scanf("%d%d",&r1,&c1);
if(r1==c1)
{
printf("Enter the elements in to the matrix");
for(i=0;ifor(j=0;jscanf("%d",&a[i][j]);
printf("Entered matrix");
for(i=0;i{
for(j=0;j{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("Sum=%d",s);
}
else
printf("Not a square matrix");
break;
default: printf("Inavalid choice");
}
printf("Do you want to continue(y/n)";
scanf("%c",&con);
}
while(con=='y'||con=='Y');
getch();
}
Output:
Viva questions:
Aim:
To write a program to copy one file to other, use command line arguments.
Theory:
Algorithm/Flowchart:
Program code:
#include<stdio.h>
int main(int argc,char *argv[])
{
FILE *fs,*ft;
int ch;
if(argc!=3)
{
printf("Invalide numbers of arguments.");
return 1;
}
fs=fopen(argv[1],"r");
if(fs==NULL)
{
printf("Can't find the source file.");
return 1;
}
ft=fopen(argv[2],"w");
if(ft==NULL)
{
printf("Can't open target file.");
fclose(fs);
return 1;
}
while(1)
{
ch=fgetc(fs);
if (feof(fs)) break;
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
return 0;
}
Output:
Experiment No-7 Date:
Aim:
An array of of record contains information of managers and workers of a
company. Print all the data of managers and workers in separate files.
Theory:
Algorithm/Flowchart:
Program code:
#include<stdio.h>
#include<conio.h>
struct employee
{
char name[22];
char city[20];
int age;
int salary;
};
void main()
{
struct employee emp[5];
FILE *fp;
int i;
int max;
clrscr();
printf("ENTER THE FOLLOWING INFORMATION OF 5 EMPLOYEES\n\n");
for(i=0;i<5;i++)
{
printf("NAME:");
scanf("%s",&emp[i].name);
printf("City:");
scanf("%s",&emp[i].city);
printf("Age:");
scanf("%d",&emp[i].age);
printf("Salary:");
scanf("%d",&emp[i].salary);
}
fp=fopen("employee.dat","r");
for(i=0;i<5;i++)
{
fprintf(fp,"%s\n",emp[i].name);
fprintf(fp,"%s\n",emp[i].city);
fprintf(fp,"%d\n",emp[i].age);
fprintf(fp,"%d\n",emp[i].salary);
}
fclose(fp);
fp=fopen("employee.dat","r");
for(i=0;i<5;i++)
{
fscanf(fp,"%s",&emp[i].name);
fscanf(fp,"%s",&emp[i].city);
fscanf(fp,"%d",&emp[i].age);
fscanf(fp,"%d",&emp[i].salary);
}
//find out the maximum salary
max=emp[0].salary;//consider first emp salary is highest
for(i=0;i<5;i++)
{
if(emp[i].salary>=max)
max=emp[i].salary;
}
printf("\n\n MAXIMUM SALARY PAID TO EMPLOYEE:\n\n");
//Print employees details whose salary is highest
for(i=0;i<5;i++)
if(emp[i].salary==max)
{
printf("\nNAME:%s",emp[i].name);
printf("\nCITY:%s",emp[i].city);
printf("\nAGE:%d",emp[i].age);
printf("\nSALARY:%d",emp[i].salary);
}
getch();
}
Output:
Viva Questions:
Q1: How are structure passing and returning implemented?
Ans:
Aim:
Write a program to perform the following operators on strings without
using string functions-To find the length of string.
Theory:
Algorithm/Flowchart:
Program code:
#include <stdio.h>
int main()
{
char s[1000], i;
Output:
Experiment No-8(b) Date:
Aim:
rite a program to perform the following operators on strings without
W
using string functions-To concatenate two string.
Theory:
Algorithm/Flowchart:
Program code:
#include<stdio.h>
void main(void)
{
char str1[25],str2[25];
int i=0,j=0;
printf("\nEnter First String:");
gets(str1);
printf("\nEnter Second String:");
gets(str2);
while(str1[i]!='\0')
i++;
while(str2[j]!='\0')
{
str1[i]=str2[j];
j++;
i++;
}
str1[i]='\0';
printf("\nConcatenated String is %s",str1);
}
Output:
Experiment No-8(c) Date:
Aim:
Write a program to perform the following operators on strings without
using string functions-To find Reverse of a string.
Theory:
Algorithm/Flowchart:
Program code:
#include<stdio.h>
#include<string.h>
int main() {
char str[100], temp;
int i, j = 0;
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
Output:
Experiment No-8(d) Date:
Aim:
Write a program to perform the following operators on strings without
using string functions-To copy one string to another string.
Theory:
Algorithm/Flowchart:
Program code:
#include<stdio.h>
int main() {
char s1[100], s2[100];
int i;
i = 0;
while (s1[i] != '\0') {
s2[i] = s1[i];
i++;
}
s2[i] = '\0';
printf("\nCopied String is %s ", s2);
return (0);
}
Output:
Viva Questions:
Q1: What is difference between string and character?
Ans:
Aim:
Write a Program to store records of an student in student file. The data
must be stored using Binary File.Read the record stored in “Student.txt” file
in Binary code.Edit the record stored in Binary File.Append a record in the
Student file.
Theory:
Algorithm/Flowchart:
Program code:
#include<stdio.h>
int main()
{
char text[100];
int i;
int countL,countU,countS;
for(i=0;text[i]!='\0';i++)
{
//check for alphabet
if((text[i]>='A' && text[i]<='Z') || (text[i]>='a' && text[i]<='z'))
{
if((text[i]>='A' && text[i]<='Z'))
{
//it is upper case alphabet
countU++;
}
else
{
//it is lower case character
countL++;
}
}
else
{
//character is not an alphabet
countS++; //it is special character
}
}
//print values
printf("\nUpper case characters: [ %d ]\n",countU);
printf("\nLower case characters: [ %d ]\n",countL);
printf("\nSpecial characters: [ %d ]\n",countS);
return 0;
}
Output:
Viva Questions:
Q1: What is difference between string and character?
Ans:
Aim:
Write a program to count lowercase, uppercase letters and special
character present in the file.
Theory:
Algorithm/Flowchart:
Program code:
#include<stdio.h>
int main()
{
char text[100];
int i;
int countL,countU,countS;
for(i=0;text[i]!='\0';i++)
{
//check for alphabet
if((text[i]>='A' && text[i]<='Z') || (text[i]>='a' && text[i]<='z'))
{
if((text[i]>='A' && text[i]<='Z'))
{
//it is upper case alphabet
countU++;
}
else
{
//it is lower case character
countL++;
}
}
else
{
//character is not an alphabet
countS++; //it is special character
}
}
//print values
printf("\nUpper case characters: [ %d ]\n",countU);
printf("\nLower case characters: [ %d ]\n",countL);
printf("\nSpecial characters: [ %d ]\n",countS);
return 0;
}
Output:
Viva Questions:
Q1: How are structure passing and returning implemented?
Ans: