Lab 6document From ?
Lab 6document From ?
Lab 6document From ?
ROLL # 102
SEC: C
LAB # 06
Task 1: Assume you have 3 integer variables in your program with
names a, b, and c. Write down a program which perform following
tasks.
i. Declare and initialize each variable in a separate statement.
ii.Declare each variable in separate statement, then initialize each variable
separate statement.
separate statement.
(i)SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a= 3;
int b= 6;
int c= 9;
printf("the integer one is %d\n",a);
printf("the integer two is %d\n",b);
printf("the integer three is %d\n",c);
return 0;
}
OUTPUT:
(ii)SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int b;
int c;
printf("the integer one is: ",a);
scanf("%d",&a);
printf("the integer two is: ",b);
scanf("%d",&b);
printf("the integer three is: ",c);
scanf("%d",&c);
printf("the integer one is %d\n",a);
printf("the integer two is %d\n",b);
printf("the integer three is %d\n",c);
return 0;
}
OUTPUT:
(iii)SOURCE CODE:
(iii)SOURCE CODE:
#include<stdio.h>
int main()
{
int a,b,c;
printf("the integer one is ",a);
scanf("%d",&a);
printf("the integer two is ",b);
scanf("%d",&b);
printf("the integer three is ",c);
scanf("%d",&c);
return 0;
}
OUTPUT:
(iv)SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=2,b=5,c=9;
printf("the integer one is %d\n",a);
printf("the integer two is %d\n",b);
printf("the integer three is %d\n",c);
return 0;
}
OUTPUT:
(v)SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=2;
int b=5;
int c=9;
printf("\nthe integer three is %d\n",c);
return 0;
}
OUTPUT:
Lab Task 2: Perform each of the following operations in your
program. Does your program run? If yes, then what is output, and if
no, then what is the error generated?
i. Try to print the value of a variable, which is not declared in your program
. ii. Try to print the value of a variable, which is declared but not initialized in your program.
iii. Try to assign the value of a variable beyond its allowable range and then print its value.
iv. Try to assign an integer variable the value of a float and then print its value.
v. Try to assign a character variable the value of a string (i.e. in double quotes instead of a
single quote) and then print its value.
a. Try to assign an integer variable the result of expression ¾ and print its value.
b. Try to assign an integer variable the result of expression 3.0/4.0 and print its value.
c. Try to assign a float variable the result of expression ¾ and print its value.
d. Try to assign a float variable the result of expression 3.0/4.0 and print its value.
(i)Source code:
#include<stdio.h>
Int main()
{
Printf(enter your name);
Scanf(“%d”,&a);
Return (0);
}
OUTPUT:
Error
(ii)Source code
#include<stdio.h>
int main()
{
int a=14,b;
printf("\na=%d",a);
printf("\nb=%d",b);
return(0);
}
OUTPUT:
(iii)SOURCE CODE:
#include<stdio.h>
int main()
{
int a=3233;
printf("\na=%d",a);
return(0);
}
OUTPUT:
(iv)SOURCE CODE:
#include<stdio.h>
int main()
{
int a=245.799;
printf("\na=%d",a);
return(0);
}
OUTPUT:
(V)
(a)SOURCE CODE:
#include<stdio.h>
int main()
{
int a=3/4;
printf("\na=%d",a);
return(0);
}
OUTPUT:
(b)SOURCE CODE:
#include<stdio.h>
int main()
{
int a=3.0/4.0;
printf("\na=%d",a);
return(0);
}
OUTPUT:
(c)SOURCE CODE:
#include<stdio.h>
int main()
{
float a=3.0/4.0;
printf("\na=%f",a);
return(0);
}
OUTPUT:
(d)SOURCE CODE:
#include<stdio.h>
int main()
{
float a=3.0/4.0;
printf("\na=%f",a);
return(0);
}
OUTPUT:
Lab Task 3: Declare and initialize two variables a, b and calculate the
sum and store it in a variable c. Declare and initialize two variables a,
b and calculate the sum without storing it in a variable.
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
int b;
int c;
printf("enter value one ",a);
scanf("%d",&a);
printf("enter second value ",b);
scanf("%d",&b);
c=a+b;
printf("addition is %d",c);
return 0;
}
OUTPUT:
Task 4: Print following shape using simple printf statements:
(1)SOURCE CODE:
#include <stdio.h>
Main ()
{
int i,j,r;
printf("Input number of rows (half of the diamond) :");
scanf("%d",&r);
for(i=0;i<=r;i++)
{
for(j=1;j<=r-i;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
for(i=r-1;i>=1;i--)
{
for(j=1;j<=r-i;j++)
printf(" ");
for(j=1;j<=2*i-1;j++)
printf("*");
printf("\n");
}
}
OUTPUT:
(2)SOURCE CODE:
#include<stdio.h>
int main()
{
int size, i, j;
printf( "Input the size of the square: " );
scanf( "%d", &size );
if(size < 1 || size > 10) {
printf("Size should be in the range 1 to 10\n");
return 0;
}
for(i=0; i<size; i++){
for(j=0; j<size; j++)
{
if(i==0 || i==size-1)
printf("*");
else if(j==0 || j==size-1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
OUTPUT:
(3)SOURCE CODE:
#include <stdio.h>
#include <conio.h>
main()
{
int i,j;
for (i=0; i<7; i++) {
for (j=0; j<=i; j++) {
printf(" * ");
}
printf("\n");
}
getch();
}
OUTPUT:
(4)SOURCE CODE:
#include<stdio.h>
int main()
{
int i,j,k;
for(i=0 ; i<=7 ; i++)
{
for(j=7 ; j>i ; j--)
printf(" ");
for(k=0 ; k<=i; k++)
{
if(k%2==0)
printf("*");
else
printf("*");
}
printf("\n");
}
return 0;
}
OUTPUT:
(5)SOURCE CODE:
#include <stdio.h>
int main()
{
int i, j, N;
int count;
printf("Enter N: ");
scanf("%d", &N);
count = N * 2 - 1;
for(i=1; i<=count; i++)
{
for(j=1; j<=count; j++)
{
if(j==i || (j==count - i + 1))
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
OUTPUT:
(6)SOURCE CODE:
#include <stdio.h>
#include <conio.h>
main()
{
int i,j;
for (i=0; i<8; i++) {
for (j=0; j<5; j++) {
printf(" * ");
}
printf("\n");
}
getch();
}
Output: