0% found this document useful (0 votes)
28 views18 pages

Prac Pgms Alt-1

Uploaded by

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

Prac Pgms Alt-1

Uploaded by

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

1// C Program to Print Prime Numbers upto a given Number

#include<stdio.h>

int main()

int num,i,count=0;

printf("Enter a number: ");

scanf("%d",&num);

for(i=2;i<=num/2;i++)

if(num%i==0)

count++;

break;

if(count==0 &&num!= 1)

printf("%d is a prime number",num);

else

printf("%d is not a prime number",num);

return 0;

output:

Enter a number: 5

5 is a prime number
2.Decimal to Binary Conversion Algorithm

#include<stdio.h>
int main()
{
long int decimalNumber,remainder,quotient;
int binaryNumber[100],i=1,j;
printf("Enter any decimal number: ");
scanf("%ld",&decimalNumber);
quotient = decimalNumber;
while(quotient!=0){
binaryNumber[i++]= quotient % 2;
quotient = quotient / 2;
}
printf("Equivalent binary value of decimal number %d: ",decimalNumber);
for(j = i -1 ;j> 0;j--)
printf("%d",binaryNumber[j]);
return 0;
}

output:

Enter any decimal number: 50


Equivalent binary value of decimal number 50: 110010

3.C program to accept N numbers and arrange them in an ascending order


#include <stdio.h>
void main ()
{
inti,j,a,n,number[30];
printf("Enter the value of N\n");
scanf("%d",&n);
printf("Enter the numbers \n");
for(i=0; i<n;++i)
scanf("%d",&number[i]);
for(i=0; i<n;++i)
{
for(j=i+1; j<n;++j)
{
if(number[i]> number[j])
{
a= number[i];
number[i]= number[j];
number[j]= a;
}
}
}
printf("The numbers arranged in ascending order are given below\n");
for(i=0; i<n;++i)
printf("%d\n",number[i]);
}

Output:
Enter the value of N
5
Enter the numbers
80
20
67
10
45
The numbers arranged in ascending order are given below
10
20
45
67
80

4.// Implement binary search to find a particular name in a list of names;

#include <stdio.h>
#include <string.h>
void main()
{
Int i,n,low,high,mid;
char a[50][50],key[20];
printf("enter the number of names to be added\n");
scanf("%d",&n);
printf("enter the name in ascending order\n");
for(i=0;i<=n-1;i++)
{
scanf("%s",&a[i]);
}
printf("\n");
printf("enter the name to be searched\n");
scanf("%s",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if (strcmp(key,a[mid])==0)
{
printf("key found at the position %d\n",mid+1);
exit(0);
}
else if(strcmp(key,a[mid])>0)
{
high=high;
low=mid+1;
}
else
{
low=low;
high=mid-1;
}
}
printf("name not found\n");
}
5.Write a program to count the number of alphabets, special characters and words in a list of text
using file.

#include <stdio.h>
int main()
{
FILE *fp;
char filename[100];
charch;
int linecount, wordcount, charcount;
linecount = 0;
wordcount = 0;
charcount = 0;
printf("Enter a filename :");
gets(filename);
fp = fopen(filename,"r");
if ( fp )
{
while ((ch=getc(fp)) != EOF)
{
if (ch != ' ' &&ch != '\n')
{
++charcount;
}
if (ch == ' ' || ch == '\n')
{
++wordcount;
}
if (ch == '\n')
{
++linecount;
}
}
if (charcount> 0)
{
++linecount;
++wordcount;
}
}
else
{
printf("Failed to open the file\n");
}
printf("Lines : %d \n", linecount);
printf("Words : %d \n", wordcount);
printf("Characters : %d \n", charcount);
getchar();
return(0);
}

Sample Output
Consider a text file named wolf.txt with the following content:
Help Help,
Go away you naughty wolf.
Enter a filename :wolf.txt
Lines : 2
Words : 7
Characters : 30

6.Write a program of matrix multiplication using function.


#include<stdio.h>
#include<conio.h>

Void read_arr(int a[10][10],int row,int col)


{
Int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf("Enter Element %d %d : ",i,j);
scanf("%d",&a[i][j]);
}
}
}

Void mul_arr(int m1[10][10],int m2[10][10],int m3[10][10],int row,int col)


{
Int i,j,k;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
for (k=1;k<=row;k++)
{
m3[i][j] = m3[i][j] + (m1[i][k] * m2[k][j]);
}
}
}
}

Void print_arr(int m[10][10],int row,int col)


{
Int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
printf("%d ",m[i][j]);
}
printf("\n");
}
}

main()
{
int m1[10][10],m2[10][10],m3[10][10],row,col;
clrscr();
printf("Enter number of rows :");
scanf("%d",&row);
printf("Enter number of colomns :");
scanf("%d",&col);
read_arr(m1,row,col);
read_arr(m2,row,col);
mul_arr(m1,m2,m3,row,col);

print_arr(m3,row,col);
getch();
}
7.Calculate the binomial co-efficient using functions

#include<stdio.h>

Int BC(int n,int k);

Int main()

intn,k;

printf("Enter n and k : ");

scanf("%d%d",&n,&k);

printf("%\nBinomial coefficient\n",BC(n,k));

printf("%d\n",BC(n,k));

return0;

if(k==0||k==n)

