c programming 2015

2nd Semester

Year 2015

Q.No. 2 Write function that takes list of integers of size 10 as parameter and display all the prime numbers of the list.

#include<stdio.h>
void chk(int a[])
{
    printf("Prime numbers:\n");
    int c;
    for(int i=0;i<10;i++)
    {
        c=0;
        for(int j=2;j=a[i]/2;j++)
        {
            if(a[i]%j==0)
            {
                c=1;
                break;
            }
        }
        if(c==0)
            printf("%d\n",a[i]);
    }
}
void main()
{
    int a[10];
    printf("Enter 10 numbers: ");
    for(int i=0;i,<10;i++)
        scanf("%d",a[i]);
    chk(a);
}
Output:
Enter 10 numbers: 5 6 7 8 9 10 11 1 2 3
Prime numbers:
5
7
11
1
2
3

Q.no 3 Write a program that displays the line “Best of Luck” 5 times, using a function.

#include<stdio.h>
void dis()
{
    for(int i=0;i<5;i++)
        printf("Best of Luck\n");
}
void main()
{
    dis();
}
Output:
Best of Luck
Best of Luck
Best of Luck
Best of Luck
Best of Luck

Q.No. 5 Write a program to read a line of text, and display the number of vowels in it.

#include<stdio.h>
#include<string.h>
void main()
{
    char a[50];
    int i=0,c=0;
    printf("Enter line:\n");
    gets(a);
    strlwr(a);
    while(a[i]!='\0')
    {
        if(a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u')
            c++;
        i++;
    }
    printf("Number of vowels in the user given string is: %d.",c);
}
Output:
Enter line:
AEIOU
Number of vowels in the user given string is: 5.

Q.No. 6 Write a program that prints three different strings in three different lines.

#include<stdio.h>
void main()
{
    char a[3][30];int i;
    printf("Enter 3 texts:\n");
    for(i=0;i<3;i++)
       gets(a[i]);
    for (i=0;i<3;i++)
       printf("%s\n",a[i]);
}
Output:
Enter 3 texts:
Hello
How
Are you?
Hello
How
Are you?




Q.No. 7

#include<stdio.h>
#include<conio.h>
void main(){
	FILE * fp;
	char ch;
	char name[100];
	fp = fopen("a.txt","a");
	printf("y/n");
	scanf("%c", &ch);
	printf("%c", ch);
	while(ch == 'y'){
		printf("Enter name");
		scanf("%s", name);
		printf("%s", name);
		if(name[0] == 'a')
		fprintf(fp,"%s\n", name);
		printf("add y/n");
		ch = getche();
	}
	fclose(fp);
	getch();
}

Q.No. 8

#include<stdio.h>
#include<conio.h>
void main(){
	int i,j,k,l;
	for(i = 1; i<=4;i++){
		for(k=4;k>=i;k--){
			printf(" ");
		}
		for(j = i; j>=1;j--){
			printf("%d", j);
		}
		j = 1;
		for(l = j+1; l<=i;l++)
		{
			printf("%d", l);
		}
		printf("\n");
	}
	getch();
}

Q.No. 9

#include<stdio.h>
#include<conio.h>
struct employee{
	char name[100];
	int age;
};
void main(){
	int i, age = 0, averageAge;
	struct employee e[5];
	for(i = 0; i < 5; i++){
		printf("Enter name of employee %d: ", (i+1));
		scanf("%s", e[i].name);
		printf("Enter age of employee %d: ", (i+1));
		scanf("%d", &e[i].age);
	}
	for(i = 0; i < 5; i++){
		age = age + e[i].age;
	}
	averageAge = age/5;
	printf("\nThe average age of the employees is %d", averageAge);
	getch();
}

 






Leave a Reply

Your email address will not be published.