Showing posts with label Accept. Show all posts
Showing posts with label Accept. Show all posts

C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers

How to write a c program to accept n numbers from user store these numbers into an array & calculate average of n numbers in C Programming Language ?

Solution:
/*C Program to accept n numbers from user store these numbers into an array & calculate average of n numbers*/
#include<stdio.h>
#include<conio.h>
  void main()
{
  int a[20],n,i,t=0;
  float avg=0;
  clrscr();
  printf("\nEnter the n numbers: ");
  scanf("%d",&n);
  printf("\nEnter the numbers: ");
  for(i=0;i<n;i++)
{
  scanf("%d",&a[i]);
  t=t+a[i];
}
  printf("\nTotal is:%d",t);
  avg=(float)t/n;
  printf("\nAverage of n numbers is: %f",avg);
  getch();
}

//OUTPUT:
//Enter the n numbers: 10
//Enter the numbers: 1
//2
//3
//4
//5
//6
//7
//8
//9
//10

//Total is: 55
//Average of n numbers is: 5.5

C Program to accept n numbers & store all prime numbers in an array & display this result

How to write a C Program to accept n numbers & store all prime numbers in an array & display this result in C Programming Language ?


Solution:
/*C Program to accept n numbers & store all prime numbers in an array & display this result*/
#include<stdio.h>
#include<conio.h>
  void main()
{
  int a[10],i,n;
  clrscr();
  printf("\nEnter the n number: ");
  scanf("%d",&n);
  printf("\nEnter the numbers: ");
  for(i=0;i<=n;i++)
{
  scanf("%d",&a[i]);
}
  printf("\nThe Prime Number is: ");
  for(i=1;i<=n;i++)
{
  if(a[i]%2==1)
{
  printf("\t%d",a[i]);
}
}
  getch();
}

//OUTPUT:
//Enter the n number: 4

//Enter the numbers: 1
//2
//3
//4
//5

//The Prime Number is: 3 5

C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order

How to write a c program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order in C Programming Language ?


Solution:
/*C program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
  void main()
{
  char c[10][20],temp[20];
  int i,j,n;
  clrscr();
  printf("\nEnter number of strings: ");
  scanf("%d",&n);
  printf("\nEnter the strings: ");
  for(i=0;i<=n;i++)
{
  gets(c[i]);
}
  puts(c[i]);
  for(i=0;i<=n;i++)
{
  for(j=i+1;j<=n;j++)
{
  if(strcmp(c[i],c[j])>0)
 {
  strcpy(temp,c[i]);
  strcpy(c[i],c[j]);
  strcpy(c[j],temp);
 }
}
}
  printf("\nSorted list is:\n");
  for(i=0;i<=n;i++)
  printf("\n%s",c[i]);
  getch();
}

//OUTPUT:
//Enter the number of strings: 3
//Enter the strings: VINIT
//RAVI
//SURAJ
//Sorted list is: RAVI
//SURAJ
//VINIT

C Program to accept n numbers from user, store these numbers into an array. Find out Maximum & Minimum number from an Array

How to write a c program to accept n numbers from user, store these numbers into an array. Find out Maximum & Minimum number from an Array in C Programming Language ?


Solution:
/*C Program to accept n numbers from user, store these numbers into an array. Find out Maximum & Minimum number from an Array*/
#include<stdio.h>
#include<conio.h>
  void main()
{
  int a[10],i,n,max,min;
  clrscr();
  printf("\nEnter the n numbers: ");
  scanf("%d",&n);
  printf("\nEnter  the numbers: ");
  for(i=0;i<n;i++)
{
  scanf("%d",&a[i]);
}
  max=a[0];
  min=a[0];
  for(i=0;i<n;i++)
{
  if(a[i]>max)
{
  max=a[i];
}
  if(a[i]<min)
{
  min=a[i];
}
}
  printf("\nMaximum number is:%d",max);
  printf("\nMinimum number is:%d",min);
  getch();
}

//OUTPUT:
//Enter the n numbers: 4
//Enter the numbers: 1 2 3 4
//Maximum number is:4
//Minimum number is:1

C Program to accept data from user store that data into text file

How to write a C program to accept data from user store that data into text file in C Programming Language ?


Solution:
/*write a c program to accept data from user store that data into text file */
#include<stdio.h>
#include<conio.h>
void main()
{
char str[60];
FILE *fp;
clrscr();
printf("\nenter the string:");
gets(str);
fp=fopen("peer.txt","w");
if(fp==NULL)
{
printf("connot open");
exit();
}
fputs(str,fp);
getch();
}
/*
enter the string:i am not an intelligent                                      
                                                                             
Type EXIT to return to Turbo C++. . .Microsoft(R) Windows DOS                
(C)Copyright Microsoft Corp 1990-2001.                                        
                                                                             
C:\TC\BIN>type peer.txt                                                      
i am not an intelligent                                                      
C:\TC\BIN>exit                                                                
*/

Accept n number from user, store these number into an array and calculate the average of n number

How to write a C Program to accept n number from user, store these number into an array and calculate the average of n number in C Programming Language ?


Solution:
/*write a c progarm to accept n number from user, store
these number into an array and calculate the average of n
number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,n,sum=0;
float avg;
clrscr();
printf("\nenter how many number :");
scanf("%d",&n);
printf("\nenter the element :\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
avg=sum/n;
printf("\nthe average is :%f",avg);
getch();
}
/*
enter how many number :5                                                    
                                                                           
enter the element :                                                        
9                                                                          
8                                                                          
7                                                                          
4                                                                          
5                                                                          
                                                                           
the average is :6.000000                                                    
*/