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");
    }
}

Thursday, 6 December 2018

Programming for Problem Solving (3110003) - Program no 1 to 5

1. Write a program that perfomrs as calculator. (Addition, Subtraction, Division, Multiplication, Modulus)

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

void main()
{
    int a,b,ans;
    clrscr();
    printf("Enter the value of a");
    scanf("%d",&a);
    printf("Enter the value of b");
    scanf("%d",&b);
   
    ans = a+b;
    printf("\n Addition :%d ",ans);
   
    ans=a-b;
    printf("\n Subtraction :%d ",ans);
   
    ans=a*b;
    printf("\n Multiplication :%d ",ans);
   
    ans=a/b;
    printf("\n Division :%d ",ans);
   
    ans=a%b;
    printf("\n Modulus :%d ",ans);
    getch();       
}



2. Write a program to find area of triangle(a=h*b*.5)

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

int main()
{
    float a,h,b;
    printf("Enter the value of height");
    scanf("%f",&h);
   
    printf("Enter the value of base");
    scanf("%f",&b);
   
    a = h*b*0.5;
    printf("\nArea of Triangle: %f",a);
          
}


 3. Write a program to calculate simple interest (i = (p*r*n)/100 )

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

int main()
{
    long int p,r,n,i;
   
    printf("Enter principal amount");
    scanf("%ld",&p);
   
    printf("Enter rate of interest");
    scanf("%ld",&r);
   
    prin

tf("Enter number of years");
    scanf("%ld",&n);
   
    i=(p*r*n)/100;
   
    printf("\nSimple interest: %ld",i);
          
}


4. Write a C program to interchange two numbers. 

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

int main()
{
    int a,b,temp;
    printf("Enter value of a");
    scanf("%d",&a);
   
    printf("Enter value of b");
    scanf("%d",&b);
   
    printf("Value before interchange");
    printf("\n Value of a: %d",a);
    printf("\n Value of b: %d",b);
   
    temp=a;
    a=b;
    b=temp;
   
    printf("\nValue after interchange");
    printf("\n Value of a: %d",a);
    printf("\n Value of b: %d",b);
              
}


5. Write a C program to enter a distance in to kilometre and convert it in to meter, feet, inches and centimetre 

#include<stdio.h>
#include<conio.h>
int main()
{
    float km, m, cm, feet, inch;
    printf("Enter the distance in kilometers: ");
    scanf("%f", &km);
    m=km*1000;
    cm=m*100;
    feet=km*3280.8;
    inch=feet*12;
    printf("The distance in meters is %f\n", m);
    printf("The distance in centimeters is %f\n", cm);
    printf("The distance in feet is %f\n", feet);
    printf("The distance in inches is %f\n", inch);
   
}
 

 

Programming for Problem Solving (3110003) - Program no 60.

60. A file with name data contains numeric value. Write a program to read that value and write even number into even.txt file and odd number into odd.txt file.

#include<stdio.h>
#include<conio.h>
int main()
{
    FILE *fp1,*fp2,*fp3;
    fp1=fopen("data.txt","r");
    fp2=fopen("odd.txt","w");
    fp3=fopen("even.txt","w");
    int a;
    while(1)
    {
        a=fgetc(fp1);
        if(a==EOF)
        break;
        else
        {
            if(a%2==0)
            {
                fputc(a,fp3);
            }   
            else
            {
                fputc(a,fp2);
            }
        printf("%c",a);
        }
    }
  }

Tuesday, 4 December 2018

Programming for Problem Solving (3110003) - Program no 40 & 41

40. Write a program to reverse string.

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

int main()
{
char str1[30],str2[30];
int i=0,j,len;
printf("Enter string");
gets(str1);
len=strlen(str1);
for(j=len-1;j>=0;j--)
{
str2[i]=str1[j];
i++;
}
str2[i]='\0';
strcpy(str1,str2);
printf("Reverse string: %s", str1);


}

41. Write a program to convert string into upper case

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

int main()
{
char str[30];
int i=0,j,len;
printf("Enter string");
gets(str);

for(i=0;str[i]!='\0';i++)
{
if(str[i]>=97)
{
str[i]=str[i]-32;
}
else
{
str[i]=str[i];
}

}

printf("\nString in UpperCase: %s", str);


}

Monday, 3 December 2018

Programming for Problem Solving (3110003) - Program No. 37 to 39

37. Write a program to find a 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");
}

38. Write a program to replace a character in given string.

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

int main()
{
char str[20],ch1,ch2;
int i,f;
printf("Enter String: ");
gets(str);

printf("\nEnter Character to Replace: ");
ch1=getche();

printf("\nEnter Character with Replace: ");
scanf("%c",&ch2);

for(i=0;str[i]!='\0';i++)
{
if(ch1==str[i])
{
str[i]=ch2;
f=0;
break;
}
else
f=1;
}
if(f==0)
{
printf("\n character is Replaced");
printf("\n New string %s",str);
}
else
printf("\n character is not available to replace");
}

39. Write a program to delete a character in given string.

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

int main()
{
char str1[20],ch,str2[20];
int i,j=0;
printf("Enter String: ");
gets(str1);

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

for(i=0;str1[i]!='\0';i++)
{
if(ch!=str1[i])
{
str2[j]=str1[i];
j++;
}
}
str2[j]='\0';
strcpy(str1,str2);
printf("\n New string %s",str1);
}

Programming for Problem Solving (3110003) - Program No. 35

35. 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);



}