Thursday, 7 November 2013

Download Thapar Lab Assignment - 1 (2013) SOLVED UTA003.zip
File size: 15.6KB


Note: All the programs are tested on a GCC compiler.

Find all C Programs HERE -
http://www.mavenscientists.com/2013/09/c-tutorial.html

If you like this, you can join for the free seminar here

http://www.mavenscientists.com/2013/11/request-free-seminar-faq.html

1
#include <stdio.h>
int main()
{
// Declaring five variables of datatype float.
// The variables are initialized with the values of temperatures of 5 sunny days
float temp1; // day1 temperature
float temp2; // day2 temperature
float temp3; // day3 temperature
float temp4; // day4 temperature
float temp5; // day5 temperature

printf("Enter Day 1 temperature : ");
scanf("%f", &temp1);
printf("Enter Day 2 temperature : ");
scanf("%f", &temp2);
printf("Enter Day 3 temperature : ");
scanf("%f", &temp3);
printf("Enter Day 4 temperature : ");
scanf("%f", &temp4);
printf("Enter Day 5 temperature : ");
scanf("%f", &temp5);

float sum = temp1 + temp2 + temp3 + temp4 + temp5; // calculating sum
float average = sum / 5; // finding average temperature
printf("Average temperature of 5 sunny days : %f", average);
return 0;
}



#include <stdio.h>
int main()
{
float basicCostOfVehicle;
float exciseDuty;
float salesTax;
float roadTax;
float totalCostofVehicle;

printf("Enter Basic Cost : ");
scanf("%f", &basicCostOfVehicle);

exciseDuty = (15 * basicCostOfVehicle) / 100;
salesTax = (10 * basicCostOfVehicle) / 100;
roadTax = (1 * basicCostOfVehicle) / 100;

totalCostofVehicle = basicCostOfVehicle + exciseDuty + salesTax + roadTax;

printf("\nExcise Duty (15%%) : %f", exciseDuty);
printf("\nSales Tax   (10%%) : %f", salesTax);
printf("\nRoad Tax    (1%%)  : %f", roadTax);
printf("\n\nTotal Cost  : %f", totalCostofVehicle);

return 0;
}


3
#include <stdio.h>

int main()
{
int x = 2;
int y = 3;
int z = 2;

printf("Enter X : ");
scanf("%d", &x);
printf("Enter Y : ");
scanf("%d", &y);
printf("Enter Z : ");
scanf("%d", &z);

float exp1 = x*x*x + 2*x*x + 3*x;
float exp2 = x + y*y + z*z*z;

printf("Expression 1 : %f", exp1);
printf("\nExpression 2 : %f", exp2);

return 0;
}


4
#include <stdio.h>

int main()
{
int month; // asked to find
int days; // asked to find

int totalDays = 224; // we enter this

month = totalDays / 30; // 30 is the number of days in a month (assume)
days = totalDays % 30; //the remainder is the number of days

printf("Total Days : %d", totalDays);
printf("\n\nMonths : %d", month);
printf("\nDays   : %d", days);

return 0;
}


5
#include <stdio.h>

int main()
{
int number = 7; // the number to check for even or odd

printf("Enter Number : ");
scanf ("%d", &number);

(number == 0 ? printf("Zero entered") : number % 2 == 0 ? printf("%d is EVEN", number): printf("%d is ODD", number) );

return 0;

}

6
#include <stdio.h>

int main()
{
int year;

printf("Enter Year : ");
scanf("%d", &year);

(year % 4 == 0 || year % 100 == 0 ? printf ("Leap Year"): printf ("Not a Leap Year"));

return 0;

}

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

int main()
{
   int a;
   int b;
   int c;
   int d;
   double root1;
   double root2;

   printf("Quadratic Equation :  a*x*x + b*x + c = 0");
   printf("\nEnter A : ");
   scanf("%d", &a);
   printf("Enter B : ");
   scanf("%d", &b);
   printf("Enter C : ");
   scanf("%d", &c);

d = b*b - 4*a*c;

if(d==0)
{
        printf("Both roots are equal\n");
        root1=-b/(2*a);
        root2=root1;
        printf("First  Root = %f\n",root1);
        printf("Second Root = %f\n",root2);
}
else if(d>0)
    {
        printf("Both roots are real and different\n");
root1=(-b+sqrt(d))/(2*a);
root2=(-b-sqrt(d))/(2*a);
printf("First  Root = %f\n",root1);
printf("Second Root = %f\n",root2);
}
else
printf("Root are imeginary\n No Solution \n");   
   
return 0;
}

