Sunday, 23 December 2018

Programming for Problem Solving - (3110003) - Program

1. Write a program to create a function which is used to check number is prime or not. function return 0 if number is not prime otherwise function should return 1.

#include<stdio.h>
#include<conio.h>

int prime(int);
int main()
{
    int num,ans;
    printf("Enter number: ");
    scanf("%d",&num);
    ans=prime(num);
    if(ans==1)
    {
        printf("\n Number is prime");
    }
    else
    {
        printf("\n Number is not prime");
    }
}
int prime(int num)
{
    int i,f;
    for(i=2;i<num;i++)
    {
        if(num%i==0)
        {
            f=1;
            break;
        }
        else
        {
            f=0;
        }
    }
        if(f==1)
        {
            return 0;
        }
        else
        {
            return 1;
        }
   
}

2. Write a program to enter a number and check last digit of number is odd or even.

#include<stdio.h>
#include<conio.h>

int main()
{
    int num,last;
    printf("Enter number: ");
    scanf("%d",&num);
    last = num%10;
   
    if(last%2==0)
    {
        printf("\n Last digit of number is even");
    }
    else
    {
        printf("\n Last digit of number is odd");
    }
}

3. Write a program to create a function which is used to add first n numbers.

#include<stdio.h>
#include<conio.h>

int add(int);
int main()
{
    int num,sum;
    printf("Enter how many numbers you want to add");
    scanf("%d",&num);
   
    sum=add(num);
    printf("\nFinal addition of all numbers %d",sum);
}
int add(int num)
{
    int i,sum=0;
    for(i=1;i<=num;i++)
    {
        sum=sum+i;
    }   
    return sum;
}

4. Write a program to convert Fahrenheit to Centigrade.

#include<stdio.h>
#include<conio.h>

int main()
{
    float fah, cel;
    printf("Enter temperature in Fahrenheit : ");
    scanf("%f",&fah);
    cel=(fah-32) / 1.8;
    printf("Temperature in Celsius = %f",cel);
}

5. Write a program to create calculator.

#include<stdio.h>
#include<conio.h>

int main()
{
    int a,b,c=0,ch;
    printf("\n 1. Addition");
    printf("\n 2. Subtraction");
    printf("\n 3. Division");
    printf("\n 4. Multiplication");
    printf("\n 5. Modulus");
    printf("\n Enter first number: ");
    scanf("%d",&a);
    printf("\n Enter second number: ");
    scanf("%d",&b);
      
    printf("\n Enter your choice: ");
    scanf("%d",&ch);
   
    switch(ch)
    {
        case 1: c=a+b;
                break;
        case 2: c=a-b;
                break;
        case 3: c=a/b;
                break;
        case 4:    c=a*b;
                break;
        case 5: c=a%b;
                break;
        default:
                printf("\n Invalid Choice");
                break;
    }
    if(ch<=5 && ch>=1)
    {
        printf("\n Answer %d: ",c);
    }
  }      

6. Write a program to create Fibonacci series.

#include<stdio.h>
#include<conio.h>

int main()
{
    int term,a,b,c,i;
    a=0;
    b=1;
    printf("Enter number of terms you want to print: ");
    scanf("%d",&term);
    if(term<=1)
    {
        printf("\n Invalid Term");
    }
    else
    {
        printf("\n%d   %d",a,b);
        for(i=1;i<=term-2;i++)
        {
            c=a+b;
            printf("   %d",c);
            a=b;
            b=c;
        }
    }
}


7. Write a program to create a function which is used to find the sum and average of value given by user as many as user wants to enter.

#include<stdio.h>
#include<conio.h>

int av(int);
int main()
{
    int num,add,avg;
    printf("\nHow many numbers you want to add");
    scanf("%d",&num);
    add=av(num);
    avg=add/num;
    printf("\n Addition is %d: ",add);
    printf("\n Average si %d: ",avg);
}
int av(int num)
{
    int i,no,sum=0;
    for(i=1;i<=num;i++)
    {
        printf("\n Enter %d number: ",i);
        scanf("%d",&no);
        sum=sum+num;
    }
    return sum;
}

