Prac Pgms Alt-1
Prac Pgms Alt-1
#include<stdio.h>
int main()
int num,i,count=0;
scanf("%d",&num);
for(i=2;i<=num/2;i++)
if(num%i==0)
count++;
break;
if(count==0 &&num!= 1)
else
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:
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
#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
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 main()
intn,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;
printf("Enter a string:");
scanf("%s", string1);
length=strlen(string1);
if(string1[i]!= string1[length-i-1])
{
flag=1;
break;
if(flag)
Else
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;
scanf("%d",&n);
{
scanf("%f",&x[i]);
std_deviation=sqrt(variance);
Output
88
32
12
10
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