C programming 2014

2nd Semester

year 2014
Q.no 3 Write function that display the factorial of 7 only.

#include<stdio.h>
void fact(int n)
{
    int fact = 1;
    if(n==7)
    {
       for(int i=1;i<=n;i++)
           fact*=i;
       printf("The factorial of 7 is: %d.",fact);
    }
    else
       printf("This function gives the factorial of the number 7 only");
}
void main()
{
    fact(7);
}
Output:
The factorial of 7 is: 5040.

Q.no 4 Write program that takes string as input from the user, and then display the length.

#include<stdio.h>
#include<string.h>
void main()
{
    char srt[50];
    printf("Enter a string:\n");
    gets(str);
    printf("The length of string is %d.",strlen(srt));
}

Q.no 5 Write a program to display the following pattern.
* #
* #
* ? ?
* ? ? +

#include<stdio.h>
void main()
{
    int i;
    for(i=0;i<4;i++)
    {
        printf("*\t");
        if(i<2)
            printf("#");
        if(i==2)
            printf("?\t?");
        if(i==3)
            printf("?\t?\t+");
        printf("\n");
    }
}

Group “c”

Q.no 7 Write function that takes string from the user, if the length is 7, then display the middle character of that string and function returns, otherwise user is asked to input another string.

#include<stdio.h>
#include<string.h>
int chk()
{
    char a[50];
    printf("Enter a string: ");
    gets(a);
    if(strlen(a)==7)
    {
        printf("Middle character of the given string: %c.",a[3]);
        return 1;
    }
    return 0;
}
void main()
{
    int  i = 1;
    while(i)
    {
        if(chk())
            break;
    }
}
Output:
Enter a string: Hello
Enter a string: Byebye!
Middle character of the given string: b.
<input id="768.1866742804351" class="cdbx-try-code cdbx-btn-main" style="background-color: #ffffff; margin-bottom: 0; color: #008b8b; border: 1px solid rgba(231, 231, 230, 1); border-radius: 10px; font-size: 13px; height: 30px; min-width: 60px; max-width: 150px; padding: 4px; font-weight: normal; outline: none; display: none; float: right;" type="button" value="Run" data-code="1534082476681.867" data-highlight="" data-lang="0" data-filename="Filename" data-mce-style="background-color: #ffffff; margin-bottom: 0; color: #008b8b; border: 1px solid rgba(231, 231, 230, 1); border-radius: 10px; font-size: 13px; height: 30px; min-width: 60px; max-width: 150px; padding: 4px; font-weight: normal; outline: none; display: none; float: right;">

 
Q.No. 8 Write program that stores the information of students in a file. In this file new records of the students can be added without affecting the existing records of the students. Students have name, age and address as attributes.

#include <stdio.h>
struct student{
    char name[20],address[30];
    int age;
}p;
int main()
{
    FILE *A;
   printf("enter a std name,address and age for 10 std; ");
   scanf("%s%s%d%d",p.name,p.address,&p.age);
   A= fopen("a.txt","a+");
   fprintf(A,"\n\n%s\n%s\n%d\n%d",p.name,p.address,p.age);
   fclose(A);
   return 0;
}




Q.No. 9 Write program which displays sum of all the elements of a 3D array.

#include<stdio.h>
#include<conio.h>
void main()
{
    int a[2][2][2],b[2][2][2];
    int i,j,k,sum1=0 ,sum2=0;
    clrscr();
    printf("enter the element in frst array");
    for (i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            for(k=0;k<2;k++)
            {
                scanf("%d",&a[i][j][k]);
                sum1+=a[i][j][k];
            }
        }
    }
    printf("enter the element in seecond array");
    for (i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            for(k=0;k<2;k++)
            {
                scanf("%d",&b[i][j][k]);
                sum2+=a[i][j][k];
            }
        }
    }
    printf("the sum of all element is %d",sum1+sum2);
    getch();
}

Leave a Reply

Your email address will not be published.