8. Write a program to create a function to swap the values of two variables.(Call by Reference)

#include<stdio.h>
#include<conio.h>

void swap(int *, int *);
int main()
{
    int num1,num2;
    printf("\nEnter first number: ");
    scanf("%d",&num1);
    printf("\nEnter second number: ");
    scanf("%d",&num2);
    printf("\nValue before swapping");
    printf("\nValue of first number: %d",num1);
    printf("\nValue of second number: %d",num2);
    swap(&num1,&num2);
    printf("\n\n\nValue after swapping");
    printf("\nValue of first number: %d",num1);
    printf("\nValue of second number: %d",num2);
}
void swap(int *a, int *b)
{
    int c;
    c=*a;
    *a=*b;
    *b=c;
}

9. Write a program to check character from given string.

#include<stdio.h>
#include<conio.h>

int main()
{
char str[20],ch;

int i,f;
printf("Enter String: ");
gets(str);

printf("\nEnter Character to Search from String: ");
scanf("%c",&ch);

for(i=0;str[i]!='\0';i++)
{
if(ch==str[i])
{
f=0;
break;
}
else
f=1;
}
if(f==0)
printf("\n character is available");
else
printf("\n character is not available");
}

10. Write a program to print appropriate day using switch case.

#include<stdio.h>
#include<conio.h>

int main()
{
    int ch;
    printf("Enter day value between 1 to 7: ");
    scanf("%d",&ch);
    switch(ch)
    {
        case 1: printf("Sunday");
                break;
        case 2: printf("Monday");
                break;
        case 3: printf("Tuesday");
                break;
        case 4: printf("Wednesday");
                break;
        case 5: printf("Thursday");
                break;
        case 6: printf("Friday");
                break;
        case 7: printf("Saturday");
                break;
        default:
                printf("Invalid Value");
                break;               
        }
   
}

11. Write a program to find maximum and minimum numbers from given 10 numbers.

#include<stdio.h>
#include<conio.h>

int main()
{
    int a[10],max,min,i;
   
    for(i=0;i<10;i++)
    {
        printf("\nEnter %d number: ",i);
        scanf("%d",&a[i]);
    }
    max=0;
    min=a[0];
    for(i=0;i<10;i++)
    {
        if(max<a[i])
        {
            max=a[i];
        }
        if(min>a[i])
        {
            min=a[i];
        }
    }   
    printf("\nMaximum number: %d",max);
    printf("\nMinimum number: %d",min);
}

12.  Write a C program to calculate the average, geometric and harmonic mean of n elements
in an array.

#include<stdio.h>
#include<conio.h>
#include<math.h>

int main()
{
int i,mul=1,a[5];
float sum1=0,sum2=0,havg,gavg,avg;

for(i=0;i<5;i++)

{
printf("enter value of %d element : ",i+1);
scanf("%d",&a[i]);
}

//Calculation of Average
for(i=0;i<5;i++)
{
sum1=sum1+a[i];
}
avg=(float)sum1/5;

//Calculation of Harmonic mean
for(i=0;i<5;i++)
{
sum2=sum2 + (float) 1/a[i];
}
havg=(float)5/sum2;

//Calculation of Geometric mean
for(i=0;i<5;i++)
{
mul=mul*a[i];
}
gavg=pow(mul,(float)1/5);

printf("\n Average of array %f",avg);
printf("\n Harmonic Mean of array %f",havg);
printf("\n Goemetric Mean of array %f",gavg);

}

13. Write a program to wire string into file


#include<stdio.h>
#include<conio.h>