8
#include <stdio.h>

int main()
{
int number;
int sumOfDigits = 0;

printf("Enter Number : ");
scanf("%d", &number);

while(number > 0)
{
int ld = number % 10;
sumOfDigits += ld;
number /= 10;
}
printf("Sum Of Digits : %d", sumOfDigits);

return 0;
}

9
#include <stdio.h>

int main()
{
int number;
int I;

printf("Enter Number : ");
scanf("%d", &number);

for(I = 1; I <= 10; I++)
{
printf ("%d * %d = %d \n", number, I, number * I);
}

return 0;
}

10
#include <stdio.h>

int main()
{
int number;
int firstDigit;
int lastDigit;
int sum;

printf("Enter four digit Number : ");
scanf("%d", &number);

firstDigit = number / 1000;
lastDigit = number % 10;
sum = firstDigit + lastDigit;
printf("Sum : %d", sum);

return 0; 

}

11
#include <stdio.h>

int main()
{

int number1;
int number2;
int number3;

printf("Enter Number 1 : ");
scanf("%d", &number1);
printf("Enter Number 2 : ");
scanf("%d", &number2);
printf("Enter Number 3 : ");
scanf("%d", &number3);


if ((number1 > number2) && (number1 > number3))
printf ("Number 1 : %d is greatest",number1);
if ((number2 > number1) && (number2 > number3))
printf ("Number 2 : %d is the greatest",number2);
if ((number3 > number1) && (number3 > number2));
printf ("Number 3 : %d is the greatest",number3);

return 0;

}

12
#include <stdio.h>

int main()
{
int number;
int N;
int ld;
int reverseNumber = 0;

printf("Enter Number to Reverse : ");
scanf("%d", &number);

N = number;

while(N > 0)
{
ld = N % 10;
reverseNumber = reverseNumber * 10 + ld;
N = N / 10;
}

printf("Reverse Number : %d", reverseNumber);

return 0;

}

13
#include <stdio.h>

int main()
{
int number;
int reverseNumber = 0;
int N;
int difference;

printf("Enter Number : ");
scanf("%d", &number);

N = number;
while(N > 0)
{
int ld = N % 10;
reverseNumber = reverseNumber * 10 + ld;
N /= 10;
}

difference = number - reverseNumber;

if(difference % 9 == 0)
{
printf("Difference is 9");
}
else
{
printf("Difference is not 9");
}

return 0;


}

14
#include <stdio.h>

int main()
{
char ch;

printf("Enter an alphabet : ");
scanf("%c", &ch);

if(ch >= 65 && ch <= 91 )
{
ch += 32; //changing upper case to lower case
}

switch(ch)
{
case 'a' : 
case 'e' :
case 'i' :
case 'o' :
case 'u' :
printf("Vowel");
break;
case 'b' :
case 'c' :
case 'd' :
case 'f' :
case 'g' :
case 'h' :
case 'j' :
case 'k' :
case 'l' :
case 'm' :
case 'n' :
case 'p' :
case 'q' :
case 'r' :
case 's' :
case 't' :
case 'v' :
case 'w' :
case 'x' :
case 'y' :
case 'z' :
printf("Consonant");
break;
default :
printf("Invalid English Character Entered");
}

return 0;

}

15
#include <stdio.h>

