Showing posts with label c program. Show all posts
Showing posts with label c program. Show all posts

Tuesday, 23 June 2015

WRITE A C PROGRAME TO FIND AMSTRONG NUMBER



#include<stdio.h>
int main()
{
  int n, n1, rem, num=0;
  printf("Enter a positive  integer value: ");
  scanf("%d", &n);
  n1=n;  while(n1!=0)
  {
                        rem=n1%10;
                        num+=rem*rem*rem;
                        n1=n1/10;
            }
            if(n==num)
            {
                        printf("%d is an Armstrong number",n);
            }
            else
                        printf("%d is not an Armstrong number",n);
}


OUTPUT

Enter a positive  integer value: 142
142 is not an Armstrong number

Write a C Program to read a file and display its content along with line number before each line.



#include<stdio.h>
main()
{
    FILE *fs;
    char ch;
    int i=1;
    fs=fopen("Hello.c","r");
    if(fs==NULL)
    {
        printf("can't open source file");
        exit(1);
    }
    printf("%d",i++);
    while(1)
    {
        ch=fgetc(fs);
        if(ch==EOF)
        {
            break;
        }
        printf("%c",ch);
        if(ch=='\n')
        {
            printf("%d",i);
            i++;
        }
    }
    fclose(fs);
}

OUTPUT
1#incluce<stdio.h>
2main()
3{
4       printf("Hello World");
5}
6

NOTE: First save "Hello.c" file

WRITE A C PROGRAM TO DELETE ALL VOWELS FROM THE SENTENC. ASSUME THAT SENTENSE IS NOT MORE THAN 80 CHARACTERS LONG



#include<stdio.h>
main()
{
    char line[80],ar[5],newline[80],ch;
    int i=0,j=0,k=0,count=0;
    printf("Enter a sentence:\n");
    gets(line);
    while(line[i]!='\0')
    {
        ch=toupper(line[i]);
        if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
        {
            count++;
        }
        else
        {
            newline[k]=line[i];
            k++;
        }
        i++;
    }
    newline[k]='\0';
    printf("\nNumber of vowels are %d \n",count);
    printf("\nThe modified line is : \n%s",newline);
}

OUTPUT
Enter a sentence:
Hai friends this is raj
Number of vowels are 7
The modified line is :
H frnds ths s rj

WRITE A C PROGRAM TO FIND SMALLEST NUMBERS IN A GIVEN ARRAY



#include<stdio.h>
main()
{
    int a[10],i,min;
    printf("Enter 10 numbers in the array :");
    for(i=0;i<10;i++)
    {
        scanf("%d",&a[i]);
    }
    min=a[0];
    for(i=1;i<10;i++)
    {
        if(a[i]<min)
        {
            min=a[i];
        }
    }
    for(i=0;i<10;i++)
    {
        printf("%d  ",a[i]);
    }
    printf("\n The minimum number is %d",min);
}

OUTPUT
Enter 10 numbers in the array : 5
8
9
52
65
2
78
45
96
35
5  8  9  52  65  2  78  45  96  35
The minimum number is 2

WRITE A C PROGRAM TO COPY THE FILE.REPLACE ALL LOWERCASE CHARACTERS IN SOURCE FILE TO UPPER CASE CHARACTERS IN DESTINATION FILE



#include<stdio.h>
main()
{
    FILE *fs,*fd;
    char ch;
    fs=fopen("Hello.c","r");
    if(fs==NULL)
    {
        printf("can't open source file");
        exit(1);
    }
    fd=fopen("deccan.txt","w");
    if(fd==NULL)
    {
        printf("can't open destination file");
        exit(2);
    }
    while(1)
    {
        ch=fgetc(fs);
        if(ch==EOF)
        {
            break;
        }
        fputc(toupper(ch),fd);
    }
    fclose(fs);
    fclose(fd);
}


OUTPUT
-------------------------------------------
NOTE: First create a file name as Hello.c