int main()
{
    FILE *fp;
    fp=fopen("a.txt","w");
    char str[30];
    int i=0;
    printf("Enter string that you want to write into file: ");
    gets(str);
 
    while(str[i]!='\0')
    {
        fputc(str[i],fp);
        i++;
    }
    if(str[i]==NULL)
    {
        printf("\nString is successfully written into file");
    }
}


 14. Write a program to convert string into upper case.

 #include<stdio.h>
#include<conio.h>

int main()
{
    char str[30];
    int i;
    printf("Enter string: ");
    gets(str);
 
    for(i=0;str[i]!='\0';i++)
    {
        if(str[i]>=97 && str[i]<=123)
        {
            str[i]=str[i]-32;
        }
    }
    printf("\n String in Upper Case: %s",str);
 
}

 15. Write a C program using global variable, static variable and local variable

 #include<stdio.h>
#include<conio.h>

int c; //global variable
int runner()
{
    static int count = 0;
    count++;
    return count;
}

int main()
{
    int a; //local variable
    printf(" Output of count variable: %d", runner());
    a=runner();
    a = c + a;
    printf("\n Output of local variable: %d",a);
    return 0;
}

16. Write a program to sort an array using selection sort


#include<stdio.h>
#include<conio.h>

int main()
{
    int a[5],i,j,temp;
    for(i=0;i<5;i++)
    {
        printf("Enter value of %d element: ",i+1);
        scanf("%d",&a[i]);
    }
    for(i=0;i<5;i++)
    {
        for(j=0;j<5;j++)
        {
            if(a[i]<a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }
    printf("\nSorted");
    for(i=0;i<5;i++)
    {
        printf("\n %d",a[i]);
    }
}

17. Write a C program to find 1+1/2+1/3+1/4+....+1/n.

 #include<stdio.h>
#include<conio.h>

int main()
{
    int i,term;
    float sum=0;
    printf("Enter total number of terms you want to add: ");
    scanf("%d",&term);
 
    for(i=1;i<=term;i++)
    {
        sum=sum+ (float) 1/i;
    }
    printf("\n Final sum: %f",sum);
}


18. Write a program to print address of variable using pointer.

#include<stdio.h>
#include<conio.h>

int main()
{
    int a,*p;
    p=&a;
    printf("Enter value of a:");
    scanf("%d",&a);
 
    printf("Value of a: %d",a);
    printf("\nAddress of a: %p",p); //%p is used to print pointer value
 
}


19. Write a program to find maximum number between three numbers using nested if.

#include<stdio.h>
#include<conio.h>

int main()
{
    int a,b,c;
    printf("\nEnter first number: ");
    scanf("%d",&a);
    printf("\nEnter second number: ");
    scanf("%d",&b);
    printf("\nEnter third number: ");
    scanf("%d",&c);
 
    if(a>b)
    {
        if(a>c)
        {
            printf("\nFirst number is maximum");
        }
        else
        {
            printf("\nThird number is maximum");
        }
    }
    else
    {
        if(b>c)
        {
            printf("\nSecond number is maximum");
        }
        else
        {
            printf("\nThird number is maximum");
        }
    }
}


20. Create a structure with name time_struct with three members. Hours, minutes and seconds
      Take value from user and display in following format : HH:MM:SS


#include<stdio.h>
#include<conio.h>

struct time_struct
{
    int hours;
    int minutes;
    int seconds; 
};
int main()
{
    struct time_struct t1;
    printf("\nEnter Hours: ");
    scanf("%d",&t1.hours);
    printf("\nEnter Minutes: ");
    scanf("%d",&t1.minutes);
    printf("\nEnter Seconds: ");
    scanf("%d",&t1.seconds);
 
    if(t1.hours<=24 && t1.minutes<=59 && t1.seconds <=59)
    {
        printf("\n%d:%d:%d",t1.hours,t1.minutes,t1.seconds);
    }
    else
    {
        printf("Invalid value");
    }
}

No comments:

Post a Comment