All C programs Solutions
C-Ready Program Solutions
Question : Entering Data & other Operations on 2D Numeric Array
main()
{
int num[3][3],r,c;
int sum;
clrscr();
printf("Entering Data:");
getch();
for(r=0;r<3;r=r+1)
{
for(c=0;c<;c=c+1)
{
printf("Enter Number:");
scanf("%d",&num[r][c]);
}
}
clrscr();
printf("Showing Data");
getch();
printf("\n ROW MAJOR Order\n");
for(r=0;r<3;r=r+1)
{
for(c=0;c<3;c=c+1)
{
printf(" %d ",num[r][c]);
}
}
printf("\n\n Column Major Order");
getch();
for(c=0;c<3;c=c=1)
{
for(r=0;r<;r=r+1)
{
printf(" %d", num[r][c]);
}
printf("\n");
}
clrscr();
printf("\n Calculating Row WIse Total");
getch();
for(r=0;r<3;r=r+1)
{
for(sum=0,c=0;c<3;c=c+1)
{
sum=sum+num[r][c];
}
printf("Total of Element of ROW %d is %d ",r+1,sum);
}
clrscr();
printf("Calculating Column wise total");
getch();
for(c=0;c<3;c=c+1)
{
for (sum=0,r=0;r<3;r=r+1)
{
sum=sum+num[r]c];
}
printf("\nTOtal of Elements of Column %d is %d",c+1,sum);
}
getch();
}
// Bubble Sort
main()
{
int num[10],i,j,c;
clrscr();
for(c=0;c<10;c=c+1)
{
printf("Enter Data:");
scanf("%d",&num[c]);
}
clrscr();
printf("Sorting and Showing Data");
getch();
for (i=0;i<10;i=i+1)
{
for(j=0;j<9;j=j+1)
{
if(num[j]>num[j+1])
{
c=num[j];
num[j]=num[j+1];
num[j+1]=c;
]
}
}
printf("Sorted Data is :");
for(i=0;i<10;i=i+1)
{
printf("%d ",num[c]);
}
getch();
}
Character array
main()
{
char str[80];
int l,w;
printf("Enter Any String");
fflush(stdin);
getch(str);
printf("Counting Length of String");
getch();
i=0;
while(str[i]!='\0')
{
l=l+1;
}
printf("Length of String %d",l);
getch();
printf("Counting No. of Words,'wait'");
getch();
l=0;w=0;
while(1)
{
if((str[l]==' '))
w=w+1;
l=l+1;
if(str[l]=='\0')
{
w=w+1;
break;
}
}
printf("Total Number of Words %d",w);
getch();
clrscr();
printf("COunting Vowels in the String ");
getch();
l=0;w=0;
while(str[l]!='\0')
{
switch(str[l])
{
case 'A' :
case 'E':
case 'I':
case 'O':
case 'U':
w=w+1;
break;
}
l=l+1;
}
printf(" Number of CAPITAL VOWELS %d",w);
getch();
clrscr();
printf("ENCRYPTING STRING");
getch();
l=0;
while(str[l]!='\0')
{
str[l]=str[l]+50;
l=l+1;
}
printf("Encrypted String is %s",str);
getch();
printf("Reverse Printing the String");
l=0;
while(str[l]!='\0')
{
l=l+1;
}
l=l-1;
while(l>0)
{
printf("%c",str[l]);
l=l-1;
}
getch();
}
Question Ask for month number and print how many days
main()
{
int month;
clrscr();
printf("Enter Month Number:");
scanf("%d",&month);
switch(month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("There are 31 Days");
break;
case 2:
printf('THere are 28 or 29 Days");
break;
case 4:
case 6:
case 9:
case 11:
printf("THere are 30 Days");
break;
default:
printf("Invalid Month Value");
}
getch();
}
Question Decimal to Binary Conversion Program
main()
{
int num,q,r;
clrscr();
printf("Enter Number:");
scanf("%d",&num);
while(1)
{
q=num/2;
r=num%2;
if(q<1)
{
printf("%d",r);
break;
}
printf("%d",r);
num=q;
}
getch();
}
Question : Write C program to calculate Factorial of given Number
main()
{
int fact,num;
clrscr();
printf("Enter Any Number:");
scanf("%d",&num);
fact=1;
while(num>=1)
{
fact=fact*num;
num=num-1;
}
printf("Factorial is %d",fact);
getch();
}
Question: Greater among three numbers
main()
{
int a,b,c;
clrscr();
printf("Enter First Number;");
scanf("%d",&a);
printf("Enter Second Number;");
scanf("%d",&b);
printf("Enter Third Number:");
scanf("%d",&c);
if ((a>b) && (a>c))
printf("First Number is greater");
if ((b>a) && (b>c))
printf("Second Number is greater");
if ((c>a) && (c>b))
printf("Third Number is greater");
getch();
}
// Linked List Implementation
struct student
{
int reg_no;
char sname[20];
struct student *next;
};
struct student *fp,*ep,*temp,*p,s;
main()
{
int ch;
fp=NULL;
ep=NULL;
clrscr();
while(1)
{
printf("\n 1. Enter Data");
printf("\n 2. Show List");
printf("\n3. Search,show and delete");
printf("\n4. Exit");
printf("\nEnter Your Choice");
scanf("%d",&ch);
if(ch==1)
enter_data();
if(ch==2)
show_list();
if(ch==3)
search_show();
if(ch==4)
break;
}
}
enter_data()
{
temp=(struct student *) (malloc(sizeof(s)));
if(temp==NULL)
{
printf("Bad Memory Allocation");
getch();
return;
}
printf("Enter Details");
printf(\nEnter Registration No.");
scanf("%d",&temp->reg_no);
printf("\nEnter Name");
fflush(stdin);
gets(temp->sname);
if(fp==NULL)
{
fp=temp;
ep=temp;
fp->next=NULL;
return;
}
else
{
ep->next=temp;
temp->next=NULL;
ep=temp;
}
}
show_list()
{
p=fp;
while(1)
{
if(p==NULL)
{
printf("List Empty,No More Items to Show");
getch();
break;
}
printf("\nRegistration No:%d",p->reg_no);
printf("\nStudent Name:%s",p->sname);
p=p->next;
}
}
search_show()
{
int x;
clrscr();
printf("Enter REgistration No. to search & Delete ");
scanf("%d",&x);
p=fp;
while(1)
{
if(p==NULL)
{
printf("Search Failed, No MAtch Found");
getch();
break;
}
if((p->reg_no)==x)
{
printf("Data Found");
printf("\n Registration No: %d",p->reg_no);
printf("\nName %s",p->sname);
if(p==fp)
{
fp=fp->next;
printf("Data Deleted");
break;
}
prev->next=p->next;
printf("\nData Deleted");
break;
}
else
{
prev=p;
p=p->next;
}
}
}
Question : Sum of all Elements , highest, lowest from array
main()
{
int num[10],c,sum,max,min;
clrscr();
for(c=0;c<10;c=c+1)
{
printf("Enter Data");
scanf(%d",&num[c]);
}
sum=0;max=num[0];min=num[0];
for(c=0;c<10;c=c+1)
{
sum=sum+num[c];
if(max<num[c])
max=num[c];
if(min>num[c])
min=num[c];
}
printf("\n SUm of all Elements = %d",sum);
printf("\n Highest from Array = %d",max);
printf("\n Lowest from Array = %d",min);
getch();
}
Question Sum of first n natural numbers
#include <stdio.h>
main()
{
int n,t,sum;
clrscr();
printf("How Many natural Number:");
scanf("%d",&n);
sum=0;
t=0;
while(t<=n)
{
sum=sum+1;
t=t+1;
}
printf("Sum of %d natural Numbers is %d",n,sum);
getch();
}
Question Program to calculate nP i.e. n to the power P
main()
{
int n,m,p;
clrscr();
printf("Enter Number:");
scanf("%d",&n);
printf("Enter POwer");
scanf("%d",&p);
m=1;
while(p>=1)
{
m=m*n;
p=p-1;
}
printf("Answer is :%d",m);
getch();
}
Question Check whether entered number is EVEN, ODD POSITIVE or NEGATIVE
main()
{
int num;
clrscr();
printf("Enter Any Number:");
scanf("%d",&num);
if (num>0)
printf("Number is Positive");
else
printf("Number is Negative");
if (num%2==0)
printf("Number is EVEN");
else
printf("Number is ODD");
getch();
}
Question Enter Data in one dimensional array and display data with INDEX Value
main()
{
int num[50],c,n;
clrscr();
printf("How many Elements to be entered");
scanf("%d",&n);
if (n>50)
{
pritnf("Out of Range");
return;
}
for(c=0;c<n;c=c+1)
{
printf("Enter Data:");
scanf("%d",&num[i]);
}
for (c=0; c<n;c=c+1)
{
printf("Data as INDEX %d = %d",c+1,num[c]);
printf("\n");
}
getch();
}
Question Generate pattern 1 4 9 16 ... 100
main()
{
int n,p;
clrscr();
n=1;
p=1;
while(n<=10)
{
p=n*n;
n=n+1;
printf("%d",p);
}
getch();
}
Program : Generate Following pattern
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
main()
{
int n,c,p;
clrscr();
n=1;c=1;p=1;
while(c<=5)
{
while(n<=c)
{
printf(" %d",n);
n=n+1;
}
n=n-2;
while(n>=1)
{
printf(" %d",n);
n=n-1;
}
c=c+1;
n=1;
}
}
5
5 4
5 4 3
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
main()
{
int n,c,p;
clrscr();
c=1;
while(c<=5)
{
p=5;
n=1;
while(n<=c)
{
printf("%d",p);
p=p-1;
n=n+1;
}
c=c+1;
}
c=c-2;
while(c>=1)
{
p=5;
n=1;
while(n<=c)
{
printf(" %d",p);
p=p-1;
n=n+1;
}
c=c-1;
}
getch();
}
Question Reverse the contents of Array
main()
{
int num[10],temp,c,j;
clrscr();
for (c=0;c<10,c=c+1)
{
printf("Enter Data");
scanf("%d", &num[c]);
}
clrscr();
printf("Reversing the Data of Array");
getch();
for(c=0,j=9;;c=c+1,j=j+1)
{
if(c>j)
break;
temp=num[c];
num[c]=num[j];
num[j]=temp;
}
for (c=0;c<9;c=c+1)
{
printf("Data at %d = %d", c+1,num[c]);
}
getch();
}
Question : Selection Sort
main()
{
// program is same as Bubble Sort
//sorting loop
for(i=0;i<9;i=i+1)
{
for(j=i+1;j<8;j=j+1)
{
if(num[i]>num[j])
{
c=num[i];
num[i]=num[j];
num[j]=c;
}
}
}
// rest of the program is same as bubble sort
}
Program ; Implementation of Structure.
#include <stdio.h>
struct employee
{
int emp_code;
char emp_name[20];
double emp_sal;
};
main()
{
struct student s;
int ch;
while(1)
{
printf("\n 1. Enter Data:");
printf("\n2.Show Data");
printf("\n3.Exit");
printf("\n Enter Your Choice");
scanf("%d",&ch);
if(ch==1)
enter_data(&s);
if(ch==2)
show_data(s);
if(ch==3)
break;
}
}
enter_data(struct student *p)
{
printf("Enter Employee Code:");
scanf("d",&p->emp_code);
printf('Enter Employee Name:");
fflush(stdin);
gets(p->emp_name);
printf('Enter Salary");
scanf("%ld",&p->emp_sal);
}
show_data(struct student s)
{
printf("\nEmployee Code: %d",s.emp_code);
printf("\nEmployee Name: %s",s.emp_name);
printf("\nEmployee Salary: %ld",s.emp_sal);
}
Question TOGGLE CASE Program
main()
{
char str[]="Softech Computers, make Dreams True");
int i;
clrscr();
printf("Toggling the Available String");
getch();
i=0;
while(str[i]!='\0')
{
if((str[i]>=65) && (str[i]<=90))
{
str[i]=str[i]+32;
}
else
{
str[i]=str[i]-32;
}
i=i+1;
}
printf("\nToggled String is \n %s",str);
getch();
}
Program : Implementing File Linked and structures all together
#include <stdio.h>
struct employee
{
int emp_code;
char emp_name[20];
};
struct slist
{
int ecode;
char ename[20];
struct slist *next;
};
struct slist *fp,*ep,*p,*temp,s;
struct employee emp;
main()
{
int ch;
FILE *fp1;
fp=NULL;ep=NULL;
fp1=fopen("emp.dat","r");
if(fp1==NULL)
{
printf("Error Fetching Data");
getch();
return;
}
while((fread(&emp,sizeof(emp),1,fp1))==1)
{
temp=(struct slist *)(malloc(sizeof(s)));
if(temp==NULL)
{
printf("Error Fetching data from file,Operation Aborted");
getch();
return;
}
temp->ecode=emp.emp_code;
strcpy(temp->ename,emp.emp_name);
if(fp==NULL)
{
fp=temp;
ep=temp;
fp->next=NULL;
ep->next=NULL;
}
else
{
ep->next=temp;
temp->next=NULL;
ep=temp;
}
}
prinf("Data Fetched successfully,press any key to continue");
getch();
clrscr();
while(1)
{
prinf("\n 1. Show List");
printf("\n 2. Add New Data");
printf("\n 3. Delete Data");
printf("\n 4. Sort Data");
printf("\n 5. Exit");
printf("\n Enter Your Choice:");
scanf("%d",&ch);
if(ch==1)
show_list();
if(ch==2)
add_data();
if(ch==3)
delete_data();
if(ch==4)
sort_data();
if(ch==5)
{
write_back();
break;
}
}
}
show_list()
{
p=fp;
while(1)
{
if(p==NULL)
{
printf("List EMpty,or Data Finished");
getch();
return;
}
printf("\n Employee Code %d",p->ecode);
printf("\n Employee Namae %s",p->ename);
p=p->next;
}
}
add_data()
{
temp=(struct slist *) (malloc(sizeof(s)));
if(temp==NULL)
{
printf("Memory Allocation Failure");
getch();
return;
}
clrscr();
printf("\n Enter Employee Code:");
scanf("%d",&temp->ecode);
printf("Enter Employee Name");
fflush(stdin);
gets(temp->ename);
ep->next=temp;
temp->next=NULL;
ep=temp;
}
delete_data()
{
int x;
struct slist *prev;
printf("Enter Employee Code to delete Data");
scanf("%d",&x);
p=fp;
while(1)
{
if(p==NULL)
{
printf("List Empty, or Search Failed");
getch();
return;
}
if((p->ecode)==x)
{
if(p==fp)
{
fp=fp-next;
printf("Data Deleted');
return;
}
prev->next=p->next;
printf("Data Deleted");
return;
}
prev=p;
p=p->next;
}
}
sort_data()
{
sturct slist *q;
p=fp;
int x;
char str[20];
while(p!=NULL)
{
q=fp;
while((q-next)!=NULL)
{
if((q->ecode)>((q->next)->ecode))
{
x=q->ecode;
q->ecode=(q->next)->ecode;
strcpy(str,q->ename);
strcpy(q->ename,(q->next)->ename);
strcpy((q->next)->ename,str);
}
}
p=p->next;
}
show_list();
}
write_back()
{
FILE *fp1;
fp1=fopen("emp.dat","w");
if(fp1==NULL)
{
printf("Write BAck Failed, Error OPening Target FIle");
getch();
return;
}
printf("writing BAck DAta, Please Wait");
printf("\n Press Any Key to Continue");
getch();
p=fp;
while(1)
{
if(p==NULL)
{
printf("Write Back Complete, Temporary List EMpty");
getch();
break;
}
emp.emp_code=p->ecode;
strcpy(emp.emp_name,p->ename);
fwrite(&emp,sizeof(emp),1,fp1);
p=p->next;
}
fclose(fp);
}
Comments
Post a Comment