int main()
{
int marks;

printf("Enter Marks : ");
scanf("%d", &marks);

switch(marks)
{
case 80 :
case  81 :
case  82 :
case  83 :
case  84 :
case  85 :
case  86 :
case  87 :
case  88 :
case  89 :
case  90 :
case  91 :
case  92 :
case  93 :  
case  94 :
case  95 :
case  96 :
case  97 :
case  98 :
case  99 :
case  100 :
printf("Grade A");
break;
case  60:
case  61:
case  62:
case  63:
case  64:
case  65:
case  66:
case  67:
case  68:
case  69:
case  70:
case  71:
case  72:
case  73:
case  74:
case  75:
case  76:
case  77:
case  78:
case  79:
printf("Grade B");
break;
case 50 :
case 51 :
case 52 :
case 53 :
case 54 :
case 55 :
case 56 :
case 57 :
case 58 :
case 59 :
printf("Grade C");
break;
case 40 :
case 41 :
case 42 :
case 43 :
case 44 :
case 45 :
case 46 :
case 47 :
case 48 :
case 49 :
printf("Grade D");
break;
case : 0
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 :
case 8 :
case 9 :
case 10 :
case 11 :
case 12 :
case 13 :
case 14 :
case 15 :
case 16 :
case 17 :
case 18 :
case 19 :
case 20 :
case 21 :
case 22 :
case 23 :
case 24 :
case 25 :
case 26 :
case 27 :
case 28 :
case 29 :
case 30 :
case 31 :
case 32 :
case 33 :
case 34 :
case 35 :
case 36 :
case 37 :
case 38 :
case 39 :
printf("Grade E");
break;
default :
printf("Invalid Marks");
}

return 0;

}

17
#include <stdio.h>

int main()
{
int marks;

printf("Enter Marks : ");
scanf("%d", &marks);

if(marks >= 80 && marks <= 100)
{
printf("Grade A");
}
else if(marks >= 60 && marks < 80)
{
printf("Grade B");
}
else if(marks >= 50 && marks < 60)
{
printf("Grade C");
}
else if(marks >= 40 && marks < 50)
{
printf("Grade D");
}
else if(marks >= 0 && marks < 40)
{
printf("Grade E");
}
else
{
printf("Invalid Marks");
}

return 0;

}

18
#include <stdio.h>

int main()
{
unsigned long factorial = 1;

int n;
int  i;
scanf("%d", &n);

i = n;
while(i>1)
{
factorial *= i;
i--;
}
printf("Factorial : %d", factorial);

return 0;
}

18 - using for loop
#include <stdio.h>

int main()
{
unsigned long factorial = 1;
int n;
int  i;
scanf("%d", &n);
for(i = n; i>1; i--)
factorial *= i;
printf("Factorial : %d", factorial);
return 0;
}

19
#include <stdio.h>

int main()
{
int number = 0;
int I;

printf ("Number : ");
scanf ("%d", &number);

for(I = 2; I <= number; I++)
{
if(number % I == 0)
{
printf("%d * ", I);
number = number / I;
I--;
}
}

return 0;

}

20 Hard way
#include <stdio.h>

int main()
{
int number1;
int number2;

printf("Number 1 : ");
scanf("%d", &number1);
printf("Number 2 : ");
scanf("%d", &number2);

int answer = gcd(number1, number2);

printf("\nGCD : %d", answer);

return 0;
}
unsigned int gcd(unsigned int u, unsigned int v)
{
// simple cases (termination)
if (u == v)
return u;
if (u == 0)
return v;
if (v == 0)
return u;
// look for factors of 2
if (~u & 1) // u is even
if (v & 1) // v is odd
return gcd(u >> 1, v);
else // both u and v are even
return gcd(u >> 1, v >> 1) << 1;
if (~v & 1) // u is odd, v is even
return gcd(u, v >> 1);
 // reduce larger argument
 if (u > v)
return gcd((u - v) >> 1, v);
 
return gcd((v - u) >> 1, u);

}

20 Simple Way
#include <stdio.h>

int main()
{
int number1;
int number2;
int min;
int I;

printf("Number1 : ");
scanf("%d", &number1);
printf("Number2 : ");
scanf("%d", &number2);

if(number1 > number2)
         min = number2;
    else
         min = number1;

    for(I = min; I >= 1; I--)
{
        if(number1 % I == 0 && number2 % I == 0)
{
printf("\nGCD of two numbers : %d", I) ;
            break;
        }
    }

return 0;

}

21
#include <stdio.h>