return1;
return BC(n-1,k-1)+BC(n-1,k);

Output:

Enter nandk:

Binomial coefficient

Enternandk:

Binomial coefficient

70
8.Write a c program to check whether the given word is palindrome or not

#include<stdio.h>

#include<string.h>

int main()

char string1[20];

int i, length;

int flag =0;

printf("Enter a string:");

scanf("%s", string1);

length=strlen(string1);

for(i=0;i < length ;i++)

if(string1[i]!= string1[length-i-1])
{

flag=1;

break;

if(flag)

printf("%s is not a palindrome", string1);

Else

printf("%s is a palindrome", string1);

return0;

}
9.Write a c program to find the sum, average,standard deviation for a given set of numbers

#include <stdio.h>

#include <math.h>

#define MAXSIZE 10

void main()

float x[MAXSIZE];

int i, n;

float average, variance,std_deviation, sum =0, sum1 =0;

printf("Enter the value of N \n");

scanf("%d",&n);

printf("Enter %d real numbers \n", n);

for(i =0; i < n; i++)

{
scanf("%f",&x[i]);

/* Compute the sum of all elements */

for(i =0; i < n; i++)

sum = sum + x[i];

average = sum /(float)n;

/* Compute variance and standard deviation */

for(i =0; i < n; i++)

sum1 = sum1 +pow((x[i]- average),2);

variance = sum1 /(float)n;

std_deviation=sqrt(variance);

printf("Average of all elements = %.2f\n", average);

printf("variance of all elements = %.2f\n", variance);

printf("Standard deviation = %.2f\n",std_deviation);

Output

Enter the value of N

Enter 5 real numbers


34

88

32

12

10

Average of all elements = 35.20

variance of all elements = 794.56

Standard deviation = 28.19

10. write a program to print the student’s mark sheet assuming register number,name and marks
in 5 subjects in a structure. Create an array of structures and print the mark sheet in the university
pattern.

#include<stdio.h>
#include<conio.h>
structmark_sheet{
char name[20];
long introllno;
int marks[10];
int total;
float average;
char rem[10];
char cl[20];
}students[100];

int main(){
inta,b,n,flag=1;
char ch;
clrscr();
printf("How many students : \n");
scanf("%d",&n);
for(a=1;a<=n;++a){
clrscr();
printf("\n\nEnter the details of %d students : ", n-a+1);
printf("\n\nEnter student %d Name : ", a);
scanf("%s", students[a].name);
printf("\n\nEnter student %d Roll Number : ", a);
scanf("%ld", &students[a].rollno);
students[a].total=0;
for(b=1;b<=5;++b){
printf("\n\nEnter the mark of subject-%d : ", b);
scanf("%d", &students[a].marks[b]);
students[a].total += students[a].marks[b];
if(students[a].marks[b]<40)
flag=0;
}
students[a].average = (float)(students[a].total)/5.0;
if((students[a].average>=75)&&(flag==1))
strcpy(students[a].cl,"Distinction");
else
if((students[a].average>=60)&&(flag==1))
strcpy(students[a].cl,"First Class");
else
if((students[a].average>=50)&&(flag==1))
strcpy(students[a].cl,"Second Class");
else
if((students[a].average>=40)&&(flag==1))
strcpy(students[a].cl,"Third Class");
if(flag==1)
strcpy(students[a].rem,"Pass");
else
strcpy(students[a].rem,"Fail");
flag=1;
}
for(a=1;a<=n;++a){
clrscr();
printf("\n\n\t\t\t\tMark Sheet\n");
printf("\nName of Student : %s", students[a].name);
printf("\t\t\t\t Roll No : %ld", students[a].rollno);
printf("\n------------------------------------------------------------------------");
for(b=1;b<=5;b++){
printf("\n\n\t Subject %d \t\t :\t %d", b, students[a].marks[b]);
}
printf("\n\n------------------------------------------------------------------------\n");
printf("\n\n Totl Marks : %d", students[a].total);
printf("\t\t\t\t Average Marks : %5.2f", students[a].average);
printf("\n\n Class : %s", students[a].cl);
printf("\t\t\t\t\t Status : %s", students[a].rem);
printf("\n\n\n\t\t\t\t Press Y for continue . . . ");
ch = getche();
if((ch=="y")||(ch=="Y"))
continue;
}
return(0);
}

Outputs:
How many students : 2
Enter the details of 2 students :
Enter student 1 Name : H.Xerio
Enter student 1 Roll Number : 536435
Enter the mark of subject-1 : 46
Enter the mark of subject-1 : 56
Enter the mark of subject-1 : 76
Enter the mark of subject-1 : 85
Enter the mark of subject-1 : 75
Enter the details of 1 students :
Enter student 1 Name : K.Reriohe
Enter student 1 Roll Number : 237437
Enter the mark of subject-1 : 36
Enter the mark of subject-1 : 52
Enter the mark of subject-1 : 66
Enter the mark of subject-1 : 45
Enter the mark of subject-1 : 74

Mark Sheet
Name of Student : H.Xerio Roll No : 536435
------------------------------------------------------------------------
subject 1 : 46
subject 2 : 56
subject 3 : 76
subject 4 : 85
subject 5 : 75
------------------------------------------------------------------------
TotlMarks : 338 Average Marks : 67.6
Class : First Class Status : Pass

Press Y for continue . . .

You might also like