int main()
{
int row = 0;
int col = 0;

    for(row = 1; row <= 5; row++)
    {
for(col=1;col<=row;col++)
{
printf ("%d",col);
}
printf ("\n");
}

return 0;


}

22
#include <stdio.h>

int main()
{
int numberOfTerms;

printf("Enter N : ");
scanf("%d", &numberOfTerms);

int secondLastTerm = 0;
int lastTerm = 1;
int term; // lastTerm + secondLastTerm;

int i;

printf(" %d ", secondLastTerm);
printf(" %d ", lastTerm);

for(i = 3; i <= numberOfTerms; i++)
{
term = lastTerm + secondLastTerm;
printf(" %d ", term);

secondLastTerm = lastTerm;
lastTerm = term;
}

return 0;

}

23
#include <stdio.h>

int main()
{
int n;
int I = 0;
int count = 0;
printf("Enter N : ");
scanf("%d", &n);

int age[n];

while(1)
{
printf("Student %d Age : ", I+1);
scanf("%d", &age[I]);
if(age[I] <20 && age[I]>15 )
{
count++;
}
if(I == n-1)
{
break;
}
else
{
I++;
continue;
}
}
printf("\n%d students have age between 15 and 20", count);

return 0;

}

24
#include <stdio.h>

int main()
{
int I;
int count = 0;
for(I = 2; I<100; I++ )
{
if( !(I % 2 == 0 || I % 3 == 0) )
{
count++;
printf("%d\t", I);
}
continue;
}
printf("\n\n%d numbers between 1-100 are not dicvisible by 2 and 3", count);
return 0;
}

25
#include <stdio.h>

int main()
{
printf("Size of Char        : %d bytes\n",sizeof(char));
printf("Size of int         : %d bytes\n",sizeof(int));
printf("Size of float    : %d bytes\n",sizeof(float));
printf("Size of double      : %d bytes\n",sizeof(double));
printf("Size of long        : %d bytes\n",sizeof(long));
printf("Size of long double : %d bytes\n",sizeof(long double));
printf("Size of long long   : %d bytes\n",sizeof(long long));

return 0;
}

Pattern 1
*
**
***
****
*****

#include <stdio.h>

int main()
{
int n;
int row;
int col;
    printf("Number of Rows : ");
    scanf("%d", &n);
    for(row = 1; row <= n; row++)
    {
for (col = 1; col <= row; col++)
        {
            printf ("*");
}
printf ("\n");
}
return 0;
}

Pattern 2
*****
 ****
  ***
   **
    *

#include <stdio.h>

int main()
{
int row;
int col;
int col1;
for(row = 1; row <= 5; row++)
{
for(col = 1; col<row; col++)
{
printf(" ");
}
for(col1 = 5; col1>=row; col1--)
{
printf("*");
}
printf("\n");
}

return 0;
}

Pattern 3
*****
****
***
**
*

#include <stdio.h>

int main()
{
int row;
int col1;
for(row = 1; row <= 5; row++)
{
for(col1 = 5; col1 >= row; col1--)
{
printf("*");
}
printf("\n");
}
return 0;
}


Pattern 4
1

22

333

4444
55555

#include <stdio.h>

int main()
{
int row;
int col;
for(row = 1; row<=5; row++)
{
for(col = 1; col<=row; col++)
{
printf("%d",row);
}
printf("\n");
}
return 0;
}

Pattern 5
111
22
3

#include <stdio.h>

int main()
{
int row;
int col;
int col1;
for(row = 1; row <= 4; row++)
{
for(col = 1; col <= 4-row; col++)
{
printf("%d",row);
}
printf("\n");
}

return 0;
}

Pattern 6
1
23
456
78910

#include<stdio.h>

int main ()
{
    int row;
int col;
int k = 0;
    for(row = 1; row <= 4; row++)
    {
for(col = 1; col <= row; col++)
        {
printf ("%d",++k);
        }
printf ("\n");
    }
}




Get Ebooks delivered to your email id

Comments

Subscribe to our channel

Facebook

Powered by Blogger.

Home | Contact Us | DMCA | Terms of Service | Privacy | Advertise

Maven